可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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>
回答1:
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.
回答2:
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:
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
.
回答4:
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