根据子属性值

时间:2017-10-28 17:55:53

标签: xml xslt

我有一个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属性的值有&#34;输入&#34;在末尾。如果我删除xmlns属性下面的XSL按预期工作。

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

1 个答案:

答案 0 :(得分:0)

您的排序失败,因为ReserveItemInventory元素在 sterlingcommerce名称空间。

因此,为了在排序键中指定它,您必须指定它 命名空间。

进行2次更改:

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

为了将MultiApi属性复制到输出中, 将select中的apply-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>