1

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&#xD;</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>&#xD;</xsl:text>
</xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
0

1 Answer 1

1

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&#xA;</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="oz:categ">
    <xsl:value-of select="concat(
      '&quot;',oz:item,'&quot;,',
      '&quot;',oz:desc,'&quot;,',
      '&quot;',oz:Spec/@oz:Descriptor,'&quot;,',
      '&quot;',oz:origin,'&quot;&#xA;')"/>
  </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.