0

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:

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)
Todd Hoatson
  • 123
  • 2
  • 18

1 Answers1

1

Your wanted outcome does not match with your efforts....but that is probably a little mistake. (switching @key and headword)

These two XPath's are just working fine:

//div2/concat(@key, ' ', headword/text())
//div2/concat(@key, ',', headword[1])

Oxygen is showing this result:

to/lmh,τόλμη
*to/maros,Τόμαρος
tomei=on,τομεῖον

Maybe your used XPath engines are 1.0?

As an alternative you could try using the very simple xslt 1.0 like this:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" />
  
  <xsl:template match="div2">
    <xsl:value-of select="concat(headword[1]/text(),', ',@key)"/>
  </xsl:template>
  
</xsl:transform>

Will give this result.

  τόλμη, to/lmh
  Τόμαρος, *to/maros
  τομεῖον, tomei=on
Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19
  • "Maybe your used XPath engines are 1.0?" Yes, it turns out, for various reasons, we are not yet able to upgrade. So that is the main reason why it is not working. Thanks, @Siebe. – Todd Hoatson Sep 08 '21 at 19:01