JSP
JSP

Reputation: 587

How to get attribute's element name in the XML/XSD

This is the XML I am using to parse.

<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>

and this is the schema for it.

<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>

I have written code using javax.xml library to validate and parse it and I am able to get all the element names from the XSD. Here I am facing some difficulty in getting attributes's element name.

For an instance, in the above XSD we have an attribute called category and here I want to get it's element name nothing but book(We can find it in XML file and I couldn't see any relation that this is the attribute and it's element is so and so). How can I get this value? Finally I just want to form a string something like this book.category.

Can anyone please suggest me how can I form this String? Thanks In Advance.

Code used to get an attribute name category.

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

Upvotes: 1

Views: 1391

Answers (2)

Tomer Shetah
Tomer Shetah

Reputation: 8529

You can access attributes using @. For example, assuming we have:

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>

So the following:

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

will provide:

List(children, web)

Code run at Scastie. You can read more about advanced xml parsing at Scala: Deeper XML parsing, and extracting XML tag attributes by Alvin Alexander.

Upvotes: 0

Alexandra Dudkina
Alexandra Dudkina

Reputation: 4462

For this particular example you could use following XPath .//*[local-name()='attribute' and @name='category']/ancestor::*[local-name()='element'][1]/@name.

Here is an example of using it in 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>"; 

Upvotes: 2

Related Questions