XSL: Adding namespace prefix to elements read from external file

Discussion in 'XML' started by johanneskrueger@gmx.net, Feb 11, 2007.

  1. Guest

    Hello,

    I'm currently using <xsl:copy-of select="document(...)/svg:svg"/> to
    embed an SVG file into an XHTML file. I already defined the SVG
    namespace and assigned svg as its prefix in my XSLT 1.0 sheet.

    What commands instead of copy-of could I use in order to get the
    prefix with every element from the external file, e.g. <svg:svg>,
    <svg:line> etc. instead of just <svg> and <line>, as it is noted in
    the external file?
    I tried to create new elements and assign everything, but it didn't
    work out so far.


    Thank you for any helpful suggestion!

    Regards,
    Johannes
    , Feb 11, 2007
    #1
    1. Advertising

  2. wrote:

    > I'm currently using <xsl:copy-of select="document(...)/svg:svg"/> to
    > embed an SVG file into an XHTML file. I already defined the SVG
    > namespace and assigned svg as its prefix in my XSLT 1.0 sheet.
    >
    > What commands instead of copy-of could I use in order to get the
    > prefix with every element from the external file, e.g. <svg:svg>,
    > <svg:line> etc. instead of just <svg> and <line>, as it is noted in
    > the external file?
    > I tried to create new elements and assign everything, but it didn't
    > work out so far.


    Does the external SVG document define the SVG namespace properly (e.g.
    as its default namespace
    <svg xmlns="http://www.w3.org/2000/svg">
    or with some prefix e.g.
    <svg:svg xmlns:svg="http://www.w3.org/2000/svg">
    )? Then xsl:copy-of should suffice to create elements in the SVG
    namespace in the result tree. The prefix finally used for serialization
    should not matter.
    Or why are you interested in the prefix?



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Feb 11, 2007
    #2
    1. Advertising

  3. > What commands instead of copy-of could I use in order to get the
    > prefix with every element from the external file, e.g. <svg:svg>,
    > <svg:line> etc. instead of just <svg> and <line>, as it is noted in
    > the external file?


    Assuming that everything is working properly (which it may not be, but
    you haven't shown us an example)... What may be happening is that your
    system's serializer has chosen to make the SVG namespace the default
    namespace; once that's been done, the prefix does not have to appear on
    every element (but all contained elements which are *NOT* in that
    namespace must either have a prefix or a new default namespace
    declaration to break that association).

    The above also assumes you're going through a standard serializer. If
    you're taking the XSLT processor's output directly as SAX or DOM, the
    prefixes may not be present, since they're considered "syntactic sugar";
    you should be operating entirely off the localname and namespace URI, or
    explicitly running the output document through a normalization stage if
    you need the prefixes (which, as I said, is normally done in the
    serializer if you're outputting XML markup).

    If you want more specific advice, you'll have to be more specific about
    what you're doing and what you're seeing. A small (ten-line or so)
    example can be useful as a way of explaining that.


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Feb 11, 2007
    #3
  4. Guest

    Hello,

    thanks for your replies. To illustrate what is going on, I made some
    excerpts from my XSLT stylesheet and pasted them at the end.
    The SVG files to be inserted start with <svg xmlns:"http://www.w3.org/
    2000/svg" ... >, i.e. they have the correct namespace URI but no
    prefix assigned.

    As I need the output code for some kind of demonstration, I'd rather
    include the prefixes with the SVG section.

    Regarding your answer, Joe, I didn't entirely get what you said in the
    2nd paragraph. I'm just feeding my files into Instant Saxon and have a
    look at its output.


    Regards,
    Johannes


    XSLT file header and root:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:transform xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://
    www.w3.org/1999/XSL/Transform"
    xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:svg="http://
    www.w3.org/2000/svg" version="1.0">

    When a reference to a SVG file is found, a template is called to
    process it:

    <xsl:call-template name="getFigure">
    <xsl:with-param name="svgfile"><xsl:value-of select="@src"/></
    xsl:with-param>
    </xsl:call-template>

    <xsl:template name="getFigure">
    <xsl:param name="svgfile"/>
    <xsl:copy-of select="document($svgfile)/svg:svg"/>
    </xsl:template>
    , Feb 11, 2007
    #4
  5. wrote:
    > The SVG files to be inserted start with <svg xmlns:"http://www.w3.org/
    > 2000/svg" ... >, i.e. they have the correct namespace URI but no
    > prefix assigned.


    Which is fine; you're using the default namespace.

    > As I need the output code for some kind of demonstration, I'd rather
    > include the prefixes with the SVG section.


    Unfortunately, while there's an official mechanism in XSLT 1.0 for
    hinting that a prefix may not be needed, there isn't a clear one for
    hinting that you _do_ want it used preferentially.

    I guess you could try forcing an explicit declaration of the svg: prefix
    into your output document, and see if that's enough of a hint for your
    processor to realize that you want it to use this prefix rather than a
    default binding... something like:

    > <xsl:template name="getFigure">
    > <xsl:param name="svgfile"/>

    <svg:svg xmlns:svg="http://www.w3.org/2000/svg">
    <xsl:copy-of select="document($svgfile)/svg:svg/(@*|node())"/>
    </svg:svg>
    > </xsl:template>


    If you really need a reliable portable solution... ugh. Best I can think
    of is to recreate copy-of via recursion, and set up a template which
    matches elements in the svg namespace and explicitly reconstructs them
    using xsl:element with prefix="svg"... And even that may not be
    guaranteed, depending on how the document is processed after it leaves
    the XSLT engine.



    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Feb 11, 2007
    #5
  6. Guest

    On 11 Feb., 19:26, Joe Kesselman <> wrote:
    > I guess you could try forcing an explicit declaration of the svg: prefix
    > into your output document, and see if that's enough of a hint for your
    > processor to realize that you want it to use this prefix rather than a
    > default binding... something like:
    >
    > > <xsl:template name="getFigure">
    > > <xsl:param name="svgfile"/>

    >
    > <svg:svg xmlns:svg="http://www.w3.org/2000/svg">
    > <xsl:copy-of select="document($svgfile)/svg:svg/(@*|node())"/>
    > </svg:svg>
    >
    > > </xsl:template>


    That didn't work I'm afraid. After all, the processor is obviously
    aware of the namespace prefix (at least for the elements within the
    stylesheet). It was mandatory to write document($svgfile)/svg:svg,
    whereas document($svgfile)/svg wouldn't work. I'll give the creation
    of new elements another try and will drop a note when it works out.


    Regards,
    Johannes
    , Feb 11, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. heidi

    Tag prefix and namespace

    heidi, Nov 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    345
    heidi
    Nov 2, 2003
  2. Tim Tyler
    Replies:
    36
    Views:
    1,447
    Darryl L. Pierce
    Dec 10, 2004
  3. Replies:
    1
    Views:
    3,587
    A. Bolmarcich
    May 27, 2005
  4. Rob Smegma
    Replies:
    1
    Views:
    1,797
    shaun
    Sep 26, 2005
  5. Chris  Chiasson
    Replies:
    6
    Views:
    604
    Richard Tobin
    Nov 14, 2006
Loading...

Share This Page