[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]
RE: [xml-dev] hackable xml
- From: "Klotz, Leigh" <Leigh.Klotz@xerox.com>
- To: "Tony Nassar" <tnassar@palantir.com>, "xml-dev" <xml-dev@lists.xml.org>
- Date: Thu, 12 Aug 2010 14:59:59 -0700
XForms makes writing UI for this type of thing trivial.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xsltforms/xsltforms.xsl" type="text/xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:s="http://somecomp.com">
<head>
<title>Config</title>
<model xmlns="http://www.w3.org/2002/xforms">
<xf:instance src="config.xml" />
<xf:submission resource="config.xml" method="put" id="save" />
</model>
</head>
<body>
<group xmlns="http://www.w3.org/2002/xforms">
<input ref="s:foo">
<label>Foo: </label>
</input>
<submit submission='save'>
<label>Save</label>
</submit>
</group>
</body>
</html>
A key observation of XForms is relevant here: the use of the ref attribute. It indicates an XPath location path to a nodeset, whose first node is then accessed. In most cases, it's interpreted as the leaf-node simpleType value.
Although you said you don't want a dependency on an API, it's not possible to avoid one. The Groovy example uses an XmlParser, which is an API.
I won't sing the praises of JDOM here, other than to say I believe it's the most succinct imperative XML API I've seen, and it's quite stable.
While it would be nice if it offered JDK 1.5 generics, we've written JDOMUtil, a single file of enhancements offered as static methods provides both the "ref" power of XForms and the type-safe collections of JDK 1.5. Here's a sample showing use of a few such utilities:
List<Element> rootPartitions = JDOMUtil.selectElements( rootElement, "/ContentStore/roots/root" );
and
setEnableCount(JDOMUtil.parseBoolean(JDOMUtil.ref(root, "/ContentStore/enableCount", "false"));
So the foo config example could be written like this:
Document config = JDOMUtil.parse(new File("file.xml"));
JDOMUtil.setRef(root, "/config/foo", "xyz");
If you insist on using namespaces, use the version with the trailing list of namespaces arg:
JDOMUtil.setRef(root, "/s:config/s:foo", "xyz", Namespace.getNamespace("s", "http://somecomp.com"));
Leigh.
-----Original Message-----
From: Tony Nassar [mailto:tnassar@palantir.com]
Sent: Thursday, August 12, 2010 1:13 PM
To: xml-dev
Subject: RE: [xml-dev] hackable xml
Wow! I very often have to handle documents with multiple namespaces. Admittedly, I don't think that collisions between element names have ever been issue.
I think that that the problem here is not XML, it's the APIs. I have no trouble with XSLT, but my coworkers do, and if I had to carry out the task that Andrew describes, I'd probably resort to sed -i! I avoid DOM like the plague, and don't want to have a dependence on JDOM or some other API.
I work for a Java-intensive shop, but I'd recommend Groovy (a dynamic language build on top of Java). I believe there is a Python library that does similar things to the following (I'm sure Javascript does, too, but it's not an option for me). I really don't think this could be any easier.
// Here's the problem statement.
welch = """<config xmlns="http://somecomp.com">
<foo>abc</foo>
</config>
"""
// parsed into a nested plain old Java object config = new XmlParser().parseText(welch) // Set the value.
config.foo[0].value = 'xyz'
// Verify that this is what you wanted.
new XmlNodePrinter(preserveWhitespace: true).print(config)
-----Original Message-----
From: Andrew Welch [mailto:andrew.j.welch@gmail.com]
Sent: Monday, July 26, 2010 11:22 AM
To: Richard Salz
Cc: xml-dev
Subject: Re: [xml-dev] hackable xml
On 26 July 2010 13:53, Richard Salz <rsalz@us.ibm.com> wrote:
> I don't get it -- which community needs this xml-like thing? And why?
The community that just wants to read or write very simple xml files.
Given:
<config xmlns="http://somecomp.com">
<foo>abc</foo>
</config>
...and you want to update the value of <foo>, how would you do it?
Or put more realisticly, a colleague of yours knows very little about XML and all of its related technologies, and asks you how they should do it.
- XSLT transform
- XQuery update
- JDOM, XOM etc
- SAX parse and generate the events
- some data-binding tool (if an xsd exists)
All fairly straight forward for the xml community, but to anyone else each of those seem like a massive overkill for such a seemingly simple task. Perhaps there is a simple way that I've missed?
The ultimate goal of hackable xml is to make it possible to just do a string replace of "<foo>abc</foo>" with "<foo>newValue</foo>" (which is often what happens anyway, causing many hours of fun) and then serialize/reparse without any issues.
_______________________________________________________________________
XML-DEV is a publicly archived, unmoderated list hosted by OASIS to support XML implementation and development. To minimize spam in the archives, you must subscribe before posting.
[Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
Or unsubscribe: xml-dev-unsubscribe@lists.xml.org
subscribe: xml-dev-subscribe@lists.xml.org List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php
[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]