比较 XSLT 中的两个元素(字符串类型)? [英] To compare two elements(string type) in XSLT?

查看:25
本文介绍了比较 XSLT 中的两个元素(字符串类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的新手,有人可以建议我如何将来自 xml 的两个元素作为字符串进行比较,它们的值是:

视觉运营--XML 代码<OU_ADDR1>第五大道90号</OU_ADDR1>--XML 代码<xsl:when test="OU_NAME='OU_ADDR1'">--比较来自 XML 的两个元素<!--remove if adrees 已经包含操作单元名称<xsl:value-of select="OU_NAME"/><fo:block/>--><xsl:if test="OU_ADDR1 !='' "><xsl:value-of select="OU_ADDR1"/><fo:block/></xsl:if><xsl:if test="LE_ADDR2 !='' "><xsl:value-of select="OU_ADDR2"/><fo:block/></xsl:if><xsl:if test="LE_ADDR3 !='' "><xsl:value-of select="OU_ADDR3"/><fo:block/></xsl:if><xsl:if test="OU_TOWN_CITY !=''"><xsl:value-of select="OU_TOWN_CITY"/>,<fo:leader leader-pattern="space" leader-length="2.0pt"/></xsl:if><xsl:value-of select="OU_REGION2"/><fo:leader leader-pattern="space" leader-length="3.0pt"/><xsl:value-of select="OU_POSTALCODE"/><fo:block/><xsl:value-of select="OU_COUNTRY"/></xsl:when><xsl:否则><xsl:value-of select="OU_NAME"/><fo:block/><xsl:if test="OU_ADDR1 !='' "><xsl:value-of select="OU_ADDR1"/><fo:block/></xsl:if><xsl:if test="LE_ADDR2 !='' "><xsl:value-of select="OU_ADDR2"/><fo:block/></xsl:if><xsl:if test="LE_ADDR3 !='' "><xsl:value-of select="OU_ADDR3"/><fo:block/></xsl:if><xsl:if test="OU_TOWN_CITY !=''"><xsl:value-of select="OU_TOWN_CITY"/>,<fo:leader leader-pattern="space" leader-length="2.0pt"/></xsl:if><xsl:value-of select="OU_REGION2"/><fo:leader leader-pattern="space" leader-length="3.0pt"/><xsl:value-of select="OU_POSTALCODE"/><fo:block/><xsl:value-of select="OU_COUNTRY"/></xsl:否则></xsl:选择>

解决方案

首先提供的长代码:

 <xsl:when test="OU_NAME='OU_ADDR1'">--比较来自 XML 的两个元素<!--remove if adrees 已经包含操作单元名称 <xsl:value-of select="OU_NAME"/><fo:block/>--><xsl:if test="OU_ADDR1 !='' "><xsl:value-of select="OU_ADDR1"/><fo:block/></xsl:if><xsl:if test="LE_ADDR2 !='' "><xsl:value-of select="OU_ADDR2"/><fo:block/></xsl:if><xsl:if test="LE_ADDR3 !='' "><xsl:value-of select="OU_ADDR3"/><fo:block/></xsl:if><xsl:if test="OU_TOWN_CITY !=''"><xsl:value-of select="OU_TOWN_CITY"/>,<fo:leader leader-pattern="space" leader-length="2.0pt"/></xsl:if><xsl:value-of select="OU_REGION2"/><fo:leader leader-pattern="space" leader-length="3.0pt"/><xsl:value-of select="OU_POSTALCODE"/><fo:block/><xsl:value-of select="OU_COUNTRY"/></xsl:when><xsl:否则><xsl:value-of select="OU_NAME"/><fo:block/><xsl:if test="OU_ADDR1 !='' "><xsl:value-of select="OU_ADDR1"/><fo:block/></xsl:if><xsl:if test="LE_ADDR2 !='' "><xsl:value-of select="OU_ADDR2"/><fo:block/></xsl:if><xsl:if test="LE_ADDR3 !='' "><xsl:value-of select="OU_ADDR3"/><fo:block/></xsl:if><xsl:if test="OU_TOWN_CITY !=''"><xsl:value-of select="OU_TOWN_CITY"/>,<fo:leader leader-pattern="space" leader-length="2.0pt"/></xsl:if><xsl:value-of select="OU_REGION2"/><fo:leader leader-pattern="space" leader-length="3.0pt"/><xsl:value-of select="OU_POSTALCODE"/><fo:block/><xsl:value-of select="OU_COUNTRY"/></xsl:否则></xsl:选择>

相当于这个,代码更短:

<xsl:value-of select="OU_NAME"/></xsl:if><xsl:if test="OU_ADDR1 !='' "><xsl:value-of select="OU_ADDR1"/><fo:block/></xsl:if><xsl:if test="LE_ADDR2 !='' "><xsl:value-of select="OU_ADDR2"/><fo:block/></xsl:if><xsl:if test="LE_ADDR3 !='' "><xsl:value-of select="OU_ADDR3"/><fo:block/></xsl:if><xsl:if test="OU_TOWN_CITY !=''"><xsl:value-of select="OU_TOWN_CITY"/>,<fo:leader leader-pattern="space" leader-length="2.0pt"/></xsl:if><xsl:value-of select="OU_REGION2"/><fo:leader leader-pattern="space" leader-length="3.0pt"/><xsl:value-of select="OU_POSTALCODE"/><fo:block/><xsl:value-of select="OU_COUNTRY"/>

现在,回答您的问题:

<块引用>

如何比较两个即将到来的元素从 xml 作为字符串

在 Xpath 1.0 中,字符串只能比较相等(或不等),使用运算符 = 和函数 not() 以及运算符 =.

$str1 = $str2

在字符串 $str1 等于字符串 $str2 时准确地计算为 true().

not($str1 = $str2)

在字符串 $str1 不等于字符串 $str2 时准确地计算为 true().

还有 != 运算符.通常应该避免它,因为只要它的一个操作数是节点集,它就会有异常行为.

现在,比较两个元素节点的规则是相似的:

$el1 = $el2

$el1 的字符串值等于 $el2 的字符串值时,精确地计算为 true().

not($el1 = $el2)

$el1 的字符串值不等于 $el2 的字符串值时,精确地计算为 true().>

然而,如果 = 的操作数之一是节点集,则

 $ns = $str

恰好在节点集 $ns1 中至少有一个节点,其字符串值等于字符串 $ 时,计算为 true()str

$ns1 = $ns2

恰好在节点集 $ns1 中至少有一个节点,其字符串值等于某个节点的字符串值时,计算为 true()来自 $ns2

因此,表达式:

OU_NAME='OU_ADDR1'

仅当当前节点至少有一个名为 OU_NAME 且其字符串值为字符串 'OU_ADDR1' 的子元素时,才计算为 true().

这显然不是你想要的!

很可能是您想要的:

OU_NAME=OU_ADDR1

此表达式的计算结果为 true 正好有当前节点的至少一个 OU_NAME 子节点和当前节点的一个 OU_ADDR1 子节点相同的字符串值.

最后,在 XPath 2.0 中,还可以使用值比较运算符 ltleeqgtge 和继承自 XPath 1.0 的通用比较运算符 =.

当一个或两个参数是一个以上项目的序列时,尝试对值比较运算符求值会导致错误.

i am new to XSLT ,can any one please suggest to me how to compare two elements coming from xml as string their values are:

<OU_NAME>Vision Operations</OU_NAME> --XML code
<OU_ADDR1>90 Fifth Avenue</OU_ADDR1> --XML code

<xsl:choose>
    <xsl:when test="OU_NAME='OU_ADDR1'"> --comparing two elements coming from XML
        <!--remove if  adrees already contain  operating unit name
            <xsl:value-of select="OU_NAME"/> <fo:block/>-->
        <xsl:if test="OU_ADDR1 !='' ">
            <xsl:value-of select="OU_ADDR1"/>
            <fo:block/>
        </xsl:if>
        <xsl:if test="LE_ADDR2 !='' ">
            <xsl:value-of select="OU_ADDR2"/>
            <fo:block/>
        </xsl:if>
        <xsl:if test="LE_ADDR3 !='' ">
            <xsl:value-of select="OU_ADDR3"/>
            <fo:block/>
        </xsl:if>
        <xsl:if test="OU_TOWN_CITY !=''">
            <xsl:value-of select="OU_TOWN_CITY"/>,
            <fo:leader leader-pattern="space" leader-length="2.0pt"/>
        </xsl:if>
        <xsl:value-of select="OU_REGION2"/>
        <fo:leader leader-pattern="space" leader-length="3.0pt"/>
        <xsl:value-of select="OU_POSTALCODE"/>
        <fo:block/>
        <xsl:value-of select="OU_COUNTRY"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="OU_NAME"/>
        <fo:block/>
        <xsl:if test="OU_ADDR1 !='' ">
            <xsl:value-of select="OU_ADDR1"/>
            <fo:block/>
        </xsl:if>
        <xsl:if test="LE_ADDR2 !='' ">
            <xsl:value-of select="OU_ADDR2"/>
            <fo:block/>
        </xsl:if>
        <xsl:if test="LE_ADDR3 !='' ">
            <xsl:value-of select="OU_ADDR3"/>
            <fo:block/>
        </xsl:if>
        <xsl:if test="OU_TOWN_CITY !=''">
            <xsl:value-of select="OU_TOWN_CITY"/>,
            <fo:leader leader-pattern="space" leader-length="2.0pt"/>
        </xsl:if>
        <xsl:value-of select="OU_REGION2"/>
        <fo:leader leader-pattern="space" leader-length="3.0pt"/>
        <xsl:value-of select="OU_POSTALCODE"/>
        <fo:block/>
        <xsl:value-of select="OU_COUNTRY"/>
    </xsl:otherwise>
</xsl:choose>

解决方案

First of all, the provided long code:

    <xsl:choose>
        <xsl:when test="OU_NAME='OU_ADDR1'">   --comparing two elements coming from XML             
            <!--remove if  adrees already contain  operating unit name <xsl:value-of select="OU_NAME"/> <fo:block/>-->
            <xsl:if test="OU_ADDR1 !='' ">
                <xsl:value-of select="OU_ADDR1"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="LE_ADDR2 !='' ">
                <xsl:value-of select="OU_ADDR2"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="LE_ADDR3 !='' ">
                <xsl:value-of select="OU_ADDR3"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="OU_TOWN_CITY !=''">
                <xsl:value-of select="OU_TOWN_CITY"/>,
                <fo:leader leader-pattern="space" leader-length="2.0pt"/>
            </xsl:if>
            <xsl:value-of select="OU_REGION2"/>
            <fo:leader leader-pattern="space" leader-length="3.0pt"/>
            <xsl:value-of select="OU_POSTALCODE"/>
            <fo:block/>
            <xsl:value-of select="OU_COUNTRY"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="OU_NAME"/>
            <fo:block/>
            <xsl:if test="OU_ADDR1 !='' ">
                <xsl:value-of select="OU_ADDR1"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="LE_ADDR2 !='' ">
                <xsl:value-of select="OU_ADDR2"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="LE_ADDR3 !='' ">
                <xsl:value-of select="OU_ADDR3"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="OU_TOWN_CITY !=''">
                <xsl:value-of select="OU_TOWN_CITY"/>,
                <fo:leader leader-pattern="space" leader-length="2.0pt"/>
            </xsl:if>
            <xsl:value-of select="OU_REGION2"/>
            <fo:leader leader-pattern="space" leader-length="3.0pt"/>
            <xsl:value-of select="OU_POSTALCODE"/>
            <fo:block/>
            <xsl:value-of select="OU_COUNTRY"/>
        </xsl:otherwise>
    </xsl:choose>

is equivalent to this, much shorter code:

<xsl:if test="not(OU_NAME='OU_ADDR1)'">
              <xsl:value-of select="OU_NAME"/>
        </xsl:if>
            <xsl:if test="OU_ADDR1 !='' ">
                <xsl:value-of select="OU_ADDR1"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="LE_ADDR2 !='' ">
                <xsl:value-of select="OU_ADDR2"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="LE_ADDR3 !='' ">
                <xsl:value-of select="OU_ADDR3"/>
                <fo:block/>
            </xsl:if>
            <xsl:if test="OU_TOWN_CITY !=''">
                <xsl:value-of select="OU_TOWN_CITY"/>,
                <fo:leader leader-pattern="space" leader-length="2.0pt"/>
            </xsl:if>
            <xsl:value-of select="OU_REGION2"/>
            <fo:leader leader-pattern="space" leader-length="3.0pt"/>
            <xsl:value-of select="OU_POSTALCODE"/>
            <fo:block/>
            <xsl:value-of select="OU_COUNTRY"/>

Now, to your question:

how to compare two elements coming from xml as string

In Xpath 1.0 strings can be compared only for equality (or inequality), using the operator = and the function not() together with the operator =.

$str1 = $str2

evaluates to true() exactly when the string $str1 is equal to the string $str2.

not($str1 = $str2)

evaluates to true() exactly when the string $str1 is not equal to the string $str2.

There is also the != operator. It generally should be avoided because it has anomalous behavior whenever one of its operands is a node-set.

Now, the rules for comparing two element nodes are similar:

$el1 = $el2

evaluates to true() exactly when the string value of $el1 is equal to the string value of $el2.

not($el1 = $el2)

evaluates to true() exactly when the string value of $el1 is not equal to the string value of $el2.

However, if one of the operands of = is a node-set, then

 $ns = $str

evaluates to true() exactly when there is at least one node in the node-set $ns1, whose string value is equal to the string $str

$ns1 = $ns2

evaluates to true() exactly when there is at least one node in the node-set $ns1, whose string value is equal to the string value of some node from $ns2

Therefore, the expression:

OU_NAME='OU_ADDR1'

evaluates to true() only when there is at least one element child of the current node that is named OU_NAME and whose string value is the string 'OU_ADDR1'.

This is obviously not what you want!

Most probably you want:

OU_NAME=OU_ADDR1

This expression evaluates to true exactly there is at least one OU_NAME child of the current node and one OU_ADDR1 child of the current node with the same string value.

Finally, in XPath 2.0, strings can be compared also using the value comparison operators lt, le, eq, gt, ge and the inherited from XPath 1.0 general comparison operator =.

Trying to evaluate a value comparison operator when one or both of its arguments is a sequence of more than one item results in error.

这篇关于比较 XSLT 中的两个元素(字符串类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