I have the following input file:
<!-- input.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<input>
<value>
aaa
<value>
bbb
<value>ccc</value>
</value>
</value>
</input>
Expected output:
<!-- output.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
</ul>
How should the XSLT file look like? I tried the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/input">
<ul>
<xsl:for-each select="//value">
<li> <xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
but the the first result (aaa) also contains values from its subtree (bbb, ccc).
Try select="text()"
instead of select="."
to only select the text content.
The key to success in your case is that the template matching value should replicate only text from the current level :
<xsl:value-of select="text()"/>
Actually, it is better to add normalize-space() to remove "additional" white-space chars:
<xsl:value-of select="normalize-space(text())"/>
After printing out of the content corresponding to the current level li element, you should put apply-templates for child nodes only:
<xsl:apply-templates select="*"/>
So the whole script can look like below:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="input">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="value">
<li>
<xsl:value-of select="normalize-space(text())"/>
</li>
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:transform>
For a working example see http://xsltransform.net/pNvs5w2
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.