如何用unicode字符替换嵌套在文本中的元素

时间:2015-11-30 11:51:57

标签: xml xslt xpath

在下面的示例中,我有一个嵌套的空元素,必须用输出文档中的空格字符 替换。

这是输入的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <cd>
     <title>Empire<s/>Burlesque</title>
     <artist>Bob<s/>Dylan</artist>
   </cd>
   <cd>
     <title>Scareface</title>
     <artist>Al<s/>Pacino</artist>
     </cd>
</catalog>

这是 xsl 文件:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates select="/catalog"/>
        </root>
    </xsl:template>
    <xsl:template match="catalog">
        <xsl:for-each select="cd">
            <title>
                <xsl:value-of select="title"/>
            </title>
            <artist>
                <xsl:value-of select="artist"/>
            </artist>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我想要的是这样输出:

<root>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <title>Scareface</title>
    <artist>Al Pacino</artist>
</root>

请注意 Empire Burlesque 之间的空格。目前,输出表示中间没有空格字符的名称。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

怎么样:

XSLT 1.0

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

<xsl:template match="/catalog">
    <root>
        <xsl:apply-templates select="cd/title | cd/artist"/>
    </root>
</xsl:template>

<xsl:template match="title | artist">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="s">
    <xsl:text> </xsl:text>
</xsl:template>

</xsl:stylesheet>
相关问题