[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XSLT and Axis selection

Subject: RE: XSLT and Axis selection
From: cknell@xxxxxxxxxx
Date: Wed, 16 Jun 2004 11:04:33 -0400
xslt axis
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:   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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.