Search code examples
c#xmlxpathdita

XPath normalize-space() not working as a step function?


I've just devised an XPath expression to find all of the empty elements and to return the text directly after the ends.

I have tested this XPath in OXYGEN and returns exactly what I'm looking for. For a reason I don't yet know, I'm unable to do so in Visual Studio (C#).

The XPath expression:

//xref[not(@href)]/normalize-space(substring(following-sibling::text()[1], 1, 20))

Here is an example of the DITA (XML) file which the XPath searches:

    <?xml version="1.0" encoding="UTF-8"?>
<topic id="topic_50038EEEBC214A0EBF0660DCFFCF69C6"> 
  <title> Postulated Piping Failures in Fluid Systems Outside of
     Containment</title> 
  <prolog> 
     <author/> 
  </prolog> 
  <body> 
     <p> 
        <b>Definitions</b> 
     </p> 
     <p> 
        <u>Leakage</u> 
     </p> 
     <p>
        <xref
            href="t1.3.2.2_System Quality Group Classifications.dita#topic_AE87340A0F424D07B0E301E243CCB7B2"
            format="dita" scope="local"><?xm-replace_text System Quality Group Classifications?></xref>
        TEXT
     </p> 
     <p> 
        <u> 
        </u>TEXT
        <xref></xref>5.2
     </p>
     </body>
</topic>

In OXYGEN, using the XPath bar, the result is "5.2" which is exactly what I want to retrieve.

Here is the C# to retrieve the Node:

String xPathFindXREF = "//xref[not(@href)]/normalize-space(substring(following-sibling::text()[1], 1, 20))";
XmlNodeList xmlNodeList = currFile.SelectNodes(xPathFindXREF);

Is it possible to use XPath functions like normalize-space() in C#?


Solution

  • Your XPath expression requires XPath 2.0, which Oxygen supports but the native C# library does not.

    BTW, it's not normalize-space() support that's in question here as that's supported in both versions of XPath; it's the use of normalize-space() as a step function rather than within a predicate that requires XPath 2.0.

    Your options are to use an XPath library for C# that supports XPath 2.0, or iterate over the XPath selected nodes via C# and separately normalize the string values of each node.