Reputation: 11
I am learning xslt. I am getting expected output if i remove all prefix "oz:" from XML file. Please let me know how to fix it.
Also, please let me know a logic for "Descriptor" and "type" in element "Spec"
Expected csv output:
Item,Desc,Type,Origin
"100","nuts","Metal","USA"
"125","bolts","",""
"125","screw","",""
Actual output:
Item,Desc,Type,Origin
100nutsMetalUSA125bolts125screw
XML:
<?xml version='1.0' encoding='UTF-8'?>
<oz:listing xmlns:oz="abc:com.listing/itemData">
<oz:categ>
<oz:item>100</oz:item1>
<oz:desc>nuts</oz:desc>
<oz:Spec oz:Descriptor="Metal">
<oz:ID oz:type="Spec_Code">Metal</oz:ID>
</oz:Spec>
<oz:origin>USA</oz:origin>
</oz:categ>
<oz:categ>
<oz:item>125</oz:item1>
<oz:desc>bolts</oz:desc>
</oz:categ>
<oz:categ>
<oz:item>125</oz:item1>
<oz:desc>screw</oz:desc>
<oz:origin>CAN</oz:origin>
</oz:categ>
</oz:listing>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:text>Item,Desc,Type,Origin
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="listing">
<xsl:for-each select="./categ">
"<xsl:value-of select="normalize-space(./item)"/>","<xsl:value-of select="normalize-space(./desc)"/>","<xsl:value-of select="normalize-space(./Spec)"/>","<xsl:value-of select="normalize-space(./origin)"/>"
<!-- Add a newline at the end of the record -->
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Views: 2260
Reputation: 52858
You need to bind the abc:com.listing/itemData
namespace to a prefix in your XSLT and use it in your xpaths.
In the example below I bind it to the oz
prefix (just like the input XML), but I could've used a different prefix. As long as it's bound to the same uri (abc:com.listing/itemData
).
See http://www.jclark.com/xml/xmlns.htm for more information on how namespaces work in XML.
Example...
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oz="abc:com.listing/itemData">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:text>Item,Desc,Type,Origin
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="oz:categ">
<xsl:value-of select="concat(
'"',oz:item,'",',
'"',oz:desc,'",',
'"',oz:Spec/@oz:Descriptor,'",',
'"',oz:origin,'"
')"/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1