Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 14th, 2005, 06:16 AM
Registered User
 
Join Date: Jun 2003
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.

 
Old January 14th, 2005, 06:42 AM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How about

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

Regards
Bryan

 
Old January 14th, 2005, 10:37 AM
Registered User
 
Join Date: Jun 2003
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.






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





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