如何使用xslt获取相同名称的子文本?

时间:2010-06-20 02:01:02

标签: xslt xpath

源:

<article>
<title>title1</title>
<description>des1</description>
<child>
  <children>
    <subarticle>
      <title>subtitle11</title>
      <description>subdes11</description>
    </subarticle>
  </children>
  <children>
    <subarticle>
      <title>subtitle12</title>
      <description>subdes12</description>
    </subarticle>
  </children>
</child>
</article>
<article>
<title>title2</title>
<description>des2</description>
<child>
  <children>
    <subarticle>
      <title>subtitle21</title>
      <description>subdes21</description>
    </subarticle>
  </children>
  <children>
    <subarticle>
      <title>subtitle22</title>
      <description>subdes22</description>
    </subarticle>
  </children>
</child>
</article>

使用xslt后,我希望文档像这样:

<article>
<title>title1</title>
<description>des1 des11 des12</description>
</article>

<article>
<title>title2</title>
<description>des2 des21 des22</description>
</article>

有人提出任何建议吗?谢谢!

1 个答案:

答案 0 :(得分:2)

我猜“des11”和“des12”应该是“subdes11”和“subdes12”。

试试这个:

<xsl:template match="article">
  <article>
    <title><xsl:value-of select="title/text()"/></title>
    <description>
      <xsl:value-of select="description/text()"/>
      <xsl:for-each select="child/children"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="subarticle/description/text()"/>
      </xsl:for-each>
    </description>
  </article>
</xsl:template>