Simplifying namespace declarations

Discussion in 'XML' started by Nicolas George, Sep 18, 2007.

  1. Hi.

    I would like to simplify the namespaces declarations, something like that:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <html:em xmlns:html="http://www.w3.org/1999/xhtml">foo</html:em>
    </html>

    must become:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <em>foo</em>
    </html>

    That is: the em element is already in the XHTML namespace, there is no need
    to re-declare the namespace nor to declare the element.

    Can someone suggest how to do it efficiently/easily, preferably with
    libxml2?

    Thanks.
     
    Nicolas George, Sep 18, 2007
    #1
    1. Advertising

  2. Nicolas George wrote:

    > I would like to simplify the namespaces declarations, something like that:
    >
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <html:em xmlns:html="http://www.w3.org/1999/xhtml">foo</html:em>
    > </html>
    >
    > must become:
    >
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <em>foo</em>
    > </html>


    An XSLT stylesheet as the following could help:

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:template match="*">
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
    <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
    </xsl:template>

    <xsl:template match="/ | @* | text() | comment() |
    processing-instruction()">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 19, 2007
    #2
    1. Advertising

  3. Martin Honnen wrote in message
    <46f111a5$0$4534$-online.net>:
    > An XSLT stylesheet as the following could help:


    Thanks. I was hoping for something more straightforward and integrated, but
    it seems that it does not exist; that's a shame. I will use either this XSLT
    stylesheet or a similar mechanism directly in my program.
     
    Nicolas George, Sep 19, 2007
    #3
  4. Nicolas George wrote:
    > Thanks. I was hoping for something more straightforward and integrated


    I think the straightforward solution would involve hand-coded SAX
    processing, maintaining a stack of which namespaces are in scope.

    BUT: Beware. Remember that in some XML languages, prefixes may appear in
    the document's text context as well -- XSLT is an example of that, of
    course -- so determining which namespace declarations are "redundant"
    can be a MAJOR undertaking, requiring deep understanding of the
    document's semantics. Reducing _use_ of prefixes may be doable -- but
    you'd better plan on retaining all their declarations unless your
    application knows *exactly* what kind of data it's processing and where
    the hazards may be.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Sep 19, 2007
    #4
  5. Joseph Kesselman wrote in message <46f195ac$1@kcnews01>:
    > I think the straightforward solution would involve hand-coded SAX
    > processing, maintaining a stack of which namespaces are in scope.


    I already have a full DOM tree. As I have mentioned, I am working with
    libxml2.

    > BUT: Beware. Remember that in some XML languages, prefixes may appear in
    > the document's text context as well -- XSLT is an example of that, of
    > course -- so determining which namespace declarations are "redundant"
    > can be a MAJOR undertaking, requiring deep understanding of the
    > document's semantics.


    I was made aware of the problem. Fortunately, the document I am trying to
    simplify is no more than XHTML.
     
    Nicolas George, Sep 20, 2007
    #5
  6. Nicolas George wrote:
    > Joseph Kesselman wrote in message <46f195ac$1@kcnews01>:
    >> I think the straightforward solution would involve hand-coded SAX
    >> processing, maintaining a stack of which namespaces are in scope.

    >
    > I already have a full DOM tree. As I have mentioned, I am working with
    > libxml2.


    OK, then an the straightforward solution would be a DOM tree-walk, again
    maintaining a stack of which namespace bindings are in scope and
    modifying the tree as you go. Or rework the DOM serializer to do this,
    if you're only concerned with the generated XML syntax.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Sep 20, 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. Shawn
    Replies:
    2
    Views:
    732
    Rob Dekker
    Feb 27, 2006
  2. kartik

    simplifying use of properties

    kartik, Nov 3, 2004, in forum: Java
    Replies:
    23
    Views:
    778
    Stefan Schulz
    Nov 10, 2004
  3. Robert

    Simplifying HTML code

    Robert, Nov 30, 2004, in forum: HTML
    Replies:
    7
    Views:
    520
    Jim Michaels
    Feb 1, 2006
  4. Simplifying imports?

    , Sep 13, 2005, in forum: Python
    Replies:
    9
    Views:
    339
    George Sakkis
    Sep 14, 2005
  5. Replies:
    8
    Views:
    332
    Dave Thompson
    May 22, 2006
Loading...

Share This Page