gpt4 book ai didi

.net - .NET 中 XPath id() 的替代方案

转载 作者:行者123 更新时间:2023-12-03 15:30:52 27 4
gpt4 key购买 nike

我有以下 XML 文档:

<text xmlns:its="http://www.w3.org/2005/11/its" >
<its:rules version="2.0">
<its:termRule selector="//term" term="yes" termInfoPointer="id(@def)"/>
</its:rules>
<p>We may define <term def="TDPV">discoursal point of view</term>
as <gloss xml:id="TDPV">the relationship, expressed through discourse
structure, between the implied author or some other addresser,
and the fiction.</gloss>
</p>
</text>
termInfoPointer是指向 <gloss xml:id="TDPV"> 的 XPath 表达式元素。

我使用 LINQ-to-XML 来选择它。
XElement term = ...;
object value = term.XPathEvaluate("id(@def)");

我收到以下异常: System.NotSupportedException: This XPathNavigator does not support IDs.
我找不到解决此问题的方法,因此我尝试更换 id()与其他表达:
//*[@xml:id='TDPV'] // works, but I need to use @def

//*[@xml:id=@def]
//*[@xml:id=@def/text()]
//*[@xml:id=self::node()/@def/text()]

但这些都不起作用。

有没有办法实现 id()或者用另一个表达式替换它?

我更喜欢不涉及更换 id() 的解决方案/解决方法用另一个表达式,因为这个表达式可能很复杂,比如 id(@def) | id(//*[@attr="(id(@abc()))))))"]) .

最佳答案

如果def属性保证在 XML 文档中只出现一次,使用:

//*[@xml:id = //@def]

如果可能有不同 def属性,那么您需要提供一个 XPath 表达式来准确选择想要的 def您的情况下的属性:
//*[@xml:id = someExpressionSelectingTheWantedDefAttribute]

基于 XSLT 的验证:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:copy-of select="//*[@xml:id = //@def]"/>
</xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:
<text xmlns:its="http://www.w3.org/2005/11/its" >
<its:rules version="2.0">
<its:termRule selector="//term" term="yes" termInfoPointer="id(@def)"/>
</its:rules>
<p>We may define <term def="TDPV">discoursal point of view</term>
as <gloss xml:id="TDPV">the relationship, expressed through discourse
structure, between the implied author or some other addresser,
and the fiction.</gloss>
</p>
</text>

计算 XPath 表达式并将计算结果(所选元素)复制到输出 :
<gloss xmlns:its="http://www.w3.org/2005/11/its" xml:id="TDPV">the relationship, expressed through discourse
structure, between the implied author or some other addresser,
and the fiction.</gloss>

关于.net - .NET 中 XPath id() 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15004559/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com