XSLT 分组/合并 - 两个或多个不同的节点合二为一
XSLT grouping / merging - two or more different nodes into one
正在努力进行 xslt 转换。
我有两个(或更多)相同的 节点,除了 部分。
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
...
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
因此,我想要一个 节点,在 中有多个条目。
<A_OutbDeliveryHeader>
<A_OutbDeliveryHeaderType>
<DeliveryDocumentType>LF</DeliveryDocumentType>
<to_DeliveryDocumentText>
...
</to_DeliveryDocumentText>
...
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
...
</A_OutbDeliveryItemType>
<A_OutbDeliveryItemType>
...
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
</A_OutbDeliveryHeaderType>
</A_OutbDeliveryHeader>
最好的入门方式是什么?通过...分组?虽然我没有任何钥匙。注意:我可以使用 XSLT 2.0 或 3.0。
完整示例XML
<?xml version="1.0" encoding="UTF-8"?>
<A_OutbDeliveryHeader>
<A_OutbDeliveryHeaderType>
<DeliveryDocumentType>LF</DeliveryDocumentType>
<to_DeliveryDocumentText>
<A_OutbDeliveryHeaderTextType>
<DeliveryDocument>800005120</DeliveryDocument>
<TextElementText>Lorem Ipsum</TextElementText>
</A_OutbDeliveryHeaderTextType>
</to_DeliveryDocumentText>
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
<DeliveryDocumentItem>000010</DeliveryDocumentItem>
<Warehouse>103</Warehouse>
<ItemWeightUnit>KG</ItemWeightUnit>
<ActualDeliveryQuantity>3.000</ActualDeliveryQuantity>
<ReferenceSDDocumentItem>000010</ReferenceSDDocumentItem>
<Plant>1003</Plant>
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
</A_OutbDeliveryHeaderType>
<A_OutbDeliveryHeaderType>
<DeliveryDocumentType>LF</DeliveryDocumentType>
<to_DeliveryDocumentText>
<A_OutbDeliveryHeaderTextType>
<DeliveryDocument>800005120</DeliveryDocument>
<TextElementText>Lorem Ipsum</TextElementText>
</A_OutbDeliveryHeaderTextType>
</to_DeliveryDocumentText>
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
<DeliveryDocumentItem>000011</DeliveryDocumentItem>
<Warehouse>104</Warehouse>
<ItemWeightUnit>PC</ItemWeightUnit>
<ActualDeliveryQuantity>1.000</ActualDeliveryQuantity>
<ReferenceSDDocument>4858</ReferenceSDDocument>
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
</A_OutbDeliveryHeaderType>
</A_OutbDeliveryHeader>
编辑:到目前为止,我的 XSLT(我可以使用 XSLT 2.0 或 3.0)似乎可以解决问题。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//A_OutbDeliveryHeader">
<xsl:copy>
<xsl:for-each-group select="A_OutbDeliveryHeaderType" group-by="''">
<xsl:apply-templates select="@* | node()[not(self::to_DeliveryDocumentItem)]" />
<to_DeliveryDocumentItem>
<xsl:apply-templates select="current-group()/to_DeliveryDocumentItem/A_OutbDeliveryItemType" />
</to_DeliveryDocumentItem>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
看来你不需要分组,只是
<xsl:template match="A_OutbDeliveryHeader">
<xsl:copy>
<xsl:copy-of select="A_OutbDeliveryHeaderType[1]/(* except to_DeliveryDocumentItem)"/>
<to_DeliveryDocumentItem>
<xsl:copy-of select="A_OutbDeliveryHeaderType/to_DeliveryDocumentItem/A_OutbDeliveryItemType"/>
</to_DeliveryDocumentItem>
</xsl:copy>
</xsl:template>
正在努力进行 xslt 转换。
我有两个(或更多)相同的
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
...
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
因此,我想要一个
<A_OutbDeliveryHeader>
<A_OutbDeliveryHeaderType>
<DeliveryDocumentType>LF</DeliveryDocumentType>
<to_DeliveryDocumentText>
...
</to_DeliveryDocumentText>
...
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
...
</A_OutbDeliveryItemType>
<A_OutbDeliveryItemType>
...
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
</A_OutbDeliveryHeaderType>
</A_OutbDeliveryHeader>
最好的入门方式是什么?通过...分组?虽然我没有任何钥匙。注意:我可以使用 XSLT 2.0 或 3.0。
完整示例XML
<?xml version="1.0" encoding="UTF-8"?>
<A_OutbDeliveryHeader>
<A_OutbDeliveryHeaderType>
<DeliveryDocumentType>LF</DeliveryDocumentType>
<to_DeliveryDocumentText>
<A_OutbDeliveryHeaderTextType>
<DeliveryDocument>800005120</DeliveryDocument>
<TextElementText>Lorem Ipsum</TextElementText>
</A_OutbDeliveryHeaderTextType>
</to_DeliveryDocumentText>
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
<DeliveryDocumentItem>000010</DeliveryDocumentItem>
<Warehouse>103</Warehouse>
<ItemWeightUnit>KG</ItemWeightUnit>
<ActualDeliveryQuantity>3.000</ActualDeliveryQuantity>
<ReferenceSDDocumentItem>000010</ReferenceSDDocumentItem>
<Plant>1003</Plant>
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
</A_OutbDeliveryHeaderType>
<A_OutbDeliveryHeaderType>
<DeliveryDocumentType>LF</DeliveryDocumentType>
<to_DeliveryDocumentText>
<A_OutbDeliveryHeaderTextType>
<DeliveryDocument>800005120</DeliveryDocument>
<TextElementText>Lorem Ipsum</TextElementText>
</A_OutbDeliveryHeaderTextType>
</to_DeliveryDocumentText>
<to_DeliveryDocumentItem>
<A_OutbDeliveryItemType>
<DeliveryDocumentItem>000011</DeliveryDocumentItem>
<Warehouse>104</Warehouse>
<ItemWeightUnit>PC</ItemWeightUnit>
<ActualDeliveryQuantity>1.000</ActualDeliveryQuantity>
<ReferenceSDDocument>4858</ReferenceSDDocument>
</A_OutbDeliveryItemType>
</to_DeliveryDocumentItem>
</A_OutbDeliveryHeaderType>
</A_OutbDeliveryHeader>
编辑:到目前为止,我的 XSLT(我可以使用 XSLT 2.0 或 3.0)似乎可以解决问题。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//A_OutbDeliveryHeader">
<xsl:copy>
<xsl:for-each-group select="A_OutbDeliveryHeaderType" group-by="''">
<xsl:apply-templates select="@* | node()[not(self::to_DeliveryDocumentItem)]" />
<to_DeliveryDocumentItem>
<xsl:apply-templates select="current-group()/to_DeliveryDocumentItem/A_OutbDeliveryItemType" />
</to_DeliveryDocumentItem>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
看来你不需要分组,只是
<xsl:template match="A_OutbDeliveryHeader">
<xsl:copy>
<xsl:copy-of select="A_OutbDeliveryHeaderType[1]/(* except to_DeliveryDocumentItem)"/>
<to_DeliveryDocumentItem>
<xsl:copy-of select="A_OutbDeliveryHeaderType/to_DeliveryDocumentItem/A_OutbDeliveryItemType"/>
</to_DeliveryDocumentItem>
</xsl:copy>
</xsl:template>