使用xslt转换嵌套的xml数据

时间:2014-12-28 00:48:08

标签: xml xslt xslt-2.0

我正在尝试使用xslt转换xml;但是,我得到了一个空白的答复。 下面是我的输入xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<add>
    <page>
        <title>3Days 2Night Chiang Mai to Chiang Rai</title>
        <id>83509</id>
        <revision>
            <id>1305791</id>
            <timestamp>2009-11-27T10:35:53Z</timestamp>
            <contributor>
                <username>Texugo</username>
                <id>7666</id>
                <realname />
            </contributor>
            <comment>moved to</comment>
            <text xml:space="preserve">hello</text>
        </revision>
    </page>
</add>

我想将上面的输入文件转换为以下输出格式:

<?xml version="1.0" encoding="UTF-8"?>
<page>
   <title>3Days 2Night Chiang Mai to Chiang Rai</title>
   <id>83509</id>
   <tex>hello<text>
   <comment>moved to</comment>
</page>

以下是我写的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent ="yes"/>
    <xsl:template match="/page" >
    <page>
    <title><xsl:value-of select="title"/></title>
    <id><xsl:value-of select="id"/></id>
    <text><xsl:value-of select="revision/text" /></text>
    <comment><xsl:value-of select="revision/comment" /></comment>
    </page>
    </xsl:template>
</xsl:stylesheet>

但是,我只是获取了元素内没有值的xml骨架。 请帮忙!!

1 个答案:

答案 0 :(得分:1)

/page选择根元素page,这在您的情况下是不正确的。试试/add/pagepage

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/add/page">
        <page>
            <title>
                <xsl:value-of select="title"/>
            </title>
            <id>
                <xsl:value-of select="id"/>
            </id>
            <text>
                <xsl:value-of select="revision/text"/>
            </text>
            <comment>
                <xsl:value-of select="revision/comment"/>
            </comment>
        </page>
    </xsl:template>
</xsl:stylesheet>