user2178319
user2178319

Reputation: 3

Retrieving text from XML node with XSLT

I’m sorry if this seems like an incredibly basic question, but I would really (and I mean really) appreciate any help I can get. I’m simply trying to do the following:
1. Replace the self-closing with
2. Grab the text from 'Second_node'
3. Store that text in a variable
4. Place that text inside in the new 'Seventh_node'.

I’ve accomplished step 1, but I can’t seem to retrieve the necessary information from the required element. I've included three examples below as well as my working XSLT. I guess the key problem is storing the text contents of 'Second_node' and placing it in the new element. By way of addition info, I’m using Saxon 6.5 for the transform. If the information provided is in anyway incomplete, please let me know.

THANK YOU!

The source XML:

<firstnode>
  <Second_node>text for second node</Second_node>
   <Third_node>
     <Fourth_node>
       <Fifth_node>text for fifth node</Fifth_node>
       <Sixth_node>text for sixth node</Sixth_node>
        <Seventh_node />
   </Fourth_node>
 </Third_node>
</firstnode>

What I have so far:

<firstnode>
  <Second_node>text for second node</Second_node>
   <Third_node>
     <Fourth_node>
       <Fifth_node>text for fifth node</Fifth_node>
       <Sixth_node>text for sixth node</Sixth_node>
        <Seventh_node></Seventh_node>
   </Fourth_node>
   </Third_node>
</firstnode>

What I need:

<firstnode>
  <Second_node>text for second node</Second_node>
   <Third_node>
     <Fourth_node>
       <Fifth_node>text for fifth node</Fifth_node>
       <Sixth_node>text for sixth node</Sixth_node>
        <Seventh_node>text for second node</Seventh_node>
   </Fourth_node>
   </Third_node>
</firstnode>

My XSLT so far:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>


<xsl:template match="Seventh_node">  
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>

        <xsl:text>text for second node</xsl:text>
    </xsl:copy>
</xsl:template>

Upvotes: 0

Views: 633

Answers (3)

panhandel
panhandel

Reputation: 674

You could try just placing the text from the second node into a variable, then use that variable to place that text further down in the seventh node.

<xsl:variable name="VAR_SecNode">
   <xsl:value-of select="Second_node"/>
</xsl:variable>

...
<Seventh_node><xsl:value-of select="$VAR_SecNode" /></Seventh_node>
...

Upvotes: 1

hr_117
hr_117

Reputation: 9627

I do not see the need for an variable.
Try this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Seventh_node">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:value-of select="//Second_node"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

The <xsl:apply-templates select="@*"/> will copy attribute of Seventh_node. The <xsl:apply-templates select="node()"/>will copy child nodes of Seventh_node. If you do not need that remove this lines.

Upvotes: 1

Matthew Green
Matthew Green

Reputation: 10391

You can update your second template to the following:

<xsl:template match="Seventh_node/text()">
    <xsl:text>text for second node</xsl:text>
</xsl:template>

This will match only the text of your Seventh_node and replace it with whatever you put in the <xsl:text> element.

Upvotes: 0

Related Questions