难以循环应用partNumber XSLT,如下所示

时间:2019-04-10 00:17:08

标签: xslt-2.0

下面给出的示例SOAP输入XML:

<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: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:Description>PAD SET, RR.</b:Description>                            
                            <b:PartNumber>31500SB2100M</b:PartNumber>
                            <b:PartNumberFormatted>31500-SB2-100M</b:PartNumberFormatted> 
                        </a:Attributes>                 
                      </a:CustomerPart>
                      <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:Description>Kite  SET, RR.</b:Description>                           
                            <b:PartNumber>60211T7J305ZZ</b:PartNumber>                            
                        </a:Attributes> 
                       </a:CustomerPart>
                  </PullCustomerPartsPricingResult>
               </PullCustomerPartsPricingResponse>
             </s:Body>
  </s:Envelope>

正在应用XSLT代码,如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
    <root xmlns="http://www.dataprint.com/global/3.0/rest/">
       <xsl:for-each  select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='PullCustomerPartsPricingResponse']/*[local-name()='PullCustomerPartsPricingResult']/*[local-name()='CustomerPart']">

                    <partDetail>    
                      <partNumber>
                                <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']" />
                       </partNumber>        
                      <partDescription>
                         <xsl:value-of select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='PullCustomerPartsPricingResponse']/*[local-name()='PullCustomerPartsPricingResult']/*[local-name()='CustomerPart']/*[local-name()='Attributes'] /*[local-name()='Description']" />
                      </partDescription>
                    </partDetail>
       </xsl:for-each>
     </root>

</xsl:template>
</xsl:stylesheet> 

我当前的样本输出如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.dataprint.com/global/3.0/rest/">
   <results>
      <partDetail>
         <partNumber>31500SB2100M</partNumber>
         <partDescription>PAD SET, RR.</partDescription>
      </partDetail>
   </results>
   <results>
      <partDetail>
         <partNumber>31500SB2100M</partNumber>
         <partDescription>PAD SET, RR.</partDescription>
      </partDetail>
   </results>
</root>

我的期望输出如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.dataprint.com/global/3.0/rest/">
   <results>         
      <partDetail>
         <partNumber>31500SB2100M</partNumber>
         <partDescription>PAD SET, RR.</partDescription>
      </partDetail>
   </results>
   <results>
      <partDetail>
         <partNumber>60211T7J305ZZ</partNumber>
         <partDescription>Kite  SET, RR.</partDescription>
      </partDetail>
   </results>
</root>

我正在尝试循环元素“ a:CustomerPart”以打印PartNumber和b:Descriptionas输出,但是首先a:CustomerPart数据被重复两次。我不知道是否需要代码更改,请对此提供帮助。 EiríkrÚtlendi非常感谢您的耐心/建议

1 个答案:

答案 0 :(得分:1)

xsl:for-each内,选定的CustomerPart项成为上下文项。然后,您应该使用从此上下文项开始的相对路径,而不是从文档根目录(CustomerPart开始的绝对路径)来选择此/的子代/后代。

也就是说,您应该替换

    <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:value-of select="*[local-name()='Attributes'] /*[local-name()='PartNumber']" />

或更佳,

<xsl:value-of select="*:Attributes/*:PartNumber" />