[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Got some cool XSLT code that assembles parts toget
At 2012-03-19 19:58 +0000, Costello, Roger L. wrote:
I need code that assembles parts together to create larger parts. Here is a solution without checking for recursion. t:\ftemp>type roger.xml <?xml version="1.0" encoding="UTF-8"?> <Document> <head id="head" idref="body" /> <body id="body" idref="leg" /> <leg id="leg" idref="toes" /> <toes id="toes" /> </Document> t:\ftemp>xslt roger.xml roger.xsl <?xml version="1.0" encoding="utf-8"?> <Document> <head id="head"> <body id="body"> <leg id="leg"> <toes id="toes"/> </leg> </body> </head>
<toes id="toes"/>
<xsl:output indent="yes"/> <xsl:key name="ids" match="*[@id]" use="@id"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> <xsl:apply-templates select="key('ids',@idref)"/> </xsl:copy> </xsl:template> <xsl:template match="@idref"/> </xsl:stylesheet> t:\ftemp> For example, consider this XML document: ... It contains a lot of parts - head, body, leg, toes. A perfect use of <xsl:apply-templates/> After assembly the document should be this: ... As above. I wonder if this particular example is a special case of a general case of assembling parts together to create larger parts? That isn't evidenced in your data, so I've created an example and modified my solution. I'm using XSLT 1.0 ... this would be cleaner in XSLT 2.0 with strings. t:\ftemp>type roger2.xml <?xml version="1.0" encoding="UTF-8"?> <Document> <head id="head" idref="body" /> <body id="body" idref="leg" /> <leg id="leg" idref="toes" /> <toes id="toes" idref="body" /> </Document> t:\ftemp>xslt roger2.xml roger2.xsl <?xml version="1.0" encoding="utf-8"?> <Document> <head id="head"> <body id="body"> <leg id="leg"> <toes id="toes"/> </leg> </body> </head>
<xsl:output indent="yes"/> <xsl:key name="ids" match="*[@id]" use="@id"/> <xsl:template match="@*|node()"> <xsl:param name="visited" select="concat(' ',@id,' ')"/> <xsl:copy> <xsl:apply-templates select="@*|node()"/> <xsl:choose> <xsl:when test="contains($visited,concat(' ',@idref,' '))"> <!-- avoid recursion --> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="key('ids',@idref)"> <xsl:with-param name="visited" select="concat($visited,@idref,' ')"/> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:copy> </xsl:template> <xsl:template match="@idref"/> </xsl:stylesheet> t:\ftemp> I hope this helps, but I'm unsure if you have further requirements. . . . . . . . . Ken -- Contact us for world-wide XML consulting and instructor-led training Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/uoui9h Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|