简体   繁体   中英

Transforming nested xml data using xslt

I am trying to transform xml using xslt; however, I am getting a blank response. Below is my input 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>

I want to transform the above input file to the following output format:

<?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>

Below is the XSL which i have written:

<?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>

However, I am just getting the xml skeleton with no values inside the elements. Please help!!

/page selects the root element page , which is incorrect in your case. Try /add/page or just page :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM