Reputation: 3046
I am using this stylesheet to create an opf file for epub using an ncx file (which is the table of contents in epub) as primary input:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:fn='http://www.w3.org/2005/xpath-functions' exclude-result-prefixes='xsl xs fn' xpath-default-namespace="http://www.daisy.org/z3986/2005/ncx/" xmlns="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/" >
<xsl:output method='xml' version='1.0' encoding='UTF-8' indent='no'/>
<xsl:param name="imgs" select="doc('../../EPUB/images.xml')" />
<xsl:template match='/'>
<xsl:result-document href="EPUB/META-INF/container.xml" method="xml" encoding="UTF-8">
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
<rootfiles>
<rootfile full-path="OPS/output.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
</xsl:result-document>
<package version="2.0" unique-identifier="ID100">
<metadata>
<dc:title>E-Pub</dc:title>
<dc:language>de</dc:language>
<dc:identifier id="ID100">epub</dc:identifier>
<dc:creator>sd</dc:creator>
</metadata>
<manifest>
<item id="nav" href="nav.ncx" media-type="application/x-dtbncx+xml"/>
<item id="css" href="Style/styles.css" media-type="text/css"/>
<xsl:apply-templates select="//navPoint[content]"/>
<xsl:copy-of select="$imgs"/>
</manifest>
<spine toc="nav">
<xsl:apply-templates select="//navPoint[content]" mode="toc"/>
</spine>
</package>
</xsl:template>
<xsl:template match="ncx">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="head">
</xsl:template>
<xsl:template match="meta">
</xsl:template>
<xsl:template match="docTitle">
</xsl:template>
<xsl:template match="navMap">
</xsl:template>
<xsl:template match="navLabel">
</xsl:template>
<xsl:template match="text">
</xsl:template>
<xsl:template match="navPoint[content]">
<item id="{@id}" href="{content/@src}" media-type="application/xhtml+xml"/>
</xsl:template>
<xsl:template match="navPoint[content]" mode="toc">
<itemref idref="{@id}"/>
</xsl:template>
</xsl:stylesheet>
Everything works fine except copying the content of the file referenced by the imgs parameter. That file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<images>
<item id="d1e267_1" >
<item id="d1e398_4" />
<item id="d1e653_5" />
<!-- etc. -->
</images>
When I copy this document like shown in my stylesheet, the root element <images>
is copied into the output as well, with an empty xmlns=""
attribute.
<images xmlns="">
<item id="d1e267_1" >
<item id="d1e398_4" />
<item id="d1e653_5" />
<!-- etc. -->
</images>
And if I add the images
element to the doc-function, nothing gets copied, I guess because images is not in the declared default namespace. I only want to copy the item
elements.
How do I handle this namespace here? I already declare a default namespace (for the ncx file) and I already declare an unprefixed namespace.
Upvotes: 0
Views: 116
Reputation: 167506
You need to transform the nodes then as copying means copying it as is, with the same name and namespace, and your elements are in no namespace, if you copy them, they remain in no namespace, and as the parent is in a different namespace, the serializer adds xmlns=""
, to maintain the empty namespace.
So replace <xsl:copy-of select="$imgs"/>
with <xsl:apply-templates select="$imgs/node()" mode="change-namespace"/>
and add a template
<xsl:template match="*" mode="change-namespace">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="change-namespace"/>
</xsl:element>
</xsl:template>
If you only want to transform the item
elements, then use <xsl:apply-templates select="$imgs/*/*" mode="change-namespace"/>
or <xsl:apply-templates xpath-default-namespace="" select="$imgs/images/item" mode="change-namespace"/>
.
Upvotes: 3