By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

XSLT : Amend element value

P: 26
Hi,

i need to amend the value stored in the element in XSLT and this needs to be carried out with in a <template> code..

Please can you help me how to do this?

in the below code, in "GetTypeListValue" template call i need to amend the value stored in the input parameter (NodeName) with the value derived with in the template..

Thanks in adv for your help/comments

Cheer
Venu

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  >
  2.  
  3.     <xsl:template name="GetTypeListValue">
  4.         <xsl:param name="NodeName"/>
  5.         <xsl:copy>
  6.             <xsl:variable name="sInput" select="$NodeName"></xsl:variable>
  7.             <xsl:variable name="sTLValue">
  8.                 <xsl:choose>
  9.                     <xsl:when test="$sInput = '01'"><xsl:value-of  select="Description1"/></xsl:when>
  10.                     <xsl:when test="$sInput = '02'"><xsl:value-of  select="Description2"/></xsl:when>
  11.                 </xsl:choose>
  12.             </xsl:variable> 
  13.             <xsl:value-of select="$sTLValue"/>
  14.              <!-- i need to update the value stored $NodeName with the derived value $sTLValue here.. -->  
  15.  
  16.         </xsl:copy>
  17.     </xsl:template>
  18.  
  19.  
  20.     <xsl:template name="Get_CustomerDetail">
  21.         <xsl:call-template name="GetTypeListValue">
  22.             <xsl:with-param name="NodeName" select="/CompositionRequest/Request/Type"/>
  23.         </xsl:call-template>
  24.     </xsl:template>
  25.  
  26.     <!-- copy the data -->
  27.     <xsl:template match="*">
  28.         <xsl:copy>
  29.             <xsl:copy-of select="@*"/>
  30.             <xsl:apply-templates/>
  31.         </xsl:copy>
  32.     </xsl:template>
  33.  
  34. </xsl:stylesheet>
  35.  
Jun 18 '08 #1
Share this Question
Share on Google+
3 Replies


jkmyoung
Expert 100+
P: 2,057
What you're doing seems like a really roundabout way of accomplishing what you want. Could you post some source xml, and expected xml, highlighting the change? There's probably a better way to do this.

Where is your template even called?
Jun 18 '08 #2

P: 26
Hi,

thanks for looking at this.

i need to reuse the template to get the description for below node, hence i thought of calling the templae by passing the element-path as input parameter.
Please let me know if anything wrong here.

1) /CompositionRequest/Request/Type
2) /CompositionRequest/Request/Data/Type[@id='1']
3) /CompositionRequest/Request/Data/Type[@id='2']


Thanks for your help

Cheers
Venu

This is the sample XML as the original XML is big and i can't re-build the expected XML in the XSLT - hence i'm copying it and trying to amend selected element value.

Source XML (elements that needs to be changed are in bold)
Expand|Select|Wrap|Line Numbers
  1. <CompositionRequest>
  2.     <Request>
  3.         <Type>01</Type>
  4.         <Data>
  5.             <Type id='1'>02</Type>
  6.             <Type id='2'>03</Type>
  7.             <custname>Jones</custname>
  8.         </Data>
  9.         <Header>
  10.             <template_name>V8_1</template_name>
  11.             <version>000125</version>
  12.             <job_date>2008/06/10</job_date>
  13.             <job_id>12455252</job_id>
  14.         </Header>
  15.     </Request>
  16. </CompositionRequest>
  17.  
Expected XML (elements that needs to be changed are in bold)
Expand|Select|Wrap|Line Numbers
  1. <CompositionRequest>
  2.     <Request>
  3.         <Type>Description1</Type>
  4.         <Data>
  5.             <Type id='1'>Description2</Type>
  6.             <Type id='2'>Description3</Type>
  7.             <custname>Jones</custname>
  8.         </Data>
  9.         <Header>
  10.             <template_name>V8_1</template_name>
  11.             <version>000125</version>
  12.             <job_date>2008/06/10</job_date>
  13.             <job_id>12455252</job_id>
  14.         </Header>
  15.     </Request>
  16. </CompositionRequest>
  17.  
Jun 19 '08 #3

jkmyoung
Expert 100+
P: 2,057
Are you changing all Type nodes in general, or only the specified ones?
If it's all the type nodes, it would be simpler to have a 'Type' template.

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="Type">
  2.   <xsl:copy>
  3.      <xsl:choose>
  4.         <xsl:when test=". = '01'"><xsl:value-of  select="Description1"/></xsl:when>
  5.         <xsl:when test=". = '02'"><xsl:value-of  select="Description2"/></xsl:when>
  6.         <xsl:otherwise><xsl:value-of select="Description"/></xsl:otherwise>
  7.      </xsl:choose>
  8.   </xsl:copy>
  9. </xsl:template>
  10.  
Further, are you looking for nodes Description1, Description2, etc. or are you using the text "Description1"? If just the text, then add single quotes around it ('text') or get rid of the <xsl:value-of> part, so it is treated as text. eg
<xsl:otherwise>Description</xsl:otherwise>

--------

If it's only the xpath specified ones, it becomes a lot harder. You'd have to use an evaluate extension function to do this easily.
Jun 19 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.