XSLT 验证和连接多个变量

发布于 2024-10-16 13:11:23 字数 1156 浏览 2 评论 0原文

我必须在验证多个变量后验证并连接它们。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南七夏 2024-10-23 13:11:23

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

在 XSLT 1.0 中,假设您按文档顺序输出结果:

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

如果不在文档中顺序,那么就像 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:

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

只是一片海 2024-10-23 13:11:23

XSLT 有一个函数

concat(string, string, ...)

,您可以在另一个变量的 select= 属性中

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

或任何允许属性值模板(在花括号中)的地方使用该函数。

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

一瞬间的火花 2024-10-23 13:11:23

保留表达式顺序(xsl:sort 指令)并使用 Becker 方法(substring() 第二个参数)的 XSLT 1.0 解决方案:

<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=", "/>

注意: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):

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

本宫微胖 2024-10-23 13:11:23

可能这会很有帮助:

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

应用于此 XML:

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

结果将是:

test, test2, test3, test4, test5

对于此 XML:

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

结果将是:

test2, test4

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文