当前位置 : 主页 > 网页制作 > xml >

替代.NET中的XPath id()

来源:互联网 收集:自由互联 发布时间:2021-06-13
我有以下 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 pWe may define term def="TDPV"discoursal point of view/term as
我有以下 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是一个XPath表达式,它指向< gloss xml:id =“TDPV”>元件.

我使用LINQ-to-XML来选择它.

XElement term = ...;
object value = term.XPathEvaluate("id(@def)");

我得到以下异常:System.NotSupportedException:此XPathNavigator不支持ID.

我找不到这个问题的解决方案所以我试图用其他表达式替换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属性保证在XMLdocument中只出现一次,请使用:

//*[@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>
网友评论