Can I eliminate namespace declarations in this transformation output?
I would like to dynamically generate an XML file using XMLT that takes another XML file as input.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="data">
<xsl:apply-templates select="books" />
</xsl:template>
<xsl:template match="books">
<books>
<xsl:apply-templates select="chapters" />
</books>
</xsl:template>
<xsl:template match="chapters">
<chapter name="{@name}" source="{@source}"
xmlns:xi="http://www.w3c.org/2001/XInclude">
<xsl:apply-templates select="pages" />
</chapter>
</xsl:template>
<xsl:template match="pages" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include>
<xsl:value-of select="@name" />
</xi:include>
</xsl:template>
</xsl:stylesheet>
My desired output is below.
<chapter
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1">
<xi:include>page1</xi:include>
<xi:include>page2</xi:include>
<xi:include>page3</xi:include>
</chapter>
Unfortunately, my code gene开发者_开发百科rates the output below.
<chapter
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1">
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page1</xi:include>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page2</xi:include>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page3</xi:include>
</chapter>
Is there a way I could eliminate the actual URI for the namespace for I can have xi:include tages without them?
Move the xmlns:xi="http://www.w3.org/2001/XInclude"
to the xsl:stylesheet
element:
<xsl:stylesheet version="1.0"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="data">
<xsl:apply-templates select="books" />
</xsl:template>
<xsl:template match="books">
<books>
<xsl:apply-templates select="chapters" />
</books>
</xsl:template>
<xsl:template match="chapters">
<chapter name="{@name}" source="{@source}">
<xsl:apply-templates select="pages" />
</chapter>
</xsl:template>
<xsl:template match="pages">
<xi:include>
<xsl:value-of select="@name" />
</xi:include>
</xsl:template>
</xsl:stylesheet>
Do note that your are using two different namespace URIs:
http://www.w3.org/2001/XInclude
and
http://www.w3c.org/2001/XInclude
If they were the same, the namespace fixup process would take care of that.
For this input:
<books>
<chapters name="chapter1">
<pages name="page1"/>
<pages name="page2"/>
<pages name="page3"/>
</chapters>
</books>
This stylesheet (yours with amended typo):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="data">
<xsl:apply-templates select="books" />
</xsl:template>
<xsl:template match="books">
<books>
<xsl:apply-templates select="chapters" />
</books>
</xsl:template>
<xsl:template match="chapters">
<chapter name="{@name}" source="{@source}"
xmlns:xi="http://www.w3c.org/2001/XInclude">
<xsl:apply-templates select="pages" />
</chapter>
</xsl:template>
<xsl:template match="pages" xmlns:xi="http://www.w3c.org/2001/XInclude">
<xi:include>
<xsl:value-of select="@name" />
</xi:include>
</xsl:template>
</xsl:stylesheet>
Output:
<books>
<chapter name="chapter1" source=""
xmlns:xi="http://www.w3c.org/2001/XInclude">
<xi:include>page1</xi:include>
<xi:include>page2</xi:include>
<xi:include>page3</xi:include>
</chapter>
</books>
精彩评论