XSLT : Amend element value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bharadwajrv
    New Member
    • May 2007
    • 26

    XSLT : Amend element value

    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 "GetTypeListVal ue" 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

    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  >
    
    	<xsl:template name="GetTypeListValue">
    		<xsl:param name="NodeName"/>
    		<xsl:copy>
    			<xsl:variable name="sInput" select="$NodeName"></xsl:variable>
    			<xsl:variable name="sTLValue">
    				<xsl:choose>
    					<xsl:when test="$sInput = '01'"><xsl:value-of  select="Description1"/></xsl:when>
    					<xsl:when test="$sInput = '02'"><xsl:value-of  select="Description2"/></xsl:when>
    				</xsl:choose>
    			</xsl:variable> 
    			<xsl:value-of select="$sTLValue"/>
    			[B] <!-- i need to update the value stored $NodeName with the derived value $sTLValue here.. -->  [/B]
    			
    		</xsl:copy>
    	</xsl:template>
    	
    	
    	<xsl:template name="Get_CustomerDetail">
    		<xsl:call-template name="GetTypeListValue">
    			<xsl:with-param name="NodeName" select="/CompositionRequest/Request/Type"/>
    		</xsl:call-template>
    	</xsl:template>
    	
    	<!-- copy the data -->
    	<xsl:template match="*">
    		<xsl:copy>
    			<xsl:copy-of select="@*"/>
    			<xsl:apply-templates/>
    		</xsl:copy>
    	</xsl:template>
    	
    </xsl:stylesheet>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    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?

    Comment

    • bharadwajrv
      New Member
      • May 2007
      • 26

      #3
      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) /CompositionRequ est/Request/Type
      2) /CompositionRequ est/Request/Data/Type[@id='1']
      3) /CompositionRequ est/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)
      Code:
      <CompositionRequest>
      	<Request>
      		[B]<Type>01</Type>[/B]
      		<Data>
      			[B]<Type id='1'>02</Type>[/B]
      			[B]<Type id='2'>03</Type>[/B]
      			<custname>Jones</custname>
      		</Data>
      		<Header>
      			<template_name>V8_1</template_name>
      			<version>000125</version>
      			<job_date>2008/06/10</job_date>
      			<job_id>12455252</job_id>
      		</Header>
      	</Request>
      </CompositionRequest>
      Expected XML (elements that needs to be changed are in bold)
      Code:
      <CompositionRequest>
      	<Request>
      		[B]<Type>Description1</Type>[/B]
      		<Data>
      			[B]<Type id='1'>Description2</Type>[/B]
      			[B]<Type id='2'>Description3</Type>[/B]
      			<custname>Jones</custname>
      		</Data>
      		<Header>
      			<template_name>V8_1</template_name>
      			<version>000125</version>
      			<job_date>2008/06/10</job_date>
      			<job_id>12455252</job_id>
      		</Header>
      	</Request>
      </CompositionRequest>
      Last edited by bharadwajrv; Jun 19 '08, 09:08 AM. Reason: additional info added

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        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.

        [code=xml]
        <xsl:template match="Type">
        <xsl:copy>
        <xsl:choose>
        <xsl:when test=". = '01'"><xsl:valu e-of select="Descrip tion1"/></xsl:when>
        <xsl:when test=". = '02'"><xsl:valu e-of select="Descrip tion2"/></xsl:when>
        <xsl:otherwise> <xsl:value-of select="Descrip tion"/></xsl:otherwise>
        </xsl:choose>
        </xsl:copy>
        </xsl:template>
        [/code]

        Further, are you looking for nodes Description1, Description2, etc. or are you using the text "Descriptio n1"? 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.

        Comment

        Working...