使用copy()与document()将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 

图像元素的href值上缺lessxlink命名空间是特别有问题的。

任何想法,我可以做不同的方式读取SVG文件没有任何解释?

我发现了一个“有效”的解决scheme,但这是一个黑客,我想要更优雅的东西:

 <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> 

我想你已经触及了这个XSLT操作的原因:

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

这会在命名空间转换完成时使您的mangled命名空间保持完整,直到生成输出。