如何打印父节点和前两个子节点的文本

时间:2016-03-31 13:02:37

标签: xslt xpath

我的XML是:

<content>
  <p>The <organization>Committee on Civil Liberties</organization> calls on the <organization>Committee on Foreign Affairs</organization>, as the committee responsible:
    <mod>
      <quotedStructure>
        <recital id="_900411">
                <num>A.</num>
                <p>Whereas the progressive development ...</p>
            </recital>
            <recital id="_900412">
                <num>B.</num>
                <p>Whereas Article ...</p>
            </recital>
            <recital id="_900413">
                <num>C.</num>
                <p>Whereas, while Member States ...</p>
            </recital>
        </quotedStructure>
    </mod>
</p>

我的预期输出是:

  

公民自由委员会呼吁外交委员会作为负责的委员会:

     

一个。而逐步发展......

     

B中。而文章......

     

℃。鉴于会员国......

我的XSLT是:

<xsl:template match="content/p">
 <xsl:value-of select="text()"/>
 <xsl:apply-templates/>
</xsl:template>
<xsl:template match="content//recital">
   <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="content//recital/num">
   <xsl:value-of select="."/>
 </xsl:template>
 <xsl:template match="content//recital/p">
   <xsl:value-of select="." />
 </xsl:template>

我的当前输出是:

  

作为负责委员会的电话:

     

一个。而逐步发展......

     

B中。而文章......

     

℃。鉴于会员国......

注意:我遗漏了父<organization>节点的<p>子节点内的文本。 请让我知道XPath和XSLT以获得预期的输出。

1 个答案:

答案 0 :(得分:0)

使用此(固定)XML文件

<content>
    <p>The <organization>Committee on Civil Liberties</organization> calls on the <organization>Committee on Foreign Affairs</organization>, as the committee responsible:
        <mod >
            <quotedStructure>
                <recital id="_900411">
                    <num>A.</num>
                    <p>Whereas the progressive development ...</p>
                </recital>
                <recital id="_900412">
                    <num>B.</num>
                    <p>Whereas Article ...</p>
                </recital>
                <recital id="_900413">
                    <num>C.</num>
                    <p>Whereas, while Member States ...</p>
                </recital>
            </quotedStructure>
        </mod>
    </p>
</content>

和这个XSLT文件

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

  <xsl:template match="content">
    <xsl:apply-templates select="*"/>
  </xsl:template>

  <xsl:template match="p/text()">
    <xsl:for-each select=".">
      <xsl:value-of select="." />
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="mod/quotedStructure">
    <xsl:for-each select="recital">
      <xsl:text>&#10;</xsl:text>
      <xsl:value-of select="concat(num/text(),' ')" />
      <xsl:value-of select="p/text()" />
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

结果如下:

The Committee on Civil Liberties calls on the Committee on Foreign Affairs, as the committee responsible:


A. Whereas the progressive development ...
B. Whereas Article ...
C. Whereas, while Member States ...