Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 14th, 2005, 06:16 AM
Registered User
 
Join Date: Jun 2003
Location: , , Netherlands.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using parameter for attributename

I wonder if it is possible to pass a parameter to a stylesheet and use this parameter as an attributename, like:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" encoding="utf-8" indent="yes" />
  <xsl:param name="environment" />

  <xsl:template match="/">
    <xsl:element name="rootelement">
      <xsl:apply-templates select="*[($environment = '1') or not ($environment)]" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="*[($environment = '1') or not ($environment)]">
    ... some transformation
  </xsl:template>

</xsl:stylesheet>

In the XML-document every element has an attribute, that has the name of the parameter passed to the stylesheet. It's value can be 0 or 1. Eventually I want to end up with only those elements, whose attribute $environment has a value of 1.

Many thanks in advance,
Raoul.

Reply With Quote
  #2 (permalink)  
Old January 14th, 2005, 06:42 AM
Authorized User
 
Join Date: Jul 2004
Location: Oxford, , United Kingdom.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How about

<xsl:apply-templates select="//*[@*[name()=$environment][.='1']]" />

Regards
Bryan

Reply With Quote
  #3 (permalink)  
Old January 14th, 2005, 10:37 AM
Registered User
 
Join Date: Jun 2003
Location: , , Netherlands.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, your reply helped me a little, but my new stylesheet is not that sufficient, i guess.
Here's my XML:

<?xml version="1.0" ?>
<users env_a="1" env_b="1" env_c="1" env_d="1" env_e="1">
  <user env_a="1" env_b="1" env_c="1" env_d="1" env_e="1" id="id1" name="name1">
    <auth env_a="1" env_b="1" env_c="1" env_d="1" env_e="1">part1</auth>
    <auth env_a="1" env_b="1" env_c="1" env_d="1" env_e="1">part2</auth>
    <auth env_a="1" env_b="1" env_c="1" env_d="1" env_e="1">part3</auth>
  </user>
  <user env_a="1" env_b="1" env_c="1" env_d="1" env_e="0" id="id2" name="name2">
    <auth env_a="1" env_b="1" env_c="1" env_d="0" env_e="0">part1</auth>
    <auth env_a="1" env_b="1" env_c="1" env_d="0" env_e="0">part2</auth>
    <auth env_a="1" env_b="1" env_c="1" env_d="0" env_e="0">part3</auth>
  </user>
</users>

And my stylesheet is the following:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" encoding="utf-8" indent="yes" />
  <xsl:param name="environment" />

  <xsl:template match="/">
    <xsl:element name="users">
      <xsl:apply-templates select="*[@*[name()=$environment][.='1']]" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="*">
    <xsl:if test="@*[name()=$environment][.='1']">
      <xsl:choose>
        <xsl:when test="name() = 'auth'">
          <xsl:element name="{name()}">
            <xsl:value-of select="." />
          </xsl:element>
        </xsl:when>
        <xsl:when test="name() = 'user'">
          <xsl:element name="{name()}" use-attribute-sets="User">
            <xsl:apply-templates select="auth[@*[name()=$environment][.='1']]" />
          </xsl:element>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:attribute-set name="User">
    <xsl:attribute name="id"><xsl:value-of select="@id" /></xsl:attribute>
    <xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute>
  </xsl:attribute-set>

</xsl:stylesheet>

I don't think it is neccessary to check value of the attributename passed to the stylesheet. Is there a more sufficient way?

Regards, Raoul.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter object malfunction - out parameter dash dev C# 2005 6 December 4th, 2007 12:58 PM
parameter umeshtheone Beginning VB 6 0 May 23rd, 2007 01:13 AM
another parameter drachx Crystal Reports 0 October 3rd, 2005 07:52 PM
Parameter shanib Crystal Reports 1 September 30th, 2005 02:35 AM
Parameter Bhavin Crystal Reports 4 September 9th, 2004 10:47 AM



All times are GMT -4. The time now is 02:31 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.