Xpath 表达式给出多个值 [英] Xpath expression gives multiple values

查看:49
本文介绍了Xpath 表达式给出多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要匹配值的要求

Buyer,BilltoParty,Shipto 可以取值 AGIIS-EBID 或 EAN 或 GLN

我使用 XPath1.0 来提取这些值并使用条件.

I am using XPath1.0 to extract these values and use the condition.

我能够为买方获得正确的价值*

我面临的问题是 Shipto 和 Billtoparty

The problem I am facing is for Shipto and Billtoparty

Xpath 为 Shipto 和 Billto 获取价值

Xpath for getting value for Shipto and Billto

//*[local-name()='ShipNotice']/*[local-name()='ShipNoticeBody']/*[local-name()='ShipNoticePartners']/*[local-name()='OtherPartner']/*[local-name()='PartnerInformation']/*[local-name()='PartnerIdentifier']/@*[local-name()='Agency']

给出的值为:

Agency AGIIS-EBID
Agency SCAC
Agency AGIIS-EBID
Agency AGIIS-EBID

INPUT XML 更新了一个

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ces:names:specification:ces:schema:all:4:0">
    <soapenv:Header/>
    <soapenv:Body>
        <ces:ShipNotice Version="4.0" xmlns:ces="urn:ces:names:specification:ces:schema:all:4:0">
            <ces:Header>
                <ces:From>
                </ces:From>
                <ces:To>
                </ces:To>
            </ces:Header>
            <ces:ShipNoticeBody>
                <ces:ShipNoticePartners>
                    <ces:Buyer>
                        <ces:PartnerInformation>
                            <ces:PartnerIdentifier Agency="AGIIS-EBID">8049915600000</ces:PartnerIdentifier>
                        </ces:PartnerInformation>
                    </ces:Buyer>
                    <ces:OtherPartner PartnerRole="ShipFrom">
                        <ces:PartnerInformation>
                            <ces:PartnerIdentifier Agency="AGIIS-EBID">0447026370000</ces:PartnerIdentifier>
                        </ces:PartnerInformation>
                    </ces:OtherPartner>
                    <ces:OtherPartner PartnerRole="Carrier">
                        <ces:PartnerInformation>
                            <ces:PartnerIdentifier Agency="SCAC">B935</ces:PartnerIdentifier>
                        </ces:PartnerInformation>
                    </ces:OtherPartner>
                    <ces:OtherPartner PartnerRole="ShipTo">
                        <ces:PartnerInformation>
                            <ces:PartnerIdentifier Agency="AGIIS-EBID">8049915600000</ces:PartnerIdentifier>
                        </ces:PartnerInformation>
                    </ces:OtherPartner>
                    <ces:OtherPartner PartnerRole="BillToParty">
                        <ces:PartnerInformation>
                            <ces:PartnerIdentifier Agency="AGIIS-EBID">1024122440000</ces:PartnerIdentifier>
                        </ces:PartnerInformation>
                    </ces:OtherPartner>
                </ces:ShipNoticePartners>
                <ces:ShipNoticeDetails>
                </ces:ShipNoticeDetails>
            </ces:ShipNoticeBody>
        </ces:ShipNotice>
    </soapenv:Body>
</soapenv:Envelope>

下面是我正在使用的代码.

Below is the code I am using.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dpconfig="http://www.datapower.com/param/config" xmlns:cidx="urn:cidx:names:specification:ces:schema:all:4:0" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp">
    <xsl:param name="dpconfig:AGIIS-EBID" select=" 'AGIIS-EBID' "/>
    <xsl:param name="dpconfig:EAN" select="'EAN'"/>
    <xsl:param name="dpconfig:GLN" select="'GLN'"/>
    <xsl:template match="/">
        <xsl:copy-of select="."/>
        <xsl:variable name="Buyer" select="//*[local-name()='ShipNotice']/*[local-name()='ShipNoticeBody']/*[local-name()='ShipNoticePartners']/*[local-name()='Buyer']/*[local-name()='PartnerInformation']/*[local-name()='PartnerIdentifier']/@*[local-name()='Agency']"/>
        <xsl:variable name="Shipto" select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ShipNotice']/*[local-name()='ShipNoticeBody']/*[local-name()='ShipNoticePartners']/*[local-name()='OtherPartner']/*[local-name()='PartnerInformation']/*[local-name()='PartnerIdentifier']/@*[local-name()='Agency']"/>
        <xsl:variable name="Billto" select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ShipNotice']/*[local-name()='ShipNoticeBody']/*[local-name()='ShipNoticePartners']/*[local-name()='OtherPartner']/*[local-name()='PartnerInformation']/*[local-name()='PartnerIdentifier']/@*[local-name()='Agency']"/>
        <xsl:choose>
            <xsl:when test="$Buyer='AGIIS-EBID' and  $Shipto='AGIIS-EBID' and $Billto='AGIIS-EBID' or $Buyer='EAN' and  $Shipto='EAN' and $Billto='EAN' or $Buyer='GLN' and  $Shipto='GLN' and $Billto='GLN'">
            </xsl:when>
            <xsl:otherwise>
                    Partner didn't match the AGIIS-EBID or EAN or GLN
                </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

