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 June 9th, 2008, 07:13 PM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default Replace an attribute with another attribute

I have an XML at the end of this post.
in this XML: I have
a variable: <variable fiscalyear="2008"/>
an attribute in tableheader element: "Actual PY YTD"

I want to create an XSLT to replace "Actual CY YTD" with "Actual 2008 YTD", is that possible?

Any suggestion is greatly appreciated. I am new to xslt, I know it is powerful, just have no clue how to do it.

Thanks very much!

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <variables><variable fiscalyear="2008"/></variables>
    <filters><filter name="" type="REPORT FACTS" value="User id: ABCDE"/><filter name="" type="REPORT FACTS" value="09.06.2008"/><filter name="Year" type="FILTER SETTINGS" value="2008"/><filter name="Month" type="FILTER SETTINGS" value="3"/></filters>
    <tableheaders><tableheader header01="Structure" header02="Actual PY YTD" header03="Actual CY YTD" header04="Budget CY YTD" join="1"/></tableheaders>
    <tabledata><row data01="PUR" data02="261.03479298" data03="249.45470803" data04="233.43961195" join="1"/><row data01="PCS" data02="168.31675563" data03="164.34726647" data04="167.99465816" join="1"/><row data01="CAS" data02="97.75732698" data03="99.46597209" data04="97.67908706" join="1"/><row data01="TPU" data02="7.29061817" data03="6.78124859" data04="6.95937657" join="1"/><row data01="IBC (NEW)" data02="219.55993426" data03="215.04789106" data04="220.54434795" join="1"/><row data01="Corporate" data02="3.89445554" data03="6.24318283" data04="7.47705739" join="1"/><row data01="Admin" data02="41.45681525" data03="39.32716022" data04="48.14665681" join="1"/><row data01="Other SU" data02="107.85191851" data03="87.38876944" data04="149.55014253" join="1"/></tabledata>
    <chartheaders><chartheader header01="PUR" header02="PCS" header03="CAS" header04="TPU" header05="IBC (NEW)" header06="Corporate" header07="Admin" header08="Other SU" join="1"/></chartheaders>
    <chartdata><row data01="Actual PY YTD" data02="261.03479298" data03="168.31675563" data04="97.75732698" data05="7.29061817" data06="219.55993426" data07="3.89445554" data08="41.45681525" data09="107.85191851" join="1"/><row data01="Actual CY YTD" data02="249.45470803" data03="164.34726647" data04="99.46597209" data05="6.78124859" data06="215.04789106" data07="6.24318283" data08="39.32716022" data09="87.38876944" join="1"/><row data01="Budget CY YTD" data02="233.43961195" data03="167.99465816" data04="97.67908706" data05="6.95937657" data06="220.54434795" data07="7.47705739" data08="48.14665681" data09="149.55014253" join="1"/></chartdata>
</result>



George Meng
__________________
George Meng
Reply With Quote
  #2 (permalink)  
Old June 9th, 2008, 07:28 PM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

OK, after reading the guide to post a new question, I think it will be good for me to change the question a little bit so it is easier for people to understand.

Sorry guys.

Here is the new question:

I have an xml:

<?xml version="1.0"?>
<result>
    <variables>
        <variable fiscalyear="2008"/>
    </variables>
    <tableheaders>
        <tableheader header02="Actual PY YTD" header03="Actual CY YTD" header04="Budget CY YTD"/>
    </tableheaders>
    <chartdata>
        <row data01="Actual PY YTD" data02="261.03479298" />
        <row data01="Actual CY YTD" data02="249.45470803" />
        <row data01="Budget CY YTD" data02="233.43961195" />
    </chartdata>
</result>

I want to create an XSLT to convert "Actual CY YTD" to "Actual 2008 YTD", where 2008 is in element variable as an attribute.

Here is the XSLT, you can see that currently I just copy the XML, no convert yet.

My question is how can I do that in XSLT: replace "CY" with "2008"?

<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>

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

    <xsl:template match="/result/variables">
        <xsl:copy>
            <xsl:copy-of select="variable"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/result/tableheaders">
        <xsl:copy>
            <xsl:copy-of select="tableheader"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/result/chartdata">
        <xsl:copy>
            <xsl:copy-of select="row"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Thanks very much!

George Meng
Reply With Quote
  #3 (permalink)  
Old June 10th, 2008, 02:27 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Somebody read the sticky post!
This is not my best ever effort but should start you off. It would be easier in XSLT 2.0 which allows user defined functions, in 1.0 you need to use named templates:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>
  <xsl:template match="result">
    <result>
      <xsl:apply-templates/>
    </result>
  </xsl:template>
  <xsl:template match="/result/variables">
    <xsl:copy>
      <xsl:copy-of select="variable"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/result/tableheaders">
    <xsl:copy>
      <xsl:copy-of select="tableheader"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/result/chartdata">
    <xsl:copy>
      <xsl:apply-templates select="row"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="row">
    <xsl:variable name="data01Replacement">
      <xsl:choose>
        <xsl:when test="contains(@data01, ' CY ')">
          <xsl:call-template name="replaceCY">
            <xsl:with-param name="cyText" select="@data01"/>
            <xsl:with-param name="year" select="/*/variables/variable/@fiscalyear"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@data01"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <row data01="{$data01Replacement}" data02="@data02"/>
  </xsl:template>

  <xsl:template name="replaceCY">
    <xsl:param name="cyText"/>
    <xsl:param name="year"/>
    <xsl:value-of select="substring-before($cyText, 'CY')"/>
    <xsl:value-of select="$year"/>
    <xsl:value-of select="substring-after($cyText, 'CY')"/>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #4 (permalink)  
