【问题标题】:Difficulty in Accessing Attribute value (Xpath) XSLT难以访问属性值 (Xpath) XSLT
【发布时间】:2019-01-31 09:33:40
【问题描述】:

我无法使用下面的 xpath 访问属性标记下的 PartNumber/Description 值,但下面给出了soap xml。让我知道需要的更多详细信息。

请对此提供帮助。谢谢!

正在使用 Xpath 和 SOAP XML 消息:

<xsl:value-of select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='PullCustomerPartsPricingResponse']/*[local-name()='PullCustomerPartsPricingResult']/*[local-name()='CustomerPart']/@*[local-name()='PartNumber']"/> 


<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
   <s:Header />
   <s:Body>
      <PullCustomerPartsPricingResponse xmlns="http://cdx.dealerbuilt.com/Api/0.99/">
         <PullCustomerPartsPricingResult xmlns:a="http://schemas.datacontract.org/2004/07/DealerBuilt.BaseApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:CustomerPart>
               <a:Placement>
                  <a:GroupId>10</a:GroupId>
               </a:Placement>
               <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/DealerBuilt.Models.Parts">
                  <b:AddedDate>2017-12-19T00:00:00</b:AddedDate>
                  <b:DealerPartId>287925</b:DealerPartId>
                  <b:Description>BAT (51/500AMP85)</b:Description>
                  <b:PartNumber>31500SB2yy1M</b:PartNumber>
                  <b:QuantityLostMonthToDate>0</b:QuantityLostMonthToDate>
               </a:Attributes>
               <a:PartKey>GZ287925</a:PartKey>
               <a:CustomerListPrice xmlns:b="http://schemas.datacontract.org/2004/07/DealerBuilt.Models">
                  <b:Amount>130.49</b:Amount>
                  <b:Currency>UsDollar</b:Currency>
               </a:CustomerListPrice>
            </a:CustomerPart>
         </PullCustomerPartsPricingResult>
      </PullCustomerPartsPricingResponse>
   </s:Body>
</s:Envelope>

问候 瓦尔丹

【问题讨论】:

标签: xslt xpath ibm-datapower


【解决方案1】:

你已经用这个结束了你的长 xpath 表达式..

/@*[local-name()='PartNumber']

PartNumber 不是属性。它是一个名为 PartNumber 的元素,它是恰好名为 Attributes 的元素的子元素,但这实际上并没有使它成为 XML 术语中的属性!

它应该看起来像这样......

<xsl:value-of select="/*[local-name()='Envelope']
                        /*[local-name()='Body']
                        /*[local-name()='PullCustomerPartsPricingResponse']
                        /*[local-name()='PullCustomerPartsPricingResult']
                        /*[local-name()='CustomerPart']
                        /*[local-name()='Attributes']
                        /*[local-name()='PartNumber']"/> 

虽然如果你在 XSLT 中声明命名空间然后在你的 xpath 中使用会更好。

【讨论】:

    【解决方案2】:

    来自请求 XML 的名称空间也需要在 xslt 中使用。以下 xsl 给出了 PartNumber 值。

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:a="http://schemas.datacontract.org/2004/07/DealerBuilt.BaseApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://schemas.datacontract.org/2004/07/DealerBuilt.Models.Parts">
        <xsl:output method="xml" version="1.0"/>
        <xsl:template match="/">
            <xsl:value-of select="*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='PullCustomerPartsPricingResponse']/*[local-name()='PullCustomerPartsPricingResult']/*[local-name()='CustomerPart']/*[local-name()='Attributes']/*[local-name()='PartNumber']"/>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多