How do you link two elements together when they have the same name in xslt? -
How do you link two elements together when they have the same name in xslt? -
i have these trees, 1 construction /cars/car , sec /maker/cars/car. first 1 has reference id of sec list of cars.
<xsl:template match="t:cars/t:car"> <tr> <td> <xsl:if test="position()=1"> <b><xsl:value-of select="../@name"/><xsl:text> </xsl:text></b> </xsl:if> </td> </tr>
i have this, filled in loop larn after bit could't it.
this before:
<xsl:template match="t:cars/t:car"> <tr> <td> <xsl:if test="position()=1"> <b><xsl:value-of select="../@name"/><xsl:text> </xsl:text></b> </xsl:if> <xsl:for-each select="/t:root/t:maker/t:car"> <xsl:if test="t:root/t:maker/@id = @ref"> <xsl:value-of select="@title"/> </xsl:if> </xsl:for-each> </td> </tr>
sample:
auto> <maker type="toyota"> <car name="prius" id="1"/> </maker> <cars name="my collection"> <car ref="1" /> </cars>
this simple transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="kcarbyid" match="maker/car" use="@id"/> <xsl:template match="/*"> <table> <xsl:apply-templates/> </table> </xsl:template> <xsl:template match="cars/car"> <tr> <td> <b> <xsl:value-of select="key('kcarbyid', @ref)/@name"/> </b> </td> </tr> </xsl:template> </xsl:stylesheet>
when applied on xml document (the provided one, extended little):
<auto> <maker type="toyota"> <car name="prius" id="1"/> </maker> <maker type="honda"> <car name="accord" id="2"/> </maker> <maker type="benz"> <car name="mercedes" id="3"/> </maker> <cars name="my collection"> <car ref="2" /> <car ref="3" /> </cars> </auto>
produces wanted, right result:
<table> <tr> <td> <b>accord</b> </td> </tr> <tr> <td> <b>mercedes</b> </td> </tr> </table>
explanation: appropriate utilize of keys.
xslt
Comments
Post a Comment