Reputation: 1
Can anybody helps me to figure out a way to print the current value of the list, I have to loop on List tag and print his current children.
Input xml:
<Lists>
<List>
<ChildrenList>
<ChildName> John </ChildName>
</ChildrenList>
</List>
<List>
<ChildrenList>
<ChildName> Jo </ChildName>
<ChildName> Smith </ChildName>
</ChildrenList>
</List>
</Lists>
Desired output
<Children>
<ChildrenList1>
<ChildName> John </ChildName>
</ChildrenList1>
</Children>
<Children>
<ChildrenList1>
<ChildName> Jo </ChildName>
<ChildName> Smith </ChildName>
</ChildrenList1>
</Children>
My XSLT
<xsl:template match="/">
<Children>
<ChildrenList1>
<xsl:for-each select="Lists/List/ChildrenList">
<ChildName>
<xsl:value-of select="ChildName"/>
</ChildName>
</xsl:for-each>
</ChildrenList1>
</Children>
</xsl:template>
unfortunately, I got the following
<Children>
<ChildrenList1>
<ChildName> John </ChildName>
<ChildName> Jo </ChildName>
</ChildrenList1>
</Children>
Upvotes: 0
Views: 555
Reputation: 117102
You can get the expected output easily by doing:
<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="/Lists">
<xsl:for-each select="List/ChildrenList">
<Children>
<ChildrenList1>
<xsl:copy-of select="ChildName"/>
</ChildrenList1>
</Children>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note that the result is an XML fragment. To produce a well-formed XML document, you must add a root element, for example:
<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="/Lists">
<Root>
<xsl:for-each select="List/ChildrenList">
<Children>
<ChildrenList1>
<xsl:copy-of select="ChildName"/>
</ChildrenList1>
</Children>
</xsl:for-each>
</Root>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 1816
Use this code: it will achive by apply templates:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="List">
<Children>
<xsl:apply-templates/>
</Children>
</xsl:template>
<xsl:template match="ChildrenList">
<ChildrenList1>
<xsl:apply-templates/>
</ChildrenList1>
</xsl:template>
</xsl:stylesheet>
Or if you really want to achive it by your way then you need one more nesting for-each like below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Children>
<xsl:for-each select="Lists/List/ChildrenList">
<ChildrenList1>
<xsl:for-each select="ChildName">
<ChildName>
<xsl:value-of select="."/>
</ChildName>
</xsl:for-each>
</ChildrenList1>
</xsl:for-each>
</Children>
</xsl:template>
</xsl:stylesheet>
Updated As desired output:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Childrens>
<xsl:for-each select="Lists/List">
<Children>
<xsl:for-each select="ChildrenList">
<ChildrenList1>
<xsl:for-each select="ChildName">
<ChildName>
<xsl:value-of select="."/>
</ChildName>
</xsl:for-each>
</ChildrenList1>
</xsl:for-each>
</Children>
</xsl:for-each>
</Childrens>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1