如何在 XML/XSD 中获取属性的元素名称
How to get attribute's element name in the 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 文件中找到它,但我不能'看不到这是属性和它的元素是某某的任何关系)。我怎样才能得到这个值?最后 我只想形成一个像这样的字符串 book.category.
任何人都可以建议我如何形成这个字符串吗?提前致谢。
用于获取属性名称类别的代码。
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. You can read more about advanced xml parsing at Scala: Deeper XML parsing, and extracting XML tag attributes 作者 Alvin Alexander。
这是我用来解析的 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 文件中找到它,但我不能'看不到这是属性和它的元素是某某的任何关系)。我怎样才能得到这个值?最后 我只想形成一个像这样的字符串 book.category.
任何人都可以建议我如何形成这个字符串吗?提前致谢。
用于获取属性名称类别的代码。
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. You can read more about advanced xml parsing at Scala: Deeper XML parsing, and extracting XML tag attributes 作者 Alvin Alexander。