如何在 XML/XSD 中获取属性的元素名称



这是我用来解析的XML。

<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

这就是它的模式。

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="title"/>
<xs:element type="xs:string" name="author"/>
<xs:element type="xs:short" name="year"/>
<xs:element type="xs:float" name="price"/>
</xs:sequence>
<xs:attribute type="xs:string" name="category" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

我已经使用javax.xml库编写了代码来验证和解析它,并且能够从XSD中获取所有元素名称。在这里,我在获取属性的元素名称时遇到了一些困难。

例如,在上面的XSD中,我们有一个名为category的属性,在这里我只想得到它的元素名称book(我们可以在XML文件中找到它,但我看不到这是属性和它的元素是某某的任何关系(。如何获取此值?最后我只想形成一个类似于这本书的字符串。类别.

有人能建议我如何组成这根绳子吗?提前感谢。

用于获取属性名称类别的代码。

val attrList = doc.getElementsByTagName("xs:attribute")
val attrName = attrList.item(0).getAttributes.item(0).getNodeValue

对于这个特定的示例,您可以使用以下XPath.//*[local-name()='attribute' and @name='category']/ancestor::*[local-name()='element'][1]/@name

以下是在Java中使用它的示例:

public static void main(String[] args) throws Exception {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
XPathExpression xpath = XPathFactory.newInstance().newXPath().compile(".//*[local-name()='attribute' and @name='category']/ancestor::*[local-name()='element'][1]/@name");
String name = xpath.evaluate(doc);
System.out.println(name);
}
private static String xml = "<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">n"
+ "  <xs:element name="bookstore">n"
+ "    <xs:complexType>n"
+ "      <xs:sequence>n"
+ "        <xs:element name="book" maxOccurs="unbounded" minOccurs="0">n"
+ "          <xs:complexType>n"
+ "            <xs:sequence>n"
+ "              <xs:element type="xs:string" name="title"/>n"
+ "              <xs:element type="xs:string" name="author"/>n"
+ "              <xs:element type="xs:short" name="year"/>n"
+ "              <xs:element type="xs:float" name="price"/>n"
+ "            </xs:sequence>n"
+ "            <xs:attribute type="xs:string" name="category" use="optional"/>n"
+ "          </xs:complexType>n"
+ "        </xs:element>n"
+ "      </xs:sequence>n"
+ "    </xs:complexType>n"
+ "  </xs:element>n"
+ "</xs:schema>"; 

您可以使用@访问属性。例如,假设我们有:

val bookStoreXml =
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

因此以下内容:

(bookStoreXml  "book").map(_  "@category")

将提供:

List(children, web)

代码在Scastie运行。您可以在Scala上阅读更多关于高级xml解析的内容:更深入的xml解析,以及AlvinAlexander提取xml标记属性。

最新更新