Reputation: 311
Can anyone give me an example of merging the following XML nodes with 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>
The resulting XML would need to group the differing address nodes into one parent node for the vendor.
Can you also tell me which XSLT/XML tool you use for this work.
Upvotes: 2
Views: 258
Reputation: 60424
The following stylesheet groups by VendorID
. Note that you don't say which elements are part of the address and which aren't. This stylesheet assumes that only VendorID
and VendorName
are not part of the address. Edit accordingly.
<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>
When applied to this input:
<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>
Produces:
<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>
Upvotes: 4
Reputation: 311
XML is actually, as follows, hence why I am getting duplicates:
<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>
Upvotes: 0