xml - How to iterate over IDREFS values in XSLT 1.0? -
i have xml uses idrefs field. need extract id put in own element.
here's basic structure think need don't know use in select functions.
<xsl:template match="node_with_idrefs_field"> <xsl:for-each select="each id in @idrefsfield"> <xsl:element name="newelement"> <xsl:attribute name="ref"><xsl:value-of select="the idref"/></xsl:attribute> </xsl:element> </xsl:for-each> <!-- keep rest of content --> <xsl:apply-templates select="@*|node()"/> </xsl:template>
so node
<node_with_idrefs_field idrefsfield="id1 id2"/>
the result be
<node_with_idrefs_field> <newelement ref="id1"/> <newelement ref="id2"/> </node_with_idrefs_field>
thanks help.
you need tokenize value of idrefsfield
attribute. xslt 1.0 has no native tokenize() function, need call recursive named template you:
<xsl:template match="node_with_idrefs_field"> <xsl:copy> <xsl:call-template name="tokenize"> <xsl:with-param name="text" select="@idrefsfield"/> </xsl:call-template> </xsl:copy> </xsl:template> <xsl:template name="tokenize"> <xsl:param name="text"/> <xsl:param name="delimiter" select="' '"/> <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" /> <xsl:if test="$token"> <newelement ref="{$token}"/> </xsl:if> <xsl:if test="contains($text, $delimiter)"> <!-- recursive call --> <xsl:call-template name="tokenize"> <xsl:with-param name="text" select="substring-after($text, $delimiter)"/> </xsl:call-template> </xsl:if> </xsl:template>
alternatively, if processor supports it, use exslt str:tokenize() extension function.
Comments
Post a Comment