我想从xml获取架构元素列表。例如
<xs:schema id="SPPOnlineXML" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="SPPOnlineXML" msdata:IsDataSet="true" msdata:MainDataTable="lev_settings" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="lev_settings">
<xs:complexType>
<xs:sequence>
<xs:element name="LEVID" type="xs:int" minOccurs="0" />
<xs:element name="ATTHEADID" type="xs:int" minOccurs="0" />
<xs:element name="MINDATETYPE" type="xs:unsignedByte" minOccurs="0" />
<xs:element name="MINMONTH" type="xs:unsignedByte" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
从上面的模式我想创建一个元素数组&#34; LEVID&#34;,&#34; ATTHEAD&#34;,&#34; MINDATETYPE&#34;,&#34; MINMONTH&#34;
答案 0 :(得分:2)
要将所有 xs:element name-attribute values
作为数组获取,我建议您选择SimpleXML并使用xpath。一旦你在字符串(或文件)中获得了XML,如果你知道xpath表达式,它就非常简单:
# Obtain all element names incl. complexTypes:
//xs:element/@name
# Obtain all element names excl. complexTypes and those
# which contain anything incl. comments, text etc.:
//xs:element[not(node())]/@name
并且每个PHP代码例如:
$xml = simplexml_load_string($string);
echo "Obtain all element names incl. complexTypes:\n";
$elementNames = array_map('strval', $xml->xpath('//xs:element/@name'));
print_r($elementNames);
echo "\nObtain all element names excl. complexTypes and those
which contain anything incl. comments, text etc.:\n";
$elementNames = array_map('strval', $xml->xpath('//xs:element[not(node())]/@name'));
print_r($elementNames);
答案 1 :(得分:0)
代码:
$xml = new DOMDocument();
$xml->load('test.xml');
$xpath = new DOMXPath($xml);
$result = $xpath->query('//xs:sequence/xs:element');
$res_array = array();
foreach($result as $element_node) {
$res_array[] = $element_node->getAttribute('name');
}
var_dump($res_array);
结果:
array(4) {
[0]=>
string(5) "LEVID"
[1]=>
string(9) "ATTHEADID"
[2]=>
string(11) "MINDATETYPE"
[3]=>
string(8) "MINMONTH"
}
答案 2 :(得分:0)
我需要在html表上获取XSD文件的所有xs:元素,完整代码:
<table>
<?php
// Load from xsd file
$url = "myFile.xsd";
$xml = file_get_contents($url, FILE_USE_INCLUDE_PATH);
$xml = simplexml_load_string($xml);
$elementNames = array_map('strval', $xml->xpath('//xs:element/@name'));
foreach ($elementNames as $value) {
echo "<tr><td>$value</td></tr>";
}
$elementNames = array_map('strval', $xml->xpath('//xs:element[not(node())]/@name'));
foreach ($elementNames as $value) {
echo "<tr><td>--->$value</td></tr>";
}
?>
</table>
如果您正在寻找xs:属性,您只需要将所有代码替换为&#34; xs:element&#34; by&#34; xs:attribute&#34;