Re: [xsl] How to select a document element from the input xml file using xslt?

Subject: Re: [xsl] How to select a document element from the input xml file using xslt?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 12 Jul 2004 17:26:35 -0400
Alla,

I'm afraid you may have confused your friendly instructors by referring to a "document element". Reading your post, I infer what you mean by this is the DOCTYPE declaration, i.e. that thing at the top of your input file looking like

<!DOCTYPE element SYSTEM "http://delete.dtd";>

... but this isn't an element at all, much less the "document element" which is something else entirely (it's the element at the top level of your document, the one that contains everything else, that is whose end tag appears at the end of the document).

Jumping ahead -- as you've found, you can control what your processor writes as a DOCTYPE declaration in xsl:output -- but only up to a point. What you *can't* do (as you found) is parameterize this control or make it dynamic in any way, by (for example) wrapping it in an xsl:choose. This is because it is not really an XSLT instruction, but only a literal value the processor hands to the serializer to use when it writes the output file.

More control over this will be possible in XSLT 2.0, but in the meantime the way people handle this is by using stylesheets to write stylesheets. The first stylesheet writes a stylesheet, which is then executed in a subsequent pass. This is what the posts responding so far have been getting at: since you can't control this string directly, you do it indirectly by creating the entire stylesheet dynamically (!).

But I think this is overkill for you. Rather, I think you'll do better to handle your conditional logic outside the stylesheet altogether. Have three stylesheets:

templates.xsl contains all your templates.

deletedoc.xsl contains (only) the following:

<xsl:include href="templates.xsl"/>
<xsl:output method="xml" doctype-system="http://delete.dtd"/>

and updatedoc.xsl contains the same, only invoking update.dtd.

Then you need only to implement logic in the calling environment to call deletedoc.xsl for "delete" documents, and update.xsl for "update" documents, and you're there. (How you do this will depend, of course.)

This is the XSL-orthodox way to do it: XSLT is designed on the assumption that the host system knows how to delegate each type of document to its own proper stylesheet(s), and doesn't mandate (or include) any way to manage that delegation itself. But by using xsl:include, you manage to share templates (and maintenance) between what are formally two different stylesheets. (You could also use xsl:import.)

If this isn't sufficient, please say why and we'll try to clear it up further.

Cheers,
Wendell

At 03:10 PM 7/12/2004, you wrote:
Hi Alla,

Your implementation is read by the xslt processor and when it comes across <xsl:output... it doesnt know that you want it to output <xsl:output... but instead assumes you are declaring the <xsl:output../> element. As you probably already know the <xsl:output element must be a direct child of <xsl:stylesheet...> and cannot be inside of a template or anywhere after the first xsl:template element is declared... The way to accomplish what you are trying to do is to use the <xsl:element name="xsl:output"> and then add the necessary attributes using <xsl:attribute name="method">xml</xsl:attribute> etc...

Hope this helps!

Belkin, Alla wrote:
Hello,
I am using a stylesheet for modifications of the input xml file to the
different format of the xml file.
My output file should have a document element, the same as in the input
file. How to copy a document element? I can have input XMLs with
different document elements. I tried to do something like this, but it
doesn't work:
<xsl:choose>
        <xsl:when test="count(//Delete)  &gt; 0">
                <xsl:output method="xml"
doctype-system="http://delete.dtd"/>
        </xsl:when >
        <xsl:when test="count(//Delete) = 0">
                <xsl:output method="xml"
doctype-system="http://update.dtd"/>
        </xsl:when>
</xsl:choose>
Please, help
Thank you,
Alla Belkin


======================================================================
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