有人能举例说明如何将以下XML节点与XSLT合并吗?
<pre>
<Table1>
<VendorID>123456</VendorID>
<VendorName>StackOverflow Inc</VendorName>
<Type>ADDRESS</Type>
<TypeName>Mailing</TypeName>
<DescriptionType>Main Mailing</DescriptionType>
<Description>P O Box 01 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc/>
</Table1>
</pre>
<pre>
<Table1>
<VendorID>123456</VendorID>
<VendorName>StackOverflow Inc</VendorName>
<Type>ADDRESS</Type>
<TypeName>Remittance</TypeName>
<DescriptionType>Main Remittance</DescriptionType>
<Description>P O Box 02 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc/>
</Table1>
</pre>
生成的XML需要将不同的地址节点分组到供应商的一个父节点中。
您还可以告诉我您使用哪种XSLT / XML工具。
答案 0 :(得分:4)
VendorID
下面的样式表组。请注意,您没有说明哪些元素是地址的一部分,哪些不是。此样式表假定只有VendorID
和VendorName
不属于该地址。相应地编辑。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="byVendor" match="root/pre" use="Table1/VendorID" />
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="root/pre" />
<xsl:template
match="root/pre[generate-id() =
generate-id(key('byVendor', Table1/VendorID))]">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="Table1">
<xsl:copy>
<xsl:apply-templates select="@*|VendorID|VendorName" />
<Addresses>
<xsl:apply-templates select="key('byVendor', VendorID)/Table1"
mode="address" />
</Addresses>
</xsl:copy>
</xsl:template>
<xsl:template match="Table1" mode="address">
<Address>
<xsl:apply-templates
select="*[not(self::VendorID or self::VendorName)]" />
</Address>
</xsl:template>
</xsl:stylesheet>
应用于此输入时:
<root>
<pre>
<Table1>
<VendorID>123456</VendorID>
<VendorName>StackOverflow Inc</VendorName>
<Type>ADDRESS</Type>
<TypeName>Mailing</TypeName>
<DescriptionType>Main Mailing</DescriptionType>
<Description>P O Box 01 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc />
</Table1>
</pre>
<pre>
<Table1>
<VendorID>123456</VendorID>
<VendorName>StackOverflow Inc</VendorName>
<Type>ADDRESS</Type>
<TypeName>Remittance</TypeName>
<DescriptionType>Main Remittance</DescriptionType>
<Description>P O Box 02 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc />
</Table1>
</pre>
</root>
产地:
<root>
<pre>
<Table1>
<VendorID>123456</VendorID>
<VendorName>StackOverflow Inc</VendorName>
<Addresses>
<Address>
<Type>ADDRESS</Type>
<TypeName>Mailing</TypeName>
<DescriptionType>Main Mailing</DescriptionType>
<Description>P O Box 01 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc />
</Address>
<Address>
<Type>ADDRESS</Type>
<TypeName>Remittance</TypeName>
<DescriptionType>Main Remittance</DescriptionType>
<Description>P O Box 02 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc />
</Address>
</Addresses>
</Table1>
</pre>
</root>
答案 1 :(得分:0)
XML实际上如下,因此我得到重复的原因:
<Vendors>
<Table1>
<VendorID>123456</VendorID>
<VendorName>StackOverflow Inc</VendorName>
<Type>ADDRESS</Type>
<TypeName>Mailing</TypeName>
<DescriptionType>Main Mailing</DescriptionType>
<Description>P O Box 01 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc />
</Table1>
<Table1>
<VendorID>123456</VendorID>
<VendorName>StackOverflow Inc</VendorName>
<Type>ADDRESS</Type>
<TypeName>Remittance</TypeName>
<DescriptionType>Main Remittance</DescriptionType>
<Description>P O Box 02 Manama Bahrain</Description>
<Online>0</Online>
<CommodityTypeDesc />
</Table1>
</Vendors>