I'm still new to XPath, and trying to deal with a sizeable dataset having some complex structure. Here's some sample data:
...
<div2 id="n104226" key="to/lmh" type="main">
<headword extent="suff" lang="greek">τόλμη</headword>,
<gen lang="greek">ἡ</gen>,
<sense id="n104226.0" n="A">v. τόλμα.</sense>
</div2>
<div2 id="n104244" key="*to/maros" type="main">
<headword extent="full" lang="greek">Τόμαρος</headword>,
<gen lang="greek">ὁ</gen>,
<sense id="n104244.0" n="A">v. Τομοῦροι.</sense>
</div2>
<div2 id="n104248" key="tomei=on" type="main">
<headword extent="suff" lang="greek">τομεῖον</headword>,
<gen lang="greek">τό</gen>,
</div2>
...
I would like to produce a list of the headwords, along with the key attribute on the parent element (div2), like this:
...
τόλμη, to/lmh
Τόμαρος, *to/maros
τομ-εῖον, tomei=on
...
I have looked at numerous posts here on SO to get ideas, such as:
- How to get the attribute value of a parent element in Xpath if Child element exists?
- Xpath query to get the parent Element text and its child element attribute values?
- How to get a specifc information for an XML file
- Parse XML based on attributes and text values of related nodes
- Concatenate multiple node values in xpath
I have made several attempts at defining an XPath expression to do this, but without success. I have been trying out these expressions using the Evaluate XPath feature of XML Copy Editor, but ultimately I want to use the expression in my C# code, so I have tried both. Here are some of the expressions I have tried:
//div2/string-join(@key | headword, "<>")
//div2/concat(@key, ' ', headword/text())
//div2/concat(@key, ',', headword[1])
Each time XML Copy Editor gives me this error message:
Cannot evaluate XPath: Error at line 0: Invalid expression
Meanwhile, .NET throws an exception of the form:
{"'//div2/concat(@key, ',', headword1)' has an invalid token."}
IIRC, this last one worked for my coworker when he tried it in Oxygen.
I tried this one in XML Copy Editor:
concat(//div2/@key, ',', //div2/headword/text())
It did not result in an error message, but it only produced the first result.
Can anyone help me get this figured out?
Just for completeness, here's a snip of my C# code:
XPathNavigator navigator = ResourceDocument.CreateNavigator();
XPathNodeIterator nodeSetTmp =
navigator.Select(@"//div2/headword[1]/text()", nsmgr);
Thanks for any suggestions...
UPDATE
Here are some more XPath expressions that I tried (which did not work):
//div2 ! string-join((headword, " ", @key), "")
//div2/string-join((headword, " ", @key), "")
//div2/concat(headword, " ", @key)