xslt - How to use xsl templates and recursion to build a new xml document? -
i know how utilize xsl templates transform xml document xml document with original element hierarchy. add together attributes elements in new generated xml.
my original xml file looks this:
<shop> <product> <cookie id="001"> <price>2</price> </cookie> </product> <product> <bread id="002"> <price>5</price> </bread> </product> <product> <milk id="003"> <price>2</price> </milk> </product> </shop>
i transform next xml:
<newxml> <newelement> <newelement id="001"> <newelement price="2"/> </newelement> </newelement> <newelement> <newelement id="002"> <newelement price="5"/> </newelement> </newelement> <newelement> <newelement id="003"> <newelement price="2"/> </newelement> </newelement> </newxml>
what way this? can done using recursion templates or there improve way? i've been trying utilize next logic:
make template creates element read current elements id if exist , set newelement if current element has child, apply template (some kind of recursion)despite of many attempts haven't been able create work. help appreciated!
to this, build upon xslt identity transform, mutual design pattern in xslt. start have template copies elements , outputs them as-is]
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
what add together templates match elements, , add together codes rename them, or add together attributes required. example, rename root element add together next template:
you don't have utilize specific element names here either. if wanted rename , element id attribute same name, this
<xsl:template match="product/*[@id]"> <newelement> <xsl:apply-templates select="@*|node()"/> </newelement> </xsl:template>
and in case of price element, if wanted create attribute based on element's text content, add together template so:
<xsl:template match="price"> <newelement price="{.}" /> </xsl:template>
(note, uses attribute value templates create attribute here. preferred using xsl:attribute)
try xml starters:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="shop"> <newxml> <xsl:apply-templates select="@*|node()"/> </newxml> </xsl:template> <xsl:template match="product"> <newelement> <xsl:apply-templates select="@*|node()"/> </newelement> </xsl:template> <xsl:template match="product/*[@id]"> <newelement> <xsl:apply-templates select="@*|node()"/> </newelement> </xsl:template> <xsl:template match="price"> <newelement price="{.}" /> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
this outputs following:
<newxml> <newelement> <newelement id="001"> <newelement price="2"/> </newelement> </newelement> <newelement> <newelement id="002"> <newelement price="5"/> </newelement> </newelement> <newelement> <newelement id="003"> <newelement price="2"/> </newelement> </newelement> </newxml>
xml xslt xpath
No comments:
Post a Comment