Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 3rd, 2008, 04:02 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default loop through static list of values?

in xslt 1.0, using the following list of medical specialties below, how can i loop through this list and pass each specialty to a call-template function?

Allergy & Immunology
Anesthesiology
Behavioral Health
Cardiology

ive googled my question to some extent without any luck. the solutions ive found use tokenize() with a comma separated string variable, but this only works in xslt 2.0. is there a solution for xslt 1.0 that would solve my problem?

thanks in advance,
steve

 
Old January 3rd, 2008, 06:30 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 1.0 if you have structured data it's best to mark it up as XML, that is

<specialities>
  <speciality>Allergy &amp; Immunology</speciality>
  <speciality>Anesthesiology</speciality>
etc.

It's then easy to iterate over the list using path expressions, xsl:for-each, etc.

If you need to get it into this form by parsing text, there is always the str:tokenize template at www.exslt.org

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 3rd, 2008, 08:17 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for the reply michael...
Quote:
quote:Originally posted by mhkay
 In XSLT 1.0 if you have structured data it's best to mark it up as XML, that is

<specialities>
  <speciality>Allergy &amp; Immunology</speciality>
  <speciality>Anesthesiology</speciality>
etc.

It's then easy to iterate over the list using path expressions, xsl:for-each, etc.
in my XSLT, when i put the list of specialties into XML format, it ends up just outputing the specialties as text on my webpage. i already have an XML document (from a CMS) supplying my XSLT with all the data. i need to be able to use my static list of specialties from within my XSLT do accomplish the following (doesn't work):
Code:

<xsl:for-each select="specialties/specialty">
  <xsl:variable name="currentSpecialty" select="." />
  <xsl:if test="Collection/Hits/Content[Html/root/Specialty=$currentSpecialty]">
    <xsl:call-template name="specialtyHeader">
      <xsl:with-param name="specialtyHeader" select="$currentSpecialty" />
    </xsl:call-template>
    <xsl:for-each select="Collection/Hits/Content[Html/root/Specialty=$currentSpecialty]">
      <xsl:sort select="Html/root/SVRMCPhysician" order="descending" />
      <xsl:sort select="Html/root/LastName" order="ascending" />
      <xsl:call-template name="display"></xsl:call-template>
    </xsl:for-each>
  </xsl:if> 
</xsl:for-each>
Quote:
quote:Originally posted by mhkay


If you need to get it into this form by parsing text, there is always the str:tokenize template at www.exslt.org
i download the str:tokenize zip package from www.exslt.org and tried to make it work to no avail. it was way over my head :)

any help is greatly appreciated.
thanks,
steve

 
Old January 4th, 2008, 05:17 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Once you are inside the first for-each loop your context changes, so the next one should either refer to a previously defined variable, or back to the the root node I suspect:

<xsl:if test="/Condition/...">

or

<xsl:if test="$Condition/...">

/- Sam Judson : Wrox Technical Editor -/
 
Old January 4th, 2008, 05:18 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Oh, and add a simple template so your data isn't output, e.g.:

<xsl:template match="specialities"></xsl:template>


/- Sam Judson : Wrox Technical Editor -/
 
Old January 4th, 2008, 11:39 AM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 Once you are inside the first for-each loop your context changes, so the next one should either refer to a previously defined variable, or back to the the root node I suspect:

i tried storing my condition in a variable, but it didn't work.

it might help if i just posted my exact code below. what im really trying to do is simply just loop through a static list of values (each specialty):

my XML:
Code:
<Collection>
  <Hits count="1">
    <HitsMarker>*</HitsMarker>
    <Content>
      <Hits>1</Hits>
      <ID>5704</ID>
      <Title>Smith, Jane</Title>
      <QuickLink>/index.aspx?id=5704</QuickLink>
      <Teaser>
        <p>Allergy &amp; Immunology Anesthesiology Jane Smith Female No Select a State</p>
      </Teaser>
      <Html>
        <root>
          <Specialty>Allergy &amp; Immunology</Specialty>
          <Specialty>Anesthesiology</Specialty>
          <FirstName>Jane</FirstName>
          <LastName>Smith</LastName>
          <Gender>Female</Gender>
          <SVRMCPhysician>No</SVRMCPhysician>
          <Photo></Photo>
          <AddressLine1 />
          <AddressLine2 />
          <City />
          <State></State>
          <Zip />
          <Phone />
          <Fax />
          <Website />
          <PhysicianInformation></PhysicianInformation>
        </root>
      </Html>
    </Content>
    <Content>
      ...etc...
    </Content>
    <Content>
      ...etc...
    </Content>
  </Hits>
