XSLT 验证和连接多个变量
我必须在验证多个变量后验证并连接它们。
<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"/>
有可用的模板吗?或者任何人都可以帮助我做到这一点。
从评论中更新
我想连接五个值,如下所示:Address、Address1、City、State、Zipcode
。如果 Address
丢失,我将得到类似这样的输出:“, address1, city, state, zipcode
”。我想去掉第一个逗号。
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 XSLT 2.0 中:
string-join((Address, Address1, City, State, Zipcode), ',')
在 XSLT 1.0 中,假设您按文档顺序输出结果:
如果不在文档中顺序,那么就像 XSLT 1.0 中的许多事情一样,它可能相当乏味。
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.
XSLT 有一个函数
,您可以在另一个变量的
select=
属性中或任何允许属性值模板(在花括号中)的地方使用该函数。
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).
保留表达式顺序(
xsl:sort
指令)并使用 Becker 方法(substring()
第二个参数)的 XSLT 1.0 解决方案:无法使用 XSLT 是一件痛苦的事情2.0...看多简单!
注意:XPath 2.0 序列具有隐式的构造顺序。由于 XSLT 2.0
xsl:value-of
指令句柄序列,现在有一个@separator
。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
.可能这会很有帮助:
应用于此 XML:
结果将是:
对于此 XML:
结果将是:
May be this can be helful:
Applied to this XML:
Result will be:
And to this XML:
Result will be: