Simplifying namespace declarations

N

Nicolas George

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

Martin Honnen

Nicolas said:
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>
 
N

Nicolas George

Martin Honnen wrote in message
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.
 
J

Joseph Kesselman

Nicolas said:
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.
 
N

Nicolas George

Joseph Kesselman wrote in message said:
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.
 
J

Joe Kesselman

Nicolas said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top