[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Vocabulary Combination and optional namespaces


str words
Joe English wrote:
> Paul Prescod wrote:
>>Anybody who is so namespace allergic that they cannot add a single
>>"xmlns" attribute to their document is, in my opinion, trying to make
>>life difficult for others, not trying to make life easy for themselves.
> 
> In addition to writing XML documents, I also write software
> that processes those XML documents.  That's where the real
> namespace pain begins.

If you are writing software that _implements_ namespaces (e.g. a parser 
or filter or API) then I can see the problem. If you write software that 
_uses_ namespaces, I don't see it that much. Writing an XSLT for a 
document with namespaces is not much harder than writing it for one 
without. Ditto with Python.

In fact, here's what I did: I wrote a Python program that worked with 
RSS. Then I added the "xmlns attribute". Unsurprisingly, it just kept 
working because it was happy to ignore that attribute. But okay, one 
could argue that relying on the fact that the namespace was defaulted 
violates the spirit of the namespaces REC even if any decent XML toolkit 
should happily let you do so. So then I rewrote the program using full 
namespaces. The changes were straightforward and added exactly one line 
of code (the namespace declaration).

In XSLT all you need to do is add a namespace declaration and a couple 
of prefixes to your program.

Now I will happily admit that there are circumstances where namespaces 
are massively painful to code around. In particular, when you are 
implementing the infrastructure. But I submit that the case I mentioned, 
adding a namespace to a document type that *does not combine* namespaces 
is not one of them. Merely adding a namespace declarations does not make 
documents substantially harder to process...and that was the context of 
the original question. Whether namespaces are a good idea or not is 
totally separate.

 > That single "xmlns" attribute (er, namespace declaration)
 > means that the nice simple data model of elements, attributes,
 > and text turns into a vastly more complicated data model, where
 > names are <namespace-name,local-name> pairs -- or even worse,
 > <namespace-name,namespace-prefix,local-name> triplets -- instead
 > of atomic strings,

Working with tuples is not that difficult and if you hate it, tuples can 
be turned into strings (e.g.newline-separated) easily.

 > and you have to keep track of the namespace
 > environment to serialize or deserialize anything.

In a document with only one namespace the "namespace environment" is 
pretty simple. That was the context of the original question. It is the 
OPers _stated intention_ to use namespaces when vocabularies are mixed. 
So the question is what is the cost of also doing so when there is a 
single namespace. Not much.

And anyhow, it should be the responsibility of infrastructure to track 
the namespace environment. XSLT is an example of infrastructure that 
does a pretty good job.

Here are the Python and XSLT examples:

from xml.dom import minidom

def countTitleWords(dom):
	print
	items = dom.getElementsByTagName("item")
	for item in items:
		title = item.getElementsByTagName("title")[0]
		words = len(title.firstChild.nodeValue.split())
		newel = dom.createElement("words")
		newel.appendChild(dom.createTextNode(str(words)))
		item.appendChild(newel)
	print dom.toxml()

countTitleWords(minidom.parse("rss.xml"))

countTitleWords(minidom.parse("rss_with_ns.xml"))

rssURI = "http://www/rss"

def countTitleWordsNS(dom):
	print
	items = dom.getElementsByTagNameNS(rssURI, "item")
	for item in items:
		title = item.getElementsByTagNameNS(rssURI, "title")[0]
		words = len(title.firstChild.nodeValue.split())
		newel = dom.createElementNS(rssURI, "words")
		newel.appendChild(dom.createTextNode(str(words)))
		item.appendChild(newel)
	print dom.toxml()

countTitleWordsNS(minidom.parse("rss_with_ns.xml"))


<?xml version="1.0"?>
<xsl:stylesheet
     xmlns="http://www/rss"
     xmlns:rss="http://www/rss"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
     <xsl:template match="rss:item" priority="5" >
         <item>
                 <xsl:apply-templates/>
                 <words>blah</words>
         </item>
     </xsl:template>

     <xsl:template match="@*|node()" priority="2">
       <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
     </xsl:template>

</xsl:stylesheet>

  Paul Prescod


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.