113

While processing my XML, I'm trying to copy an SVG file referenced from an href attribute directly into my output HTML with the following line:

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

The copy-namespaces should not be necessary since the default value is "yes" anyway, but I've added it to prevent questions about whether or not I've tried it.

The files are copied into the HTML, but any namespaced elements are hosed. For example, a file that looks like this before being copied:

  <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

Looks like this afterwards:

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

The missing xlink namespace on the href value of the image element is particularly problematic.

Any thoughts on how I can do this differently to read in the SVG file without any interpretation?

I've found one solution that "works", but it's a hack and I'd like something more elegant:

<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>
Vidya Sagar
  • 1,699
  • 3
  • 17
  • 28
jrhooker
  • 1,373
  • 2
  • 11
  • 14
  • 5
    Does your stylesheet have all the namespaces declare in the root tag? – PhillyNJ Aug 30 '13 at 21:24
  • Yep, that was my first thought too. – jrhooker Aug 30 '13 at 22:10
  • 6
    Is the SVG namespace-well-formed? The fragment you show does not include a binding for the prefix 'xlink'. – C. M. Sperberg-McQueen Aug 30 '13 at 23:39
  • 3
    Which XSLT 2.0 processor do you use? Can you post minimal but complete samples allowing us the reproduce the problem? Which XSLT output method do you use? – Martin Honnen Aug 31 '13 at 09:04
  • 15
    At first sight it looks like a bug in your XSLT processor. But my suspicions are aroused when you say you are copying it to an HTML document. If you are adding it to an HTML DOM, then the HTML DOM is not namespace-friendly, so all kinds of things might happen. However, your title says "xHTML". So I think we need to know more: what XSLT processor are you using, and how are you running it? – Michael Kay Aug 31 '13 at 09:19
  • Possible duplicate of http://stackoverflow.com/questions/4703312/including-an-xml-file-in-an-xml-xsl-file – CodeManX Apr 14 '14 at 13:22
  • possible duplicate of [xsl: transform namespaced tags](http://stackoverflow.com/questions/7670981/xsl-transform-namespaced-tags) – Paul Sweatte Jun 13 '14 at 05:52
  • 1
    Would you mind sharing your code and implementation? I'm curious to at least reproducing this. – Gerard van Helden May 30 '15 at 21:54
  • me too, do you follow with the problem. – Raúl Martín Jun 13 '15 at 13:13
  • 1
    This is always the top unanswered question in my list... Thus I created an example and it works here - no problem with msxsl. This leads to the question of @MartinHonnen: which xslt processor do you use? – Wolfgang Kluge Jun 15 '15 at 04:45

1 Answers1

1

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.