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 May 29th, 2008, 02:18 PM
Authorized User
 
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default Get a node value using attribute name

Hi all,

I have an XML document that represents the rows returned in a result set. The records are represented as follows:
.
.
.
<RECORDS>
  <RECORD>
    <FIELD NAME="field1">A</FIELD>
    <FIELD NAME="field2">1</FIELD>
  </RECORD>
  <RECORD>
    <FIELD NAME="field1">B</FIELD>
    <FIELD NAME="field2">2</FIELD>
  </RECORD>
</RECORDS>
.
.
.
I'm trying to convert this to another XML document and I need to convert the node value from all fields whose NAME attribute is field2 (e.g., <FIELD NAME="field2">1</FIELD> here 1) to an attribute value in the resulting XML document to (hopefully) produce something like this:

<Data>
  <newField myFlag="1">A</newField>
  <newField myFlag="2">B</newField>
</Data>

I have a for-each loop over all RECORD elements wrapping a for-each over each FIELD node so the new elements are created in a fixed order:

<xsl:for-each select="//RECORD">
  <xsl:variable name="myFlag" select="FIELD[@NAME='field2']"/>

  <xsl:element name = "Data">
    <xsl:for-each select="FIELD">
      <xsl:variable name="fieldName" select="@NAME"/>
      <xsl:if test="$fieldName='field1'">
         <xsl:element name = "newField">
           <xsl:attribute name="myFlag"><xsl:value-of select="$myFlag"/></xsl:attribute>
           <xsl:value-of select="."/></xsl:element>
      </xsl:if>
    </xsl:for-each>
  </xsl:element>
</xsl:for-each>

I know setting the variable myFlag to FIELD[@NAME='field2'] isn't correct, but I can't find a way to get the value of the node whose attribute has a specific name.

Any help you can give would be greatly appreciated.

Thanks,
Tom
 
Old May 29th, 2008, 02:44 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're almost there. Try

<xsl:template match="RECORD">
  <newField myFlag="{FIELD[@NAME='field2']}">
    <xsl:value-of select="FIELD[@NAME='field1']"/>
  </newField>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 29th, 2008, 04:04 PM
Authorized User
 
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default

Just to be clear: Add this template to my stylesheet and replace all the code in the for-each over //RECORDS with <xsl:apply-templates select="."/> .

Is that correct?
 
Old May 29th, 2008, 04:12 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, yes, I should have realized from seeing all this for-each code that you weren't yet confident with using template rules.

You just need to add

<xsl:template match="RECORDS">
<Data>
  <xsl:apply-templates/>
</Data>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 29th, 2008, 04:28 PM
Authorized User
 
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default

That worked. Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert attribute to node-set using xalan Coolcampers XSLT 1 May 3rd, 2008 06:06 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
to get Distinct Node depending on attribute value chaitanyaXML XML 5 March 29th, 2005 10:26 AM
How can I get the element node of an attribute cdias XSLT 2 March 15th, 2004 10:34 AM





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