[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Global, dynamically-named variables (or equiv) in XSL


Subject: Re: [xsl] Global, dynamically-named variables (or equiv) in XSL
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 19 Jun 2003 15:51:16 -0400

Mark,

The trick here is the relation between the XSD simpleType element, and the element that refers to it (the xs:element element). This relation is established by the fact that the @type attribute value on xs:element is the same as the @name attribute value on the corresponding xsl:simpleType element.

This kind of cross-reference relation is very conveniently managed with XSLT keys. So:

<xsl:key name="types-by-name" match="xs:simpleType | xs:complexType" use="@name"/>

establishes an XSLT key which you can use to retrieve any node in the document (here, any simpleType or complexType element) using an associated value (here, the value of its @name attribute).

Then when you match the xs:element element, you have a way of getting to the corresponding type declaration:

<xsl:template match="xs:element">
  <xsl:apply-templates select="key('types-by-name', @type)"/>
</xsl:template>

I hope this helps. This is pretty sketchy, since you didn't indicate what you want to do with your schema. But it gives you any easy way to do the traversal you need. (There is also a hard way using plain XPath, also less efficient.)

XSLT works quite differently from what you appear to be expecting. It's a declarative, "functional" language -- for example, the order of the templates in the stylesheet doesn't actually matter (hence, as you discovered, you can't bind a value to a variable in one template and have it available in a "later" template). In order to get it to work for you, you have to think not in terms of designing a process to get from here to there, but of declaring what output you want for a given input. You might find a bit of reading on the XSLT processing model to be very helpful.

Cheers,
Wendell

At 02:59 PM 6/19/2003, you wrote:

Hi,

I'm working with an XML Schema document (.xsd), and I'm having trouble
dealing with the idea of types and references to them.  My example xsd
(from w3schools) creates several named types, e.g.

<xs:simpleType name="stringtype">
 <xs:restriction base="xs:string"/>
</xs:simpleType>

which are then referenced elsewhere in the document like this:

...
  <xs:element name="orderperson" type="stringtype"/>
...

My plan of attack to handle this was to have a template like this, which
creates a global variable based on the the "name" attribute of the named
type:

  <xsl:template match="xs:simpleType|xs:complexType">
    <xsl:variable name="@name">
      <xsl:apply-templates/>
    </xsl:variable>
  </xsl:template>

This would process the children of the named type, and store the results
to a variable named (in the example) 'stringtype'.  A later template for
xs:element would look something like

<xsl:template match="xs:element">
  <xsl:value-of select="$@type"/>
</xsl:template>

The problem is that none of this works as I had hoped.  First off, it
seems like I can't declare a variable that is named dynamically (based on
the document being transformed); and second, I can't access that variable
because it goes out of scope when the xs:simpleType template finishes.

I keep thinking that I'm just missing something; is there an alternative
way to accomplish this?  Any attempts to set me on the right track would
be sorely appreciated.


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


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




Current Thread