xml - XSLT - Trouble to filter parent nodes by its childs using a configuration node -
i have situation in xsl can't figure out how resolve.
i have following xml, , want show main nodes have @ least 1 item enabled config/enable-items nodes.
any hints?
thanks in advance.
xml:
<xml> <main name="main section 1" id="1"> <item id="a"> </item> <item id="b"> </item> <item id="c"> </item> </main> <main name="main section 2" id="2"> <item id="d"> </item> <item id="e"> </item> <item id="f"> </item> </main> <config> <enable-items> <item id="a" /> <item id="b" /> </enable-items> </config> </xml>
wanted output:
main section 1: * * b
p.s.: i've tried using key, defining key enable-items indexed id attribute, , doing for-each on items define variable ... no luck, :(, still don't know how check items inside main before showing main @name attribute ...
well,
i trying using more complex xml, on real world application. when did example post here, managed working :)
follow answer:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:key name="enable-items" match="config/enable-items/item" use="@id"/> <xsl:template match="/xml"> <xsl:apply-templates select="main" /> </xsl:template> <xsl:template match="main"> <xsl:if test="key('enable-items', item/@id)"> <h1><xsl:value-of select="@name"/> </h1> <xsl:for-each select="key('enable-items', item/@id)"> <xsl:value-of select="@id" /> <br> </xsl:for-each> </xsl:if> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment