Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT Validate and Concatenate Multiple Variables

Tags:

xslt

xslt-1.0

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>
like image 566
Karthik Avatar asked Dec 02 '22 03:12

Karthik


1 Answers

In XSLT 2.0: string-join((Address, Address1, City, State, Zipcode), ',')

In XSLT 1.0, provided you're outputting the results in document order:

<xsl:for-each select="Address | Address1 | City | State | Zipcode">
  <xsl:if test="position() != 1">, </xsl:if>
  <xsl:value-of select="."/>
</xsl:for-each>

If not in document order, then like many things in XSLT 1.0, it's probably rather tedious.

like image 131
Michael Kay Avatar answered Jan 11 '23 23:01

Michael Kay