Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Printing node values in xsl:template

Hi,

I have created xsl:template called 'testText'

From my xml file I wanted to print details from TripSummary

within my main xsl:template <xsl:template match="TripHeader">

How can I print details in my xsl:template name="testText" from TripSummary node in my XML file

Please see my sample XSL and XSL file. I have deleted all un-necessary code and made XSL and XML allot shorter

Please guide

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="tripName"></xsl:param>

  <xsl:template match="TripDetails">
    <xsl:apply-templates select="TripHeader[@name = $tripName]"></xsl:apply-templates>
  </xsl:template>

  <xsl:template name="testText" match="/TripSummary">
    <xsl:text disable-output-escaping="yes">TripID:</xsl:text>
    <xsl:value-of select="." />
  </xsl:template>
 
  <xsl:template match="TripHeader">

    <xsl:call-template name="testText">
     
    </xsl:call-template>
 
  </xsl:template>

</xsl:stylesheet>



<?xml version="1.0" encoding="utf-8"?>
<TripDetails>
  <TripSummary>
    <TripID>2568</TripID>
    <TripName>Test Trip</TripName>
  </TripSummary>
 
  <TripHeader name="Trip Details" orderNo="1">
    <Label name="Short Description" isRequired="True" helpingText="" questionID="8">
      <Control type="Text Area (Multiple Line)" questionFormat="3" maxCount="1" displayCount="1" canAddMoreControls="False">
        <FieldValue visible="True"><![CDATA[Experience one of Peru's best treks and the delights of Cusco and the Sacred Valley]]></FieldValue>
      </Control>
    </Label>
    <Label name="Trip Summary" isRequired="True" helpingText="" questionID="41">
      <Control type="Text Area (Multiple Line)" questionFormat="3" maxCount="20" displayCount="2" canAddMoreControls="True">
        <FieldValue visible="True"><![CDATA[5 nights camping]]></FieldValue>
        <FieldValue visible="True"><![CDATA[9 nights hotels]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
        <FieldValue visible="False"><![CDATA[]]></FieldValue>
      </Control>
    </Label>
  </TripHeader>

</TripDetails>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
with call-template you pass the current context, that is why you were simply seeing everything (the current context at call time was TripHeader)
By explicitely passing the node you need, you get the effect you want.
Generally I would apply for the node though, and not use a named teplate, as you can see from the example below

Two other comments
- it is bad practice to give a template both a name and a match attribute, you need to choose
- try to avoid the use of disable-output-escaping, specially when it is not necessary
<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="tripName">Trip Details</xsl:param>
    
    <xsl:template match="TripDetails">
        <xsl:apply-templates select="TripHeader[@name = $tripName]"></xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="TripSummary/TripID">
        <xsl:text>TripID:</xsl:text>
        <xsl:value-of select="." />
    </xsl:template>
    
    <xsl:template match="TripHeader">
        <xsl:apply-templates select="../TripSummary/TripID"/>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

Avatar of tia_kamakshi

ASKER

Many Thanks for your help