Re: Do you know how to refer to a node?

Subject: Re: Do you know how to refer to a node?
From: Steve Tinney <stinney@xxxxxxxxxxxxx>
Date: Sun, 19 Dec 1999 21:00:16 -0500
Sorry, I realized later that you were not in the node-and-text
problem camp, but that you wanted to rename the nodes according
to a fixed sequence.  Here is a neat way of doing that, 
exploiting the fact that document('') refers to the stylesheet 
document.

 Steve

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:strip-space elements="nodes"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="customer">
  <xsl:copy>
    <xsl:for-each select="*">
      <xsl:variable name="index" select="position()"/>
      <xsl:element name="{document('')//field[$index]}">
        <xsl:value-of select="normalize-space(.)"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

<xsl:template name="fieldnames">
  <field>street</field>
  <field>city</field>
  <field>state</field>
</xsl:template>

</xsl:stylesheet>

produces:

<?xml version="1.0" encoding="utf-8"?>
<customer><street>1 Main
Street</street><city>Pittsburgh</city><state>PA</state></customer>

from:

<customer>
 <node>1 Main Street</node>
 <node>Pittsburgh</node>
 <node>PA</node>
</customer>


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


Current Thread