Reputation: 69
In the below XSL every time the xsl:when gets satisfied I want to append as many <a
> and </a
> tags. But the data that needs to be populated inside the tags should be only once. I have shown the expected output at the end.
<xsl:param name="insert-file" as="document-node()" />
<xsl:template match="*">
<xsl:variable name="input">My text</xsl:variable>
<xsl:variable name="Myxml" as="element()*">
<xsl:call-template name="populateTag">
<xsl:with-param name="nodeValue" select="$input"/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$Myxml"></xsl:copy-of>
</xsl:template>
<xsl:template name="populateTag">
<xsl:param name="nodeValue"/>
<xsl:for-each select="$insert-file/insert-data/data">
<xsl:choose>
<xsl:when test="@index = 1">
<a><xsl:value-of select="$nodeValue"></xsl:value-of></a>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
Current output:
<?xml version="1.0" encoding="UTF-8"?
>
<a
>My text</a
>
<a
>My text</a
>
<a
>My text</a
>
<a
>My text</a
>
I want the template "populateTag" to return me the xml in the below format. How do i modify the template "populateTag" to achive the same.
Expected output from template "populateTag":
<?xml version="1.0" encoding="UTF-8"?
>
<a
><a
><a
><a
>My text</a
></a
></a
></a
>
Please give your ideas.
Upvotes: 0
Views: 245
Reputation: 3982
For that to happen, you need some kind of recursion (to nest the a-elements).
Without trying, because I don't have a sample XML document:
<xsl:param name="insert-file" as="document-node()" />
<xsl:template match="*">
<xsl:variable name="input">My text</xsl:variable>
<xsl:variable name="Myxml" as="element()*">
<xsl:call-template name="populateTag">
<xsl:with-param name="nodeValue" select="$input"/>
<xsl:with-param name="position" select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$Myxml"></xsl:copy-of>
</xsl:template>
<xsl:template name="populateTag">
<xsl:param name="nodeValue"/>
<xsl:param name="position"/>
<xsl:variable name="total" select="count($insert-file/insert-data/data[@index = 1])" />
<xsl:for-each select="$insert-file/insert-data/data[@index = 1]">
<xsl:if test="position() = $position" >
<xsl:choose>
<xsl:when test="position() = $total">
<a><xsl:value-of select="$nodeValue"></xsl:value-of></a>
</xsl:when>
<xsl:otherwise>
<a>
<xsl:call-template name="populateTag">
<xsl:with-param name="nodeValue" select="$input"/>
<xsl:with-param name="position" select="$position+1"/>
</xsl:call-template>
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:template>
Upvotes: 1