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 December 16th, 2009, 01:26 AM
Registered User
 
Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Filtering xsl:value-of on attributes

Hi,

I currently have an XML document like this:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="./transform.xsl"?>
<Documentation><Tables>
<TABLE TABLE_NAME="FirstTable">
  <COLUMN COLUMN_NAME="FirstColumn" ORDINAL_POSITION="1" IS_NULLABLE="NO" DATA_TYPE="int" />
  <COLUMN COLUMN_NAME="SecondColumn" ORDINAL_POSITION="2" IS_NULLABLE="NO" DATA_TYPE="int" />
</TABLE>

...

</Tables><Comments>
<COMMENT ForTable="FirstTable" ForColumn="FirstColumn">Description of FirstColumn</COMMENT>
</Comments></Documentation>
My question is, how do I get the value of the COMMENT when looping over the tables and columns? I have:

Code:
<xsl:for-each select="//TABLE">
  ...
  <xsl:for-each select="COLUMN">
    ...
    <xsl:value-of select="//COMMENT[@ForTable = @TABLE_NAME and @ForColumn=@COLUMN_NAME]" />
But this doesn't work. Any advice?
 
Old December 16th, 2009, 05:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The thing you have to remember if context. When you are filtering a COMMENT element in a predicate (the square brackets) you are within the context of that COMMENT element, so @ForTable is a valid attribute, but @TABLE_NAME does not exist on a COMMENT element.

You can refer to the XSLT function current() which returns the current context node from outside of the predicate (in this example that would be the COLUMN element).

Code:
<xsl:value-of select="//COMMENT[@ForTable = current()/../@TABLE_NAME AND @ForColumn=current()/@COLUMN_NAME]"/>
It may be easier to read/follow if you create temporary variables however:

Code:
<xsl:for-each select="TABLE">
  <xsl:variable name="tableName" select="@TABLE_NAME"/>
  <xsl:for-each select="COLUMN">
     <xsl:variable name="columnName" select="@COLUMN"/>
     <xsl:value-of select="//COMMENT[@ForTable=$tableName AND @ForColumn=$columnName]"/>
  ...
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 16th, 2009, 07:33 PM
Registered User
 
Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
Code:
<xsl:value-of select="//COMMENT[@ForTable = current()/../@TABLE_NAME AND @ForColumn=current()/@COLUMN_NAME]"/>
Thank you. This is the solution I ended up using.





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:function pass param to attribute of xsl:instruction bonekrusher XSLT 4 March 31st, 2009 09:06 AM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
Using xsl:if test attributes rdavisiii XSLT 4 November 9th, 2004 12:24 AM
XSL Grouping and Filtering xrow XSLT 1 October 13th, 2004 11:12 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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