Re: [xsl] Can unparsed-text() consume an in-memory XML document? Can a string version of an XML document be converted into XML?

Subject: Re: [xsl] Can unparsed-text() consume an in-memory XML document? Can a string version of an XML document be converted into XML?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 13 Aug 2012 16:55:10 -0400
Dear Roger,

I'll assume you are using XSLT 2.0.

On 8/13/2012 4:15 PM, Costello, Roger L. wrote:
I have an XSLT program that uses unparsed-text() to read in an XML
document from a URL, the program then does some string manipulations
on the string version of the XML document, and then outputs the
result.

Cool.


Question about the input to unparsed-text():

Instead of it consuming the XML document from a URL, can it consume
an in-memory XML document (i.e., one that has already been parsed;
say, a variable whose value is an XML document)?

If an XML document has been parsed and is now available as an XDM instance (a full-fledged tree, not a string), then it isn't subject to string manipulations as such, so no.


You could, however, manipulate any strings you derived from it, including values of text nodes or elements in the tree or indeed of the tree itself. Depending on what your operations are and whether they expect or require XML markup, this could be fine for you.

You could also reserialize the tree into XML (as a string bound to a variable or parameter) using XSLT to serialize it (see Evan Lenz at http://lenzconsulting.com/xml-to-string/ for some ideas on this), and then process that. (This is not an approach I would ordinarily recommend, but you might have a special case.)

Question about reconstituting an XML document from the string version
of the XML document:

I can output the string version of the XML document to a file and
then read the file back in as an XML document. But I'd like to skip
that intermediate step and go straight from the string version of the
XML document to a full-fledged XML document. Is there a way in XSLT
to convert a string to XML?

In theory it is possible to implement an XML parser in XSLT, although it won't be a trivial task. (Yet at Balisage I just demonstrated an XSLT pipeline to process LMNL syntax, which is almost as hard. See the Proceedings.)


Saxon has an extension function, saxon:parse, that will also do this, if you have Saxon-PE or EE.

Cheers,
Wendell

--
======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread