Reputation: 315
I have this XML document
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="data.xsl"?>
<products xmlns="http://zanox.com/productdata/exportservice/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://zanox.com/productdata/exportservice/v1 http://productdata.zanox.com/exportservice/schema/export-1.1.xsd">
<product>
<name>BERTRAND BELIN+YANN DESTAL+D.RIVET</name>
<program>3467</program>
</product>
And my XSL :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//product"/>
</xsl:template>
<xsl:template match="product">
<xsl:text>insert into data (name, program) values(</xsl:text>
<xsl:value-of select="./name"/>
<xsl:text>,'</xsl:text>
<xsl:value-of select="./program"/>
<xsl:text>'); </xsl:text>
</xsl:template>
</xsl:stylesheet>
But this doesn't work I need to remove the xmlns namespace from products in the XML. I don't understand why omit-xml-declaration="yes" doesn't do the job?
Upvotes: 0
Views: 3800
Reputation: 9627
To make your xslt work with an xml file with name spaces you have to add the name space declaration to the style sheet. Also the default name space (name space without an pefix in xml) needs to have an prefix in xslt. To make your style sheet work try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1="http://zanox.com/productdata/exportservice/v1">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//v1:product"/>
</xsl:template>
<xsl:template match="v1:product">
<xsl:text>insert into data (name, program) values(</xsl:text>
<xsl:value-of select="./v1:name"/>
<xsl:text>,'</xsl:text>
<xsl:value-of select="./v1:program"/>
<xsl:text>'); </xsl:text>
</xsl:template>
</xsl:stylesheet>
The xml declaration is the first line in most xml files:
<?xml version="1.0"?>
Which will be "omitted" with omit-xml-declaration="yes"
. But only make sense with xsl:output method="xml"
.
The above xslt will generate the following output:
insert into data (name, program) values(BERTRAND BELIN+YANN DESTAL+D.RIVET,'3467');
Upvotes: 1