繁体   English   中英

根据孩子的属性值对父母排序

[英]Sorting parent based on child's attribute value

我有一个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属性的值在结尾处有“ input”。 如果删除XSL下的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>

排序失败,因为ReserveItemInventory元素位于sterlingcommerce命名空间中。

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

进行2个更改:

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

为了将MultiApi属性复制到输出, select apply-templates select更改为"@*|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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM