c# - XPath normalize-space() 不能作为阶梯函数工作?

标签 c# xml xpath dita

我刚刚设计了一个 XPath 表达式来查找所有空元素并直接在结束后返回文本。

我已经在 OXYGEN 中测试了这个 XPath,并返回了我正在寻找的内容。由于我尚不清楚的原因,我无法在 Visual Studio (C#) 中执行此操作。

XPath 表达式:

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

这是 XPath 搜索的 DITA (XML) 文件的示例:

    <?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>

在 OXYGEN 中,使用 XPath 条,结果是“5.2”,这正是我想要检索的结果。

这是检索节点的 C#:

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

是否可以在 C# 中使用像 normalize-space() 这样的 XPath 函数?

最佳答案

您的 XPath 表达式需要 XPath 2.0,Oxygen 支持但原生 C# 库不支持。

顺便说一句,这里讨论的不是 normalize-space() 支持,因为它在两个版本的 XPath 中都受支持;它是将 normalize-space() 作为步进函数使用,而不是在需要 XPath 2.0 的谓词中使用。

您的选择是使用支持 XPath 2.0 的 C# 的 XPath 库,或者通过 C# 遍历 XPath 选定的节点并单独规范化每个节点的字符串值。

关于c# - XPath normalize-space() 不能作为阶梯函数工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46770921/

相关文章:

html - 如何为这个 HTML 编写 XPath 和 CSS?

xml - 在 xpath 表达式中不使用命名空间

mysql - 连接三个 MySQL 表,这样我就不会得到重复数据

xml - AntiXML 中的链接转换

c# - 没有数组名称的XML到Json c#

c# - 将文件拖放到我的列表框中

XML xs :int hexadecimal value

c# - 异常 : The XPath expression evaluated to unexpected type System. Xml.Linq.XAttribute

c# - Console.WriteLine 在 ASP.net 生产环境中去哪里?

c# - 如何将 DateTime 的 "time"转换为 int?