xml - How to apply two 'match' rules inside an xsl:template for a parent and child nodes -
xml - How to apply two 'match' rules inside an xsl:template for a parent and child nodes -
i have next basic xml file:
<structure> <part class="button" id="b1"> <style> <property name="label">click me!</property> </style> </part> </structure> and applying xsl transform html layout xforms button/eventually event well. xsl:template following:
<xsl:template match="part[@class='button']"> <xforms:trigger> <xsl:apply-templates select="@accesskey | @tabindex | @size | @style | @id"/> <xforms:label> <xsl:value-of select="@label"/> </xforms:label> <xsl:apply-templates select="@onclick"/> <xsl:apply-templates select="*"/> </xforms:trigger> </xsl:template> ideally, want xforms:label take text "click me!" , disregard style tag xml. final result should be:
<xforms:trigger id="b1"> <xforms:label>click me!</xf:label> </xforms:trigger> how there? in advance!
edit:
here new illustration of possible scenario of style tag:
<structure> <part class="button" id="b1"/> </structure> <style> <property part-name="b1" name='label'>click me!</property> </style>
to obtain result expecting template can much simpler:
<xsl:template match="part[@class='button']"> <xforms:trigger id="{@id}"> <xforms:label> <xsl:value-of select="style/property[@name='label']"/> </xforms:label> </xforms:trigger> </xsl:template> since template uses context created part, path context property style/property. have used less efficient paths .//property, */property. id copied using attribute value templates.
as other <xsl:apply-templates> seem meaningless unless code much different 1 included in question. lastly 1 add together kid elements trigger doesn't seem want.
update deal sec scenario well, i'll consider have input xml such 1 below, buttons nested styles , others refer style elsewhere in document:
<?xml version="1.0" encoding="utf-8"?> <structure> <style> <property part-name="b2" name="label">click me not!</property> <property part-name="b1" name="label">click me!</property> </style> <part class="button" id="b1"/> <part class="button" id="b3"/> <part class="button" id="b4"> <style> <property name="label">click me!</property> </style> </part> </structure> to refer properties in <style> block can create map selectable key, @part-name. when retrieve element passing button's id key, corresponding property. line sets such map called labels. homecoming <property> element has name attribute value label when called value of part-name attribute:
<xsl:key name="labels" match="property[@name='label']" use="@part-name"/> this template generate code want <part> elements button if element exists in labels map has key equal @id. retrieve element , utilize value:
<xsl:template match="part[@class='button'][key('labels', @id)]"> <xforms:trigger id="{@id}"> <xforms:label> <xsl:value-of select="key('labels', @id)"/> </xforms:label> </xforms:trigger> </xsl:template> this total stylesheet deals instance may contain both scenarios (such 1 listed above):
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xforms="http://www.w3.org/2002/xforms" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <!-- map containing property[@name='label'] using @part-name key --> <xsl:key name="labels" match="property[@name='label']" use="@part-name"/> <xsl:template match="/"> <root> <xsl:apply-templates select="structure/part"/> </root> </xsl:template> <!-- <part> elements not match other 2 templates --> <xsl:template match="part"/> <!-- buttons have @id corresponding style/property @part-name --> <xsl:template match="part[@class='button'][key('labels', @id)]"> <xforms:trigger id="{@id}"> <xforms:label> <xsl:value-of select="key('labels', @id)"/> </xforms:label> </xforms:trigger> </xsl:template> <!-- buttons have nested style/property @name=label --> <xsl:template match="part[@class='button'][style/property[@name='label']]"> <xforms:trigger id="{@id}"> <xforms:label> <xsl:value-of select="style/property[@name='label']"/> </xforms:label> </xforms:trigger> </xsl:template> </xsl:stylesheet> with info generate result:
<?xml version="1.0" encoding="utf-8"?> <root xmlns:xforms="http://www.w3.org/2002/xforms"> <xforms:trigger id="b1"> <xforms:label>click me!</xforms:label> </xforms:trigger> <xforms:trigger id="b4"> <xforms:label>click me!</xforms:label> </xforms:trigger> </root> you can see working , test results in xslt fiddle
xml xslt xforms
Comments
Post a Comment