5

我有以下 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()))))))"]).

4

1 回答 1

5

如果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>
于 2013-02-24T17:08:32.687 回答