Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Simplifying namespace declarations

Reply
Thread Tools

Simplifying namespace declarations

 
 
Nicolas George
Guest
Posts: n/a
 
      09-18-2007
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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-19-2007
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
xmlnssl="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/
 
Reply With Quote
 
 
 
 
Nicolas George
Guest
Posts: n/a
 
      09-19-2007
Martin Honnen wrote in message
<46f111a5$0$4534$>:
> 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.
 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      09-19-2007
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
 
Reply With Quote
 
Nicolas George
Guest
Posts: n/a
 
      09-19-2007
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.
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      09-20-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simplifying c code with function pointers goldfita@signalsguru.net C Programming 8 05-22-2006 12:45 AM
Simplifying this combinational logic? Shawn VHDL 2 02-27-2006 04:24 AM
Simplifying HTML code Robert HTML 7 02-01-2006 08:07 AM
Simplifying imports? chapolim-colorado@bol.com.br Python 9 09-13-2005 11:39 PM
simplifying use of properties kartik Java 23 11-10-2004 08:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57