XSLT Validate and Concatenate Multiple Variables

2019-05-11 09:47发布

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 xslt-1.0
4条回答
别忘想泡老子
2楼-- · 2019-05-11 10:25

XSLT has a function

concat(string, string, ...)

that you can use in the select= attribute of another variable

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

or anywhere an attribute value template is allowed (in curly braces).

查看更多
仙女界的扛把子
3楼-- · 2019-05-11 10:28

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.

查看更多
Lonely孤独者°
4楼-- · 2019-05-11 10:32

An XSLT 1.0 solution preserving the expression order (xsl:sort instruction) and using Becker's method (substring() second argument):

<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> 

It's a pain that you can't use XSLT 2.0... Look how simple!

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

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.

查看更多
Evening l夕情丶
5楼-- · 2019-05-11 10:40

May be 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>

Applied to this XML:

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

Result will be:

test, test2, test3, test4, test5

And to this XML:

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

Result will be:

test2, test4
查看更多
登录 后发表回答