1

I am trying to find a specific xsd element of specific parent xsd elements using xpath but could not find a solution.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="XYZ">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="XX" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="AA">
                <xs:complexType>
                  <xs:attribute type="xs:string" name="testA" use="required" />
                  <xs:attribute type="xs:string" name="testB" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="YY" minOccurs="0" />
        <xs:element name="ZZ">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="AA" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I tried the following xpath:

/xs:element[@name='XYZ']/xs:element[@name='XX']/xs:element[@name='AA']

but the result is an empty list.

I hope somebody can help me.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Shorty123
  • 539
  • 1
  • 7
  • 26
  • 1
    please add the actual xsd data on which you apply the xpath expression. – collapsar Oct 25 '19 at 11:34
  • What hosting language are you using to execute the XPath, and how did you account for the XSD namespace? – kjhughes Oct 25 '19 at 12:31
  • Ich verwende die javax#XpathFactory. I am just trying to find a specific element from a given treeitemlist (XYZ, XX, AA). Sorry if i misunderstood your question. – Shorty123 Oct 25 '19 at 12:41
  • Do you understand how to use XML namespaces? – kjhughes Oct 25 '19 at 12:42
  • Maybe not good enough. So your answer is that i should look into xml namespaces again? Or that it is not possible to have two elements with the same name ('AA'). – Shorty123 Oct 25 '19 at 13:11
  • You have `xs:complexType` and `xs:sequence` elements between your `xs:element`... – Alejandro Oct 25 '19 at 13:38
  • Okay the problem is i can Not use the real xsd. So i prepared this quick example. My Hope was that this is enough to Show what i would Like to achieve with the xpath Statement: to get the right AA element by using the name Attributes of the Elements. – Shorty123 Oct 25 '19 at 13:56
  • 1
    Yes, I'm guiding you to be sure you understand [how XPath deals with XML namespaces](https://stackoverflow.com/q/40796231/290085). Be sure to mind @Alejandro's point too. – kjhughes Oct 25 '19 at 15:20
  • 1
    `//xs:element[@name="XYZ"]//xs:element[@name="XX"]//xs:element[@name="AA"]` should give the desired result - you just need to use `//` to handle arbitrary depth of ancestors. [XPath Examples](https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256086(v=vs.100)?redirectedfrom=MSDN) is an indispensable guide. – Filburt Oct 31 '19 at 21:36

1 Answers1

2
//xs:element[@name="XYZ"]//xs:element[@name="XX"]//xs:element[@name="AA"]

should give the desired result - you just need to use // to handle arbitrary depth of ancestors.

From XPath Examples:


bookstore//title

All <title> elements one or more levels deep in the <bookstore> element (arbitrary descendants).


bookstore//book/excerpt//emph

All <emph> elements anywhere inside <excerpt> children of <book> elements, anywhere inside the <bookstore> element.

Filburt
  • 17,626
  • 12
  • 64
  • 115