3

我想在 node 之后和 node 之前获取generate-id(.)所有文本节点的。我正在寻找一些通用的 XSL,而不是与下面提到的示例输入模式紧密耦合。对于任何输入模式,我想获取 node和.<m/></n><m/><n/>

示例输入以便更好地理解:

<a>
  <b> 
    <c>
      This is first text node
    </c> 
  </b> 
  <d> 
    <e>
      This is my second text node
    </e> 
    <f> 
      This is my <m/>third text node 
    </f> 
    <g>
      One more text node
    </g>
  <h>
    <i>
      This is my fourth text node
    </i>
  </h>
  <j> 
    This is my fifth <n/>text node 
  </j>
  <k> 
    <l>
      This is my sixth text node 
    </l>
  </k>     
</d> 
</a> 

预期输出:生成文本节点的 ID,其值为“第三个文本节点”、“另一个文本节点”、“这是我的第四个文本节点”、“这是我的第五个”,它们位于节点<m/><n/>

请给出你的想法。

4

1 回答 1

4

这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vtextPostM" select="//text()[preceding::m]"/>
 <xsl:variable name="vtextPreN" select="//text()[following::n]"/>

 <xsl:variable name="vtextBN-MandN" select=
  "$vtextPostM[count(.|$vtextPreN) = count($vtextPreN)]"/>

 <xsl:variable name="vNL" select="'&#xA;'"/>
 <xsl:variable name="vQ">"</xsl:variable>

 <xsl:template match="/">
   <xsl:for-each select="$vtextBN-MandN">
    <xsl:value-of select=
     "concat($vNL, 'Id: ', $vQ, generate-id(), $vQ,
            'Text: ', $vQ, ., $vQ)
     "/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当应用于提供的 XML 文档时,会产生正确且想要的结果

Id: "IDAOZDLB"Text: "third text node
    "
Id: "IDAQZDLBIDAQZDLB"Text: "
      One more text node
    "
Id: "IDAUZDLBIDAUZDLB"Text: "
      This is my fourth text node
    "
Id: "IDAYZDLB"Text: "
    This is my fifth "

请注意节点集交集的 Kaysian 方法的使用:

$ns1[count(.|$ns2)=count($ns2)]

选择同时属于 nodeset$ns1和 nodeset 的所有节点$ns2

于 2010-04-22T22:26:00.053 回答