Old June 10th, 2008, 02:55 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I would do this using the "modified identity template" coding pattern.

First write a template (the "identity template" that copies everything unchanged, recursively:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

Then override it with rules for things you want to change:

<xsl:template match="@*[.='Actual CY YTD']">
  <xsl:attribute name="{name()}">Actual 2008 YTD</xsl:attribute>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old June 10th, 2008, 10:51 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks Joe, that is what I need.

Quote:
quote:Originally posted by joefawcett
 Somebody read the sticky post!
This is not my best ever effort but should start you off. It would be easier in XSLT 2.0 which allows user defined functions, in 1.0 you need to use named templates:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>
  <xsl:template match="result">
    <result>
      <xsl:apply-templates/>
    </result>
  </xsl:template>
  <xsl:template match="/result/variables">
    <xsl:copy>
      <xsl:copy-of select="variable"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/result/tableheaders">
    <xsl:copy>
      <xsl:copy-of select="tableheader"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/result/chartdata">
    <xsl:copy>
      <xsl:apply-templates select="row"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="row">
    <xsl:variable name="data01Replacement">
      <xsl:choose>
        <xsl:when test="contains(@data01, ' CY ')">
          <xsl:call-template name="replaceCY">
            <xsl:with-param name="cyText" select="@data01"/>
            <xsl:with-param name="year" select="/*/variables/variable/@fiscalyear"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@data01"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <row data01="{$data01Replacement}" data02="@data02"/>
  </xsl:template>

  <xsl:template name="replaceCY">
    <xsl:param name="cyText"/>
    <xsl:param name="year"/>
    <xsl:value-of select="substring-before($cyText, 'CY')"/>
    <xsl:value-of select="$year"/>
    <xsl:value-of select="substring-after($cyText, 'CY')"/>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
George Meng
Reply With Quote
  #6 (permalink)  
Old June 10th, 2008, 10:54 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Jay,

I like you code, just one more question: Is there a way not hard code "2008" in your code?

<xsl:attribute name="{name()}">Actual 2008 YTD</xsl:attribute>

Thanks a lot!

Quote:
quote:Originally posted by mhkay
 I would do this using the "modified identity template" coding pattern.

First write a template (the "identity template" that copies everything unchanged, recursively:

<xsl:template match="@*|node()">
<xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

Then override it with rules for things you want to change:

<xsl:template match="@*[.='Actual CY YTD']">
<xsl:attribute name="{name()}">Actual 2008 YTD</xsl:attribute>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
George Meng
Reply With Quote
  #7 (permalink)  
Old June 10th, 2008, 10:56 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

Code:
<xsl:attribute name="{name()}"Actual <xsl:value-of select="/result/variables/variable/@fiscalyear"/> YTD</xsl:attribute>
/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #8 (permalink)  
Old June 10th, 2008, 10:58 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Michael,

I am so sorry, I type the wrong name.

Best Regards,

Quote:
quote:Originally posted by georgemeng
 Hi Jay,

I like you code, just one more question: Is there a way not hard code "2008" in your code?
&nbsp;
<xsl:attribute name="{name()}">Actual 2008 YTD</xsl:attribute>

Thanks a lot!

Quote:
quote:Originally posted by mhkay
 I would do this using the "modified identity template" coding pattern.

First write a template (the "identity template" that copies everything unchanged, recursively:

<xsl:template match="@*|node()">
<xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

Then override it with rules for things you want to change:

<xsl:template match="@*[.='Actual CY YTD']">
<xsl:attribute name="{name()}">Actual 2008 YTD</xsl:attribute>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
George Meng
George Meng
Reply With Quote
  #9 (permalink)  
Old June 10th, 2008, 11:04 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks you so much Sam, love you guys!

Best Regards!
Quote:
quote:Originally posted by samjudson
Code:
<xsl:attribute name="{name()}"Actual <xsl:value-of select="/result/variables/variable/@fiscalyear"/> YTD</xsl:attribute>
/- Sam Judson : Wrox Technical Editor -/
George Meng
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
Grouping by attribute aiyer0912 XSLT 1 November 13th, 2006 06:47 AM
attribute Eddy de Boer XSLT 5 June 16th, 2006 07:12 AM
all attribute kfir XML 1 May 8th, 2006 09:34 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
get attribute value... clayman58 XSLT 1 August 9th, 2005 04:33 AM



All times are GMT -4. The time now is 11:14 AM.


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