使用 document()的 copy-of 将 SVG 添加到 XHTML 输出

在处理 XML 时,我试图用以下代码将引用自 href属性的 SVG 文件直接复制到输出 HTML 中:

 <xsl:copy-of copy-namespaces="yes" select="document(@href)"/>

copy-namespaces应该是不必要的,因为默认值是“ yes”,但我已经添加了它,以防止问题,我是否已经尝试了。

这些文件被复制到 HTML 中,但是任何带名称空间的元素都被清空了。例如,复制之前如下所示的文件:

  <rdf:RDF>
<cc:Work rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
<dc:title/>
</cc:Work>
</rdf:RDF>
</metadata>
<g transform="translate(-519.21143,-667.79077)" id="layer1">
<image xlink:href="data:image/png;base64

之后看起来像这样:

  <_0:RDF xmlns:_0="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<_0:Work xmlns:_0="http://creativecommons.org/ns#" about="">
<_0:format xmlns:_0="http://purl.org/dc/elements/1.1/">image/svg+xml</_0:format>
<_0:type xmlns:_0="http://purl.org/dc/elements/1.1/" resource="http://purl.org/dc/dcmitype/StillImage"/>
<_0:title xmlns:_0="http://purl.org/dc/elements/1.1/"/>
</_0:Work>
</_0:RDF>
</metadata>
<g id="layer1" transform="translate(-519.21143,-667.79077)">
<image href="data:image/png;base64

在 image 元素的 href值上缺少 xlink 名称空间尤其成问题。

对于如何不同地读取 SVG 文件而不进行任何解释,您有什么想法吗?

我已经找到了一个“有效”的解决方案,但它是一个黑客,我想要一个更优雅的解决方案:

<xsl:template name="topic-image-svg">
<!-- Generate tags to embed SWFs -->
<xsl:element name="div">
<xsl:if test="@width">
<xsl:attribute name="width">
<xsl:value-of select="@width"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@height">
<xsl:attribute name="height">
<xsl:value-of select="@height"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="document(@href)" mode="svg"/>
</xsl:element>
</xsl:template>


<xsl:template match="*" mode="svg">
<xsl:copy copy-namespaces="yes">
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="self::node()[name() = 'xlink:href']">
<xsl:attribute name="xlink:href"><xsl:value-of select="."></xsl:value-of></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:copy></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates mode="svg"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
2606 次浏览

I think you've hit upon the reason for this XSLT operation:

http://www.w3schools.com/xsl/el_namespace-alias.asp

which leaves your mangled namespaces intact until output is generated, when the namespace transformation is done.