2017-10-28 221 views
0

我有一個XML,它帶有作爲子項屬性嵌入的itemid。我應該根據ItemID的值對XML進行排序。下面是XML根據孩子的屬性值對父項進行排序

<MultiApi TransIdKey="e5d6bd63-88cd-455f-8ab3-9510b5edb2b7" OrderId="" SoId=""> 
    <API FlowName="Reservation"> 
     <Input> 
      <ReserveItemInventory CheckInventory="Y" DemandType="RESERVED" ItemID="19998548" OrganizationCode="" QtyToBeCancelled="0" QtyToBeReserved="1" ReservationID="1000000000003" ShipNode="DC-W" UnitOfMeasure="EACH" xmlns="http://www.sterlingcommerce.com/documentation/YFS/reserveItemInventory/input"/> 
     </Input> 
    </API> 
    <API FlowName="Reservation"> 
     <Input> 
      <ReserveItemInventory CheckInventory="Y" DemandType="RESERVED" ItemID="19998546" OrganizationCode="" QtyToBeCancelled="0" QtyToBeReserved="1" ReservationID="1000000000003" ShipNode="DC-W" UnitOfMeasure="EACH" xmlns="http://www.sterlingcommerce.com/documentation/YFS/reserveItemInventory/input"/> 
     </Input> 
    </API> 
</MultiApi> 

我有以下XSLT,但失敗以來的xmlns屬性的值有「輸入」結尾。如果我刪除下面的XSL xmlns屬性按預期工作。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="MultiApi"> 
    <xsl:copy> 
     <xsl:apply-templates select="API"> 
      <xsl:sort select="Input/ReserveItemInventory/@ItemID" data-type="number"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

我想要的輸出可考慮如下

<MultiApi TransIdKey="e5d6bd63-88cd-455f-8ab3-9510b5edb2b7" OrderId="" SoId=""> 
<API FlowName="Reservation"> 
    <Input> 
     <ReserveItemInventory CheckInventory="Y" DemandType="RESERVED" ItemID="19998546" OrganizationCode="" QtyToBeCancelled="0" QtyToBeReserved="1" ReservationID="1000000000003" ShipNode="DC-W" UnitOfMeasure="EACH" xmlns="http://www.sterlingcommerce.com/documentation/YFS/reserveItemInventory/input"/> 
    </Input> 
</API> 
<API FlowName="Reservation"> 
    <Input> 
     <ReserveItemInventory CheckInventory="Y" DemandType="RESERVED" ItemID="19998548" OrganizationCode="" QtyToBeCancelled="0" QtyToBeReserved="1" ReservationID="1000000000003" ShipNode="DC-W" UnitOfMeasure="EACH" xmlns="http://www.sterlingcommerce.com/documentation/YFS/reserveItemInventory/input"/> 
    </Input> 
</API> 

回答

0

你的排序失敗,因爲ReserveItemInventory元素是 sterlingcommerce命名空間。

因此,爲了在排序鍵中指定它,你必須指定這個 命名空間。

製作2變化:

  • 添加xmlns:sc="http://www.sterlingcommerce.com/documentation/YFS/reserveItemInventory/input"stylesheet標籤。
  • sort命令中添加sc:之前ReserveItemInventory

爲了具有MultiApi屬性複製到輸出, 變化selectapply-templates"@*|API"(添加@*到 指定屬性節點和|作爲分隔符)。

完整腳本如下(選中上的XslTransform):

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:sc="http://www.sterlingcommerce.com/documentation/YFS/reserveItemInventory/input"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="MultiApi"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|API"> 
     <xsl:sort select="Input/sc:ReserveItemInventory/@ItemID" data-type="number"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

感謝您的響應VALDI,改變XSL沒有工作要麼 的 ......

+0

我想你錯過任何可能的細節。建議的解決方案絕對有效。 正如需要,第一個輸出「API」元素具有「ItemID' ='19998546'和第二個'19998548'。有關工作示例,請參閱http://xsltransform.net/gVhD8RY –

+0

您可以在這裏發佈XSL嗎? –

相關問題