RE: [xsl] XSLT and Axis selection

Subject: RE: [xsl] XSLT and Axis selection
From: cknell@xxxxxxxxxx
Date: Wed, 16 Jun 2004 11:04:33 -0400
Never use "//" as part of an XPath expression unless you have a very good reason for doing so. In this case, you don't.

<xsl:for-each> is most often an artifact of a programmer who hasn't yet grasped the nature of functional programming. Look at this stylesheet. You will see that it consists of template matching and apply-templates elements. There is no need for for-each or call-template in the circumstances.

I don't say this as some sort of XSLT guru. I learned it after doing the same things you are doing (like most, my background is in procedural programming) and reading this list for a long time. Eventually the "push model" ingrained itself into my thinking.

Google for "push model" and "pull model" and "XSLT". Spend some time learning and thinking about when each is appropriate.
*******************************
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" indent="yes" encoding="UTF-8" />

  <xsl:param name="TransactionTotal" select="'2'" />

  <xsl:template match="/">
    <records>
    <xsl:apply-templates>
      <xsl:with-param name="TID" select="$TransactionTotal" />
    </xsl:apply-templates>
    </records>
  </xsl:template>

  <xsl:template match="records">
    <xsl:param name="TID" />
    <xsl:apply-templates>
      <xsl:with-param name="TID" select="$TID" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="record">
    <xsl:param name="TID" />
    <record>
    <xsl:apply-templates>
      <xsl:with-param name="TID" select="$TID" />
    </xsl:apply-templates>
    </record>
  </xsl:template>

  <xsl:template match="page">
    <xsl:param name="TID" />
    <page>
    <xsl:apply-templates>
      <xsl:with-param name="TID" select="$TID" />
    </xsl:apply-templates>
    </page>
  </xsl:template>

  <xsl:template match="id">
    <id><xsl:value-of select="." /></id>
  </xsl:template>

  <xsl:template match="*">
    <xsl:param name="TID" />
    <xsl:if test="name() = concat('data',$TID)"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:if>
  </xsl:template>

</xsl:stylesheet>
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     <ashushujev@xxxxxxxxxxxx>
Subject:  [xsl] XSLT and Axis selection


Hi,

Im puzzled with the following XSL transformation. I want to be able to
find out the next element name in XML. If it matches output the content
of that element.

XML:
<?xml version="1.0"?>
<records>
    <record>
        <page>
            <id>1</id>
            <data1>100</data1>
            <data2>Mr. Joe Bloggs</data2>
        </page>
    </record>
</records>


XSL:
<xsl:template name="TransactionLine">
	<xsl:param name="TransactionTotal">2</xsl:param>
	<xsl:if test="number($TransactionTotal) > 0">
		<xsl:call-template name="Transaction">
			<xsl:with-param name="TransactionID" select="$TransactionTotal"/>
		</xsl:call-template>
		<xsl:call-template name="TransactionLine">
			<xsl:with-param name="TransactionTotal"
select="number($TransactionTotal) - 1"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>
<xsl:template name="Transaction">
	<xsl:param name="TransactionID"/>
        <xsl:for-each select="//record/page/*">
	    <xsl:if test="following::node()[name()  concat('data',$TransactionID)">
                <xsl:value-of select="text()"/>
            </xsl:if>
        </xsl:for-each>
</xsl:template>

I would appreciate your comments.

Current Thread