Reputation: 21
Hi I am beginner to use the XSLT. I had an XMl with different attributes and my aim is to create a new XML with respective order pattern of attributes. For this I am using the below XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<channel>
<title>Test Data</title>
<link>http://www.google.com</link>
<xsl:apply-templates select="products/product" />
</channel>
</xsl:template>
<xsl:template match="products/product">
<item>
<xsl:apply-templates select="@*"/>
</item>
</xsl:template>
<xsl:template match="products/product/@*">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="products/product/Parent_identifier | products/product/Unique_ID | products/product/EAN>
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The sample XML is as Follows:
<?xml version='1.0' encoding='UTF-8'?>
<products>
<product>
<EAN>9999999999975</EAN>
<Parent_identifier>BP_100001766</Parent_identifier>
<Unique_ID>999999975</Unique_ID>
</product>
<product>
<EAN>99000009999975</EAN>
<Parent_identifier>BP_10066</Parent_identifier>
<Unique_ID>999000975</Unique_ID>
</product>
<product>
<EAN>99111119999975</EAN>
<Parent_identifier>BP_10222066</Parent_identifier>
<Unique_ID>99911110975</Unique_ID>
</product>
</products>
My expected out put XML is:
<?xml version='1.0' encoding='UTF-8'?>
<products>
<product>
<Parent_identifier>BP_100001766</Parent_identifier>
<Unique_ID>999999975</Unique_ID>
<EAN>9999999999975</EAN>
</product>
<product>
<Parent_identifier>BP_10066</Parent_identifier>
<Unique_ID>999000975</Unique_ID>
<EAN>99000009999975</EAN>
</product>
<product>
<Parent_identifier>BP_10222066</Parent_identifier>
<Unique_ID>99911110975</Unique_ID>
<EAN>99111119999975</EAN>
</product>
</products>
Can anyone help me to find out my mistake and to get expected output
Upvotes: 2
Views: 449
Reputation: 163322
There are no attributes in this XML. Your sample input/output shows that you are trying to change the order of elements. Most of your uses of @*
in your stylesheet should therefore be changed to *
.
To output elements in a different order from the input, select them explicitly in the apply-templates:
XSLT 2.0/3.0
<xsl:apply-templates select="Parent_identifier, Unique_ID, EAN"/>
XSLT 1.0
<xsl:apply-templates select="Parent_identifier"/>
<xsl:apply-templates select="Unique_ID"/>
<xsl:apply-templates select="EAN"/>
Upvotes: 3