Сложность доступа к значению атрибута (Xpath) XSLT - PullRequest
0 голосов
/ 31 января 2019

Мне не удалось получить доступ к значению PartNumber / Description в теге атрибута с указанным ниже xpath, но ниже указан файл XML. Мы дадим вам дополнительную информацию.

Используемый Xpath и XML-сообщение SOAP:

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

С уважением, Vardhan

Ответы [ 2 ]

0 голосов
/ 07 февраля 2019

Пространства имен из 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>
0 голосов
/ 31 января 2019

Вы закончили свое длинное выражение 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.

...