XSLT validates and combines multiple variables

I need to check and combine several variables after checking 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 for this, or can someone help me with this.

Update from comments

I want to combine the five values, like this: Address, Address1, City, State, Zipcode. If Addressabsent, I get output like " , address1, city, state, zipcode". I want to get rid of this 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>
+3
source share
4 answers

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

In XSLT 1.0, if you output 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 the order of the document, then, like so many things in XSLT 1.0, this is probably pretty tedious.

+7
source

XSLT has a function

concat(string, string, ...)

select=

<xsl:variable name="vals" select="{concat($val1,$val2,$vl3,$val4,$val5)}"/>

( ).

+5

XSLT 1.0, ( xsl:sort) (substring() ):

<xsl:for-each select="Address|Address1|City|State|Zipcode">
   <xsl:sort select="substring-before(
                        '|Address|Address1|City|State|Zipcode|',
                        concat('|',name(),'|')
                     )"/>
   <xsl:value-of select="concat(
                            substring(
                               ', ',
                               1 div (position()!=1)
                            ),
                            .
                         )"/>
</xsl:for-each> 

, XSLT 2.0... , !

<xsl:value-of 
     select="Address, Address1, City, State, Zipcode"
     separator=", "/>

Note : The XPath 2.0 sequence has an implicit build order. Since the sequence of XSLT 2.0 command descriptors xsl:value-ofnow exists @separator.

+1
source

Maybe this can be helful:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="/*">
        <xsl:variable name="vMyVars">
            <xsl:apply-templates select="Address | Address1 | City | State | Zipcode" mode="vMyVars"/>
        </xsl:variable>
        <xsl:value-of select="substring($vMyVars, -1, string-length($vMyVars))"/>
    </xsl:template>

    <xsl:template match="*" mode="vMyVars"/>

    <xsl:template match="*[normalize-space()]" mode="vMyVars">
        <xsl:value-of select="."/>
        <xsl:text>, </xsl:text>
    </xsl:template>

</xsl:stylesheet>

Applies to this XML:

<elems>
    <Address>test</Address>
    <Address1>test2</Address1>
    <City>test3</City>
    <State>test4</State>
    <Zipcode>test5</Zipcode>
</elems>

The result will be:

test, test2, test3, test4, test5

And to this XML:

<elems>
    <Address1>test2</Address1>
    <City>       </City>
    <State>test4</State>
    <Zipcode></Zipcode>
</elems>

The result will be:

test2, test4
0
source

Source: https://habr.com/ru/post/1790879/


All Articles