I need help in processing node attribute value in a loop. Suppose I have a input XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<property type="0-0-1.address" value="a">value1</property>
<property type="0-0-2.address" value="c">value4</property>
<property type="0-0-3.address" value="b">value2</property>
</root>
I want output with changed random values declared using varaibles,but using a single template which can be processed within a loop for type attribute
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<property type="0-0-1.address" value="123">value1</property>
<property type="0-0-2.address" value="abc">value4</property>
<property type="0-0-3.address" value="cd">value2</property>
</root>
I have tried with this xsl, but won't work
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:param name="count" select="0" />
<xsl:variable name="i" select="'1'" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template name="inct" match="property[@type=concat('0-0-',$i,'.address')]/@value">
<xsl:param name="count" />
<xsl:param name="i" select="$count" />
<xsl:attribute name="value">
<xsl:value-of select="concat('0-0-',$i,'.address')" />
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="loop">
<xsl:with-param name="count" select="4" />
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="res" />
<xsl:param name="limit" select="$count+1" />
<xsl:param name="count" select="1" />
<xsl:param name="i" select="$count" />
<xsl:if test="$count > 1">
<xsl:call-template name="inct">
<xsl:with-param name="count" select="$count - 1"/>
<xsl:with-param name="limit" select="$limit" />
</xsl:call-template>
<xsl:value-of select="$count"/>
<xsl:call-template name="loop">
<xsl:with-param name="count" select="$count - 1" />
<xsl:with-param name="limit" select="$limit" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
No comments:
Post a Comment