XSLT - 转换无法正常工作

时间:2013-02-27 08:29:42

标签: xslt xslt-1.0 xslt-2.0

我有一个如下的请求XML,需要将其转换为不同的格式。请问一些pne请帮助我。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:fetchOrderListResponse xmlns:ns2="http://impl.lob.wipro.com/">
         <return>
            <customerOrderNumber>1</customerOrderNumber>
            <orderId>E147011D-AE9F-4AC8-AF37-05DC538AF1D0</orderId>
            <ordertotal>0</ordertotal>
         </return>
         <return>
            <customerOrderNumber>1</customerOrderNumber>
            <orderDetails>
               <itemPrice>50.0</itemPrice>
               <itemQty>2</itemQty>
               <itemUnit>0</itemUnit>
               <orderDetailsId>37516016-D71B-4790-951F-55D00B0CC159</orderDetailsId>
            </orderDetails>
            <orderDetails>
               <itemPrice>39.0</itemPrice>
               <itemQty>3</itemQty>
               <itemUnit>0</itemUnit>
               <orderDetailsId>6095ABC7-0D0D-4B2E-92E5-80F24E9092B8</orderDetailsId>
            </orderDetails>
            <orderId>84EC371D-40CA-455E-A0FA-7EA733E9BFD3</orderId>
            <ordertotal>217</ordertotal>
         </return>
         <return>
            <customerOrderNumber>1</customerOrderNumber>
            <orderId>54712493-2172-4ADB-814B-BC7AA0BB72C3</orderId>
            <ordertotal>0</ordertotal>
         </return>
         <return>
            <customerOrderNumber>1</customerOrderNumber>
            <deliverydate>2013-02-06T00:00:00+05:30</deliverydate>
            <orderDetails>
               <itemPrice>565.0</itemPrice>
               <itemQty>1</itemQty>
               <itemUnit>0</itemUnit>
               <orderDetailsId>9A5030BE-F95F-4C62-B5A2-41FF85423218</orderDetailsId>
            </orderDetails>
            <orderDetails>
               <itemPrice>4.0</itemPrice>
               <itemQty>90</itemQty>
               <itemUnit>0</itemUnit>
               <orderDetailsId>65A8B3BE-D407-43D8-8754-EA1E26AA56E4</orderDetailsId>
            </orderDetails>
            <orderId>0BDCB222-0117-47A9-8813-DF03A1D19E5E</orderId>
            <ordertotal>925</ordertotal>
         </return>
         <return>
            <customerOrderNumber>1</customerOrderNumber>
            <orderId>8E4220DC-884B-47BC-A565-E26B80BA5249</orderId>
            <ordertotal>0</ordertotal>
         </return>
         <return>
            <customerOrderNumber>1</customerOrderNumber>
            <deliverydate>2013-02-06T00:00:00+05:30</deliverydate>
            <orderDetails>
               <itemPrice>10.0</itemPrice>
               <itemQty>4</itemQty>
               <itemUnit>0</itemUnit>
               <orderDetailsId>5A2DF895-BB0F-4039-80DB-F44CED31697B</orderDetailsId>
            </orderDetails>
            <orderDetails>
               <itemPrice>20.0</itemPrice>
               <itemQty>3</itemQty>
               <itemUnit>0</itemUnit>
               <orderDetailsId>8034FBF4-B573-4B19-BDF5-FAF6C4247A55</orderDetailsId>
            </orderDetails>
            <orderId>60161E3E-3C4A-4CE6-AAC3-E4D2BC240046</orderId>
            <ordertotal>100</ordertotal>
         </return>
      </ns2:fetchOrderListResponse>
   </soap:Body>
</soap:Envelope>

响应应采用以下格式。请帮助XSLT转换相同的

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.client.inmemory.eim.wipro.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getOrderDetailResponse>
         <!--Zero or more repetitions:-->
         <return>
            <itemPrice>?</itemPrice>
            <itemQty>?</itemQty>
            <itemUnit>?</itemUnit>
            <!--Optional:-->
            <orderDetailsId>?</orderDetailsId>
            <!--Optional:-->
            <orderId>?</orderId>
         </return>
      </ser:getOrderDetailResponse>
   </soapenv:Body>
</soapenv:Envelope>

我想出了一个XSLT来做到这一点。不幸的是它没有工作,也没有提取任何数据。请让我知道我做错了什么

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.client.inmemory.eim.wipro.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getOrderDetailResponse>
         <!--Zero or more repetitions:-->
         <return>
<xsl:for-each select="return/orderDetails">
            <itemPrice><xsl:value-of select="return/orderDetails/itemPrice"/></itemPrice>
            <itemQty><xsl:value-of select="return/orderDetails/itemQty"/></itemQty>
            <itemUnit><xsl:value-of select="return/orderDetails/itemUnit"/></itemUnit>
            <orderDetailsId><xsl:value-of select="return/orderDetails/orderDetailsId"/></orderDetailsId>
            <orderId><xsl:value-of select="orderId"/></orderId>
</xsl:for-each>
         </return>
      </ser:getOrderDetailResponse>
   </soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

不确定为什么你删除了第一个问题并再次发布完全相同的问题。在添加XSLT之后,这是一个值得回答的问题。

无论如何,问题是您的模板与/匹配,但是您尝试从xsl:for-each开始return/orderDetails。你真的必须注意XSLT中的上下文。

首先将适当的名称空间添加到xsl:stylesheet ...

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns2="http://impl.lob.wipro.com/"

其次,相应地调整select

<xsl:for-each select="/soap:Envelope/soap:Body/ns2:fetchOrderListResponse/return/orderDetails">

我不确定这是否会返回您正在寻找的内容,但它会返回数据。看到输出后,您应该能够进行调整