Ben
Ben

Reputation: 784

All the nodes having an attribute

Is there some kind of xpath syntax I could use to get all the nodes(including child nodes) that have an identifierref present in the xml below? I've been trying something like XmlNodeList nodeList = xmlDoc.SelectNodes("//@identifierref"); but that doesn't return the child title node below the item nodes. Ideally I want to obtain a node list which has access to the item nodes that have identifierref present and the title nodes below them. Below is the xml I'm working with. Thanks in advance.

<organization xmlns:adlcp="test1" xmlns="test2">
      <title>1.2 Tester</title>
      <item identifier="C2_LESSON1">
        <title>TestName1</title>
        <item identifier="I_SCO1" identifierref="SCO01">
          <title>Tester SCO 1</title>
        </item>
      </item>
      <item identifier="C2_LESSON2">
        <title>TestName2</title>
        <item identifier="I_SCO2" identifierref="SCO01">
          <title>Tester SCO 2</title>
        </item>
      </item>
      <item identifier="C2_LESSON3">
        <title>TestName3</title>
        <item identifier="I_SCO3" identifierref="SCO01">
          <title>Tester SCO 3</title>
        </item>
      </item>
    </organization>

Upvotes: 0

Views: 94

Answers (1)

user357812
user357812

Reputation:

I want to obtain a node list which has access to the item nodes that have identifierref present and the title nodes below them

//item[@identifier]

The above selects all item elements in the document having an identifier attribute.

If you want to select also the title element child.

//item[@identifier]|//item[@identifier]/title

But node set result order depends on host language (the most work with document order). Also there is no "grouping" feature: you can't iterate over each two because maybe some title is missing.

So, use first XPath expression first, and then iterate over those item getting the title with any DOM method or a relative XPath expression like:

title

Upvotes: 1

Related Questions