如何使用混合内容?

时间:2016-04-28 16:00:47

标签: xml xslt

我有一个xml输入,如:

<root>
  <section>Start of text<link>link text</link>Back to section.</section>
</root>

我希望使用xslt将输出xml作为:

<File>
  <para>Start of text</para>
  <para>link text</para>
  <para>Back to section.</para>
</File>

我是xslt的新手,不知道该怎么做。有什么建议吗?谢谢!

更新:这是我当前的xslt看起来像

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="root">
    <File>
        <xsl:apply-templates/>
        <xsl:if test="descendant::inner">
            <para>
                <xsl:value-of select="descendant::inner"/>
            </para>
        </xsl:if>
    </File>
</xsl:template>

<xsl:template match="section">
    <xsl:element name="para">
        <xsl:value-of select="text()"/>
    </xsl:element>
</xsl:template>

这是输出:

<File>
   <para>Start of textStart of text</para>
   <para>link text</para>
</File>

谢谢!

1 个答案:

答案 0 :(得分:1)

围绕text()的任何非空降序root节点,并使用para这样的元素:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/root">
    <File>
      <xsl:for-each select=".//text()">
        <xsl:if test="normalize-space(.) != ''">
          <para>      
            <xsl:value-of select="normalize-space(.)"/>
          </para>   
        </xsl:if>
      </xsl:for-each>   
    </File>
  </xsl:template>

</xsl:stylesheet>

输出结果为:

<?xml version="1.0"?>
<File>
    <para>Start of text</para>
    <para>link text</para>
    <para>Back to section.</para>
</File>
相关问题