根据孩子的属性值对父项进行排序
问题描述:
我有一个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>
答
你的排序失败,因为ReserveItemInventory
元素是 sterlingcommerce
命名空间。
因此,为了在排序键中指定它,你必须指定这个 命名空间。
制作2变化:
- 添加
xmlns:sc="http://www.sterlingcommerce.com/documentation/YFS/reserveItemInventory/input"
到stylesheet
标签。 - 在
sort
命令中添加sc:
之前ReserveItemInventory
。
为了具有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>
感谢您的响应VALDI,改变XSL没有工作要么 的 ...... XSL:应用模板> XSL:模板> XSL:样式> –
我想你错过任何可能的细节。建议的解决方案绝对有效。 正如需要,第一个输出“API”元素具有“ItemID' ='19998546'和第二个'19998548'。有关工作示例,请参阅http://xsltransform.net/gVhD8RY –
您可以在这里发布XSL吗? –