I have to validate and concatenate multiple variable after validating them.
<xsl:variable name="val1" select="//xpath"/>
<xsl:variable name="val2" select="//xpath"/>
<xsl:variable name="val3" select="//xpath"/>
<xsl:variable name="val4" select="//xpath"/>
<xsl:variable name="val5" select="//xpath"/>
Is there any template available for this or anyone can help me doing this.
Update from comments
I want to concatenate five values like this: Address, Address1, City, State, Zipcode
. If Address
is missing I'll get an output like this ", address1, city, state, zipcode
". I want to get rid of that first comma.
<xsl:variable name="__add" select="translate(//*/text()[contains(., 'Address')]/following::td[contains(@class, 'fnt')][1], ',', '')"/>
<xsl:variable name="address">
<xsl:for-each select="$__add | //*/text()[contains(., 'City')]/following::td[contains(@class, 'fnt')][1] | //*/text()[contains(., 'State')]/following::td[contains(@class, 'fnt')][1] | //*/text()[contains(., 'Pincode')]/following::td[contains(@class, 'fnt')][1]">
<xsl:value-of select="concat(substring(', ', 1 div (position()!=1)), .)"/>
</xsl:for-each>
</xsl:variable>
XSLT has a function
that you can use in the
select=
attribute of another variableor anywhere an attribute value template is allowed (in curly braces).
In XSLT 2.0:
string-join((Address, Address1, City, State, Zipcode), ',')
In XSLT 1.0, provided you're outputting the results in document order:
If not in document order, then like many things in XSLT 1.0, it's probably rather tedious.
An XSLT 1.0 solution preserving the expression order (
xsl:sort
instruction) and using Becker's method (substring()
second argument):It's a pain that you can't use XSLT 2.0... Look how simple!
Note: XPath 2.0 sequence has the implicit order of construction. Because XSLT 2.0
xsl:value-of
instruction handle sequence, there is now a@separator
.May be this can be helful:
Applied to this XML:
Result will be:
And to this XML:
Result will be: