Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting data from nested XML elements

Status
Not open for further replies.

IS300

MIS
Oct 29, 2003
121
CA
I am trying to extract data that is in a nested XML elements.

Here is the original XML:
Code:
<cmrs>
   <timeBreakdown>
        <activityCodeName rowId="1600">22:RIG WATCH</activityCodeName> 
        <today rowId="1600" /> 
        <well rowId="1600" /> 
        <well rowId="1700" /> 
        <well rowId="1800" /> 
        <well rowId="1900" /> 
        <activityCodeName rowId="1a00">OTHER</activityCodeName> 
        <today rowId="1a00">6.0</today> 
        <well rowId="1a00">6.0</well> 
        <today rowId="ff00">6</today> 
        <well rowId="ff00">6.0</well> 
   </timeBreakdown>
</cmrs>

I can get all the data for <timebreakdown> by using

Code:
<xsl:template match="cmr">
    <cmr>
        <xsl:apply-templates select="timebreakdown"/>
    </cmr>

and then

Code:
<xsl:template match="today [@rowId='ff00']">
    <TotalHours><xsl:value-of select="." /></TotalHours>
</xsl:template>
so I only get the data inside <today rowID="ff00"> to put it in a new tag in my new document. However, I only want the data for <today rowId="ff00">, it spits the rest of the elements in side the <timebreakdown> tag.

Any help would be appreciated.

Thanks!
 
The code is applying templates to timebreakdown and all its children. If no template is specified for a particular node, the XSLT processor will return the default template, which will simply return the value of the node.

So, you should specify that you only want to apply templates to that particular node:
Code:
<xsl:apply-templates select="timeBreakdown/today[@rowId='ff00']"/>
Or you could specify a more general template for timeBreakdown:
Code:
<xsl:template match="timeBreakdown">
  <TotalHours><xsl:value-of select="today[@rowId='ff00']" /></TotalHours>
</xsl:template>
By the way, shouldn't:
Code:
<xsl:template match="cmr">
    <cmr>
        <xsl:apply-templates select="timebreakdown"/>
    </cmr>
be:
Code:
<xsl:template match="cmrs">
    <cmrs>
        <xsl:apply-templates select="timeBreakdown"/>
    </cmrs>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top