</Collection>
my XSLT
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">




    <AllSpecialties>
      <Specialty>Allergy &amp; Immunology</Specialty>
      <Specialty>Anesthesiology</Specialty>
      <Specialty>Behavioral Health Services</Specialty>
      <Specialty>Birthing &amp; Womens Services</Specialty>
      <Specialty>...etc...</Specialty>
    </AllSpecialties>

    <div style="border:1px #666666 solid;margin-left:10px">
      <table width="100%" border="0" cellpadding="5" cellspacing="0">




        <xsl:for-each select="AllSpecialties/Specialty">
          <xsl:variable name="currentSpecialty" select="." />
          <xsl:if test="Collection/Hits/Content[Html/root/Specialty=$currentSpecialty]">
            <xsl:call-template name="specialtyHeader">
              <xsl:with-param name="specialtyHeader" select="$currentSpecialty" />
            </xsl:call-template>
            <xsl:for-each select="Collection/Hits/Content[Html/root/Specialty=$currentSpecialty]">
              <xsl:sort select="Html/root/SVRMCPhysician" order="descending" />
              <xsl:sort select="Html/root/LastName" order="ascending" />
              <xsl:call-template name="display"></xsl:call-template>
            </xsl:for-each>
          </xsl:if> 
        </xsl:for-each>

      </table>
    </div>
  </xsl:template>


  <xsl:template name="display">
    <tr>
      <td valign="top" style="border-top:1px #666666 dotted;border-right:1px #666666 dotted">
        <a href="physician-contact-information.aspx?id={ID}"><xsl:value-of select="Html/root/FirstName" />#160;<xsl:value-of select="Html/root/LastName" /></a>
      </td>
      <td valign="top" style="border-top:1px #666666 dotted;border-right:1px #666666 dotted">
        <xsl:for-each select="Html/root/Specialty">
          <xsl:value-of select="." />
          <br />
        </xsl:for-each>
      </td>
      <td valign="top" style="border-top:1px #666666 dotted;border-right:1px #666666 dotted">
        <xsl:if test="normalize-space(Html/root/AddressLine1)">
          <xsl:value-of select="Html/root/AddressLine1" />
          <br />
        </xsl:if>
        <xsl:if test="normalize-space(Html/root/AddressLine2)">
          <xsl:value-of select="Html/root/AddressLine2" />
          <br />
        </xsl:if>
        <xsl:if test="(normalize-space(Html/root/City) or normalize-space(Html/root/State) or normalize-space(Html/root/Zip)) and (normalize-space(Html/root/State) != 'Select a State')">
          <xsl:value-of select="Html/root/City" />, <xsl:value-of select="Html/root/State" />#160;#160;<xsl:value-of select="Html/root/Zip" /><br />
        </xsl:if>
        <xsl:if test="normalize-space(Html/root/Phone)">
          <xsl:for-each select="Html/root/Phone">
            Phone: <xsl:value-of select="." /><br />
          </xsl:for-each>
        </xsl:if>
        <xsl:if test="normalize-space(Html/root/Fax)">
          <xsl:for-each select="Html/root/Fax">
            Fax: <xsl:value-of select="." /><br />
          </xsl:for-each>
        </xsl:if>
        <xsl:if test="normalize-space(Html/root/Website)">
          <xsl:for-each select="Html/root/Website">
            <xsl:value-of select="." />
            <br />
          </xsl:for-each>
        </xsl:if>
        #160;
      </td>
      <td valign="top" style="border-top:1px #666666 dotted">
        <xsl:value-of select="Html/root/Gender" />
      </td>
    </tr>
  </xsl:template>


  <xsl:template name="specialtyHeader">
    <xsl:param name="specialtyHeader" select="specialtyHeader"/>
    <tr>
      <td colspan="4" bgcolor="#CCCCCC" style="border-top:1px #888888 solid;border-bottom:1px #888888 solid"><strong><xsl:value-of select="$specialtyHeader"/></strong></td>
    </tr>
    <tr>
      <td style="border-right:1px #666666 dotted"><strong>Name</strong></td>
      <td style="border-right:1px #666666 dotted"><strong>Speciality</strong></td>
      <td style="border-right:1px #666666 dotted"><strong>Address</strong></td>
      <td><strong>Gender</strong></td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
im sure there's many other things wrong with what ive done in my code above, but im new to XSLT. does anyone know how i would loop through my static list of specialties?

thanks in advance,
steve

 
Old January 4th, 2008, 11:48 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to put data into your XSLT stylesheet then you can do so but you need to use a top-level element (child of the root element) and put your data element into a namespace. Then you can use document('') to access the data in the stylesheet e.g.
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dt="http://example.com/2008/mydata"
  exclude-result-prefixes="dt"
  version="1.0">

  <data xmlns="http://example.com/2008/mydata">
    <AllSpecialties xmlns="">
      <Specialty>Allergy &amp; Immunology</Specialty>
      <Specialty>Anesthesiology</Specialty>
      <Specialty>Behavioral Health Services</Specialty>
      <Specialty>Birthing &amp; Womens Services</Specialty>
      <Specialty>...etc...</Specialty>
    </AllSpecialties>
  </data>

  <xsl:variable name="sp"
            select="document('')/*/dt:data/AllSpecialties/Specialty"/>




Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to concatenate values in a loop LeoMathew XSLT 6 February 14th, 2008 09:03 AM
how to add static text in drop down list vinodonline2000 ASP.NET 2.0 Basics 10 August 16th, 2007 12:39 AM
How to loop for Select Values rohit_ghosh Excel VBA 3 May 8th, 2007 03:29 PM
have a table with static and dynamic values togeth hastikeyvan Dreamweaver (all versions) 29 December 4th, 2005 12:28 PM
loop values and text box values move mateenmohd Classic ASP Basics 2 April 5th, 2005 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.