xslt - Looking up XML attributes with wildcard values, using XSL -
the task of little piece of xsl go through optionref tags in xml file , print id , displayname attributes optionref tags. id´s looked-up in options section find displayname.
and now, problem: of optionref id attributes use wildcards (*) part of values. these id´s cannot looked-up regards displayname, unless first resolved real id´s found in options section. there way extend xsl kind of "globbing"?
i print id´s , displaynames of id´s matching wildcard. instance "a01*" match both "a0101" , "a0102" in options section, these id´s , display names should printed.
this sample of xml:
<?xml version="1.0" encoding="utf-8"?> <optionlist xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <group> <optionref id="a0102"/> <optionref id="a03"/> <optionref id="a04"/> <optionref id="a0101"/> <optionref id="a0102"/> <optionref id="b01"/> </group> <options> <option displayname="option a02" id="a02"/> <option displayname="option a03" id="a03"/> <option displayname="option a0101" id="a0101"/> <option displayname="option a0102" id="a0102"/> <option displayname="option a04" id="a04"/> <option displayname="option b01" id="b01"/> </options> <rules> <opportunities> <optionref id="a01*"> <optionref id="a03"/> <optionref id="a04"/> </optionref> </opportunities> <problems> <problem> <optionref id="a03"/> <optionref id="a04"/> </problem> </problems> </rules> </optionlist>
this xsl:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output method="text" indent="yes"/> <xsl:key name="option_key" match="option" use="@id"/> <xsl:template match="optionref" > <xsl:value-of select="@id"/>: <xsl:value-of select="key('option_key', @id)/@displayname"/> </xsl:template> </xsl:stylesheet>
how like:
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:strip-space elements="*"/> <xsl:output method="text" encoding="utf-8"/> <xsl:key name="option_key" match="option" use="@id"/> <xsl:variable name="options" select="/optionlist/options/option" /> <xsl:template match="optionref"> <xsl:value-of select="@id"/> <xsl:text>: </xsl:text> <xsl:choose> <xsl:when test="contains(@id, '*')"> <xsl:for-each select="$options[starts-with(@id, substring-before(current()/@id, '*'))]"> <xsl:value-of select="@displayname"/> <xsl:text> </xsl:text> </xsl:for-each> </xsl:when> <xsl:otherwise> <xsl:value-of select="key('option_key', @id)/@displayname"/> <xsl:text> </xsl:text> </xsl:otherwise> </xsl:choose> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet>
applied example input, result be:
a0102: option a0102 a03: option a03 a04: option a04 a0101: option a0101 a0102: option a0102 b01: option b01 a01*: option a0101 option a0102 a03: option a03 a04: option a04
Comments
Post a Comment