• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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>
0
tia_kamakshi
Asked:
tia_kamakshi
  • 3
1 Solution
 
Geert BormansCommented:

<?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 name="testText">
        <xsl:param name="trip-sum"/>
        <xsl:text>TripID:</xsl:text>
        <xsl:value-of select="$trip-sum/TripID" />
    </xsl:template>
    
    <xsl:template match="TripHeader">
        
        <xsl:call-template name="testText">
            <xsl:with-param name="trip-sum" select="../TripSummary"/>
        </xsl:call-template>
        
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
Geert BormansCommented:
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

0
 
tia_kamakshiAuthor Commented:
Many Thanks for your help
0
 
Geert BormansCommented:
welcome
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now