Re: document() question

Subject: Re: document() question
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 15 Sep 1999 21:10:06 -0700
At 99/09/15 20:11 -0700, Terris wrote:
I have a string in a variable and I want to convert it
to a document via the document() function.

To express your desire without trying to immediately choose XSLT facilities, what I think you want to do is have the data in the stylesheet file.


Given the following example, it's obvious that I would
get an error because document() is treating the variable's contents
as a filename.

By definition of the document() function.


The definition of a variable/parameter without a select= attribute is a "result tree fragment", which is a fragment of markup destined only for the result tree. You cannot feed a result tree fragment into the engine as a source tree set of nodes.

I presume that I am trying to do the
impossible but I thought I would ask anyway.

Trying to feed a variable of rich markup to the document() function is impossible.


However ... getting data from the stylesheet isn't impossible and if that is what you want to do, an example is below.

In this example I have stopped using ID so that I can use the same id= attribute values in two places. I have invoked the engine twice, once with a default value and a second time with an argument.

Would something along these lines fit your requirement?

........... Ken

p.s. Note that a stylesheet writer does not have control over an XSLT engine's emission of namespace declarations

T:\ftemp>type doc2.xml
<BookList>
   <Book id="1"/>
   <Book id="2"/>
</BookList>

T:\ftemp>type list3.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";
                xmlns:data="any-uri">

<xsl:output method="xml" indent="yes"/>

<data:BookSet set="first">
  <Book id="1"><Name>The wizard of OZ</Name></Book>
  <Book id="2"><Name>Java Servlet Programming</Name></Book>
  <Book id="3"><Name>John Coltrane Rage</Name></Book>
</data:BookSet>

<data:BookSet set="second">
  <Book id="1"><Name>An Uninteresting Book</Name></Book>
  <Book id="2"><Name>Another Uninteresting Book</Name></Book>
  <Book id="3"><Name>Yet Another Uninteresting Book</Name></Book>
</data:BookSet>

 <!--source of data; default can be overridden on command line-->
<xsl:param name="source" select="'first'"/>

<xsl:template match="/BookList">          <!--document element-->
  <BookList>
    <xsl:for-each select="Book">
      <Book id="{@id}">
        <xsl:variable name="id" select="string(@id)"/>
         <!--note you cannot use document("")//Book[@id=$id]/*-->
        <xsl:for-each select='document("")'><!--the stylesheet-->
          <xsl:copy-of select="//data:BookSet[@set=$source]
                                /Book[@id=$id]
                                /*"/>
        </xsl:for-each>
      </Book>
    </xsl:for-each>
  </BookList>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt doc2.xml list3.xsl result1.xml

T:\ftemp>type result1.xml
<BookList xmlns:data="any-uri">
<Book id="1">
<Name xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>The wizard of OZ</Name>
</Book>
<Book id="2">
<Name xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>Java Servlet Programming</Name>
</Book>
</BookList>


T:\ftemp>xt doc2.xml list3.xsl result2.xml source=second

T:\ftemp>type result2.xml
<BookList xmlns:data="any-uri">
<Book id="1">
<Name xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>An Uninteresting Book</Name>
</Book>
<Book id="2">
<Name xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>Another Uninteresting Book</Name>
</Book>
</BookList>


T:\ftemp>

--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-01-2
Next instructor-led training:  1999-09-24, 1999-11-08, 1999-12-05/06,
                             1999-12-07, 2000-02-27/28, 2000-05-11/12



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread