mak*_*mak 5 .net xpath linq-to-xml
我有以下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>
Run Code Online (Sandbox Code Playgroud)
termInfoPointer是一个指向<gloss xml:id="TDPV">元素的XPath表达式.
我使用LINQ-to-XML来选择它.
XElement term = ...;
object value = term.XPathEvaluate("id(@def)");
Run Code Online (Sandbox Code Playgroud)
我得到以下异常: 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()]
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用.
有没有办法id()用另一个表达式实现或替换它?
我更喜欢不涉及替换id()另一个表达式的解决方案/解决方法,因为这个表达式可能是复杂的id(@def) | id(//*[@attr="(id(@abc()))))))"]).
如果def保证属性仅在XMLdocument中出现一次,请使用:
//*[@xml:id = //@def]
Run Code Online (Sandbox Code Playgroud)
如果可能存在不同的def属性,那么您需要提供一个XPath表达式,def在您的案例中精确选择所需的属性:
//*[@xml:id = someExpressionSelectingTheWantedDefAttribute]
Run Code Online (Sandbox Code Playgroud)
基于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>
Run Code Online (Sandbox Code Playgroud)
在提供的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>
Run Code Online (Sandbox Code Playgroud)
评估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>
Run Code Online (Sandbox Code Playgroud)