Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] XInclusion produdes invalid document

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Andrew Thompson <lordpixel AT mac.com>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] XInclusion produdes invalid document
  • Date: Mon, 7 Mar 2005 00:07:41 -0500


On Mar 6, 2005, at 3:45 PM, Elliotte Harold wrote:

You could write a post-process that stripped the xml:base attributes. It would be easiest to do this using XSLT or (in XOM 1.1) XPath:

Nodes nodes = doc.query("//*[@xml:base]");
for (int i = 0; i < nodes.size(); i++) {
Element e = (Element) node;
e.removeAttribute("xml:base", Namespace.XML_NAMESPACE);
}


So for XOM 1.0 I ended up writing this function:

private Document stripXMLBaseAttributes(Document a_document)
throws ParsingException, XSLException, IOException {
String stripBase =
"<?xml version=\"1.0\"?>" +
"<xsl:stylesheet version=\"1.0\" " +
" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\";>" +
" <xsl:output method=\"xml\" indent=\"yes\"/>" +
" <xsl:template match=\"@*|node()\">" +
" <xsl:copy>" +
" <xsl:apply-templates select=\"@*|node()\"/>" +
" </xsl:copy>" +
" </xsl:template>" +
"</xsl:stylesheet>";
XSLTransform baseStripper = new XSLTransform(new Builder().build(new StringReader(stripBase)));
return XSLTransform.toDocument(baseStripper.transform(a_document));
}

Interestingly enough this works... I think because it's completely non-namespace aware and so xml:base gets stripped off by default. So I decided that might be a little fragile and tried to write a stylesheet that would actually preserve the XML base attribute, eg, something like:


"<?xml version=\"1.0\"?>" +
"<xsl:stylesheet version=\"1.0\" " +
" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""; +
" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\";>" +
" <xsl:output method=\"xml\" indent=\"yes\"/>" +
" <xsl:template match=\"@*|@xml:*|node()\">" +
" <xsl:copy>" +
" <xsl:apply-templates select=\"@*|@xml:*|node()\"/>" +
" </xsl:copy>" +
" </xsl:template>" +
"</xsl:stylesheet>";

But that doesn't work, nor does any variant I could think of. The xml:base always seems to be dropped?
So is it even possible to write a true XSLT identity transform in the presence of arbitrary namespaces?

I was also wondering if it was possible to adjust the stylesheet so that:

<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml"; xmlns:xi="http://www.w3.org/2001/XInclude";>

becomes

<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml";>

But I couldn't find a way to do it. There's no way to match xmlns:xi in a stylesheet that I can find.

Actually, it's very interesting that the stylesheet above (the first one) DOES copy xml:lang, but it doesn't copy xml:base.
Now, I'm convinced I don't know what's going on...

AndyT (lordpixel - the cat who walks through walls)
A little bigger on the inside

(see you later space cowboy ...)





Archive powered by MHonArc 2.6.24.

Top of Page