Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Serialize parentless Text nodes

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Wolfgang Hoschek <whoschek AT lbl.gov>
  • To: Michael Kay <mike AT saxonica.com>
  • Cc: 'Elliotte Harold' <elharo AT metalab.unc.edu>, xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] Serialize parentless Text nodes
  • Date: Sat, 9 Jul 2005 09:17:02 -0700

On Jul 9, 2005, at 12:04 AM, Michael Kay wrote:

For the W3C XQuery Serialization spec, all result sequence nodes to
be serialized may be parentless, except attributes and namespaces.
Should be easy to fix as far as I can see...


Actually, the restriction on serializing attribute nodes and namespace nodes
in that spec applies whether or not they have a parent.

Essentially the spec says that if you want to serialize anything other than
a document, you wrap a document node around it, as in
<xsl:document><xsl:copy-of select="$seq"/></xsl:document>; and this throws
an error if $seq contains attribute or namespace nodes.


Right. Already the sequence normalization algorithm throws appropriate exceptions for attributes and namespace nodes:

if (node instanceof Attribute) {
throw new XMLException(
"SE0001: W3C XQuery Serialization spec forbids top- level attributes");
}
if (node instanceof Namespace) {
throw new XMLException(
"SE0001: W3C XQuery Serialization spec forbids top- level namespaces");
}

I'm of course not wrapping a real document around the nodes; For efficiency I'm directly writing the normalized sequence via

private void write(Nodes nodes) throws IOException {
writeXMLDeclaration();

int size = nodes.size();
for (int i=0; i < size; i++) {
writeChild(nodes.get(i));
if (getIndent() > 0) breakLine();
}

flush();
}

Everything works like a charm here. (For parentless text nodes, I had to add the parent == null check in Serializer.isBoundaryWhitespace as discussed previously.)






Archive powered by MHonArc 2.6.24.

Top of Page