谁能在 Xpath 上帮助我,并让我知道我进行条件匹配的方式是否正确?

Can anyone please help me on Xpath and also let me know the way I am doing the condition match is correct or not?

期待输出

我期望变量 Buyer、Shipto、Billto 应该与值 == AGIIS-EBID 匹配,但是我使用的 Xpath 给出了

I am expecting the variables Buyer, Shipto, Billto should match the value == AGIIS-EBID, but the Xpath which I am using gives

Agency AGIIS-EBID
Agency SCAC
Agency AGIIS-EBID
Agency AGIIS-EBID

推荐答案

通过查看您的 XSLT,并看到您的 ShiptoBillto 变量具有相同的声明,我认为您的意思是这些当前匹配四个节点(或者更确切地说是属性),但您只想要一个.

From looking at your XSLT, and seeing that your Shipto and Billto variables have the same declaration, I think what you are saying is that these currently match four nodes (or rather attributes), but you only want one.

具体来说,从查看您的 XML 来看,关键信息似乎位于 OtherPartner 节点

Specifically, from looking at your XML, it looks like the crucial information is on the OtherPartner node

<ces:OtherPartner PartnerRole="ShipTo">

<ces:OtherPartner PartnerRole="BillToParty">

这意味着您需要在匹配 OtherPartner 的当前条件之后添加一个额外条件.例如,对于 ShipTo,条件是这样的:

This means you need to add an extra condition after the current condition that matches OtherPartner. For example, for ShipTo the condition would be this:

    <xsl:variable name="Shipto" select="
         /*[local-name()='Envelope']
         /*[local-name()='Body']
         /*[local-name()='ShipNotice']
         /*[local-name()='ShipNoticeBody']
         /*[local-name()='ShipNoticePartners']
         /*[local-name()='OtherPartner'][@PartnerRole='ShipTo']
         /*[local-name()='PartnerInformation']
         /*[local-name()='PartnerIdentifier']
         /@*[local-name()='Agency']"/>

当然,正如注释中所指出的,如果您知道 XML 中的命名空间 uri,则最好在 XSLT 中声明它们

Of course, as pointed out in comments, if you know the namespace uri in the XML, it is preferable to declare them in the XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:dpconfig="http://www.datapower.com/param/config" 
  xmlns:cidx="urn:cidx:names:specification:ces:schema:all:4:0" 
  xmlns:dp="http://www.datapower.com/extensions"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:ces="urn:ces:names:specification:ces:schema:all:4:0" extension-element-prefixes="dp">

然后你可以简化"你的变量,例如

Then you can "simplify" your variable to this for example

<xsl:variable name="Shipto" select="
        soapenv:Envelope
        /soapenv:Body
        /ces:ShipNotice
        /ces:ShipNoticeBody
        /ces:ShipNoticePartners
        /ces:OtherPartner[@PartnerRole='ShipTo']
        /ces:PartnerInformation
        /ces:PartnerIdentifier/@Agency"/>

与您的 Billto 变量类似.只需更改 @PartnerRole 属性的条件即可.

And similary for your Billto variable. Just change the condition on the @PartnerRole attribute.

此外,您可能需要考虑在最终条件周围加上一些括号

Additionally, you might want to consider putting some parentheses around you final condition

<xsl:when test="
     ($Buyer='AGIIS-EBID' and  $Shipto='AGIIS-EBID' and $Billto='AGIIS-EBID') 
     or ($Buyer='EAN' and  $Shipto='EAN' and $Billto='EAN') 
     or ($Buyer='GLN' and  $Shipto='GLN' and $Billto='GLN')">

或者最好还是这样做...

Or better still do this...

<xsl:when test="($Buyer='AGIIS-EBID' or $Buyer='EAN' or $Buyer='GLN') 
                 and $Shipto=$Buyer 
                 and $Billto=$Buyer">

这篇关于Xpath 表达式给出多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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