views:

32

answers:

2

I have a list of node ids. I want to append "-Selected" to all the text nodes within the given set of node ids. Please let me know how we can achieve the same using XSL?

Input:

<node1 id="a">
    <node2 id="b">
        <node3 id="c">Text node0</node3>
        <node4 id="d">
            <node5 id="e">Text node1</node5>
            <node6 id="f">Text node2</node6>
        </node4>
    </node2>
    <node7 id="g">Text node3
        <node8 id="h" align="center">Text node4</node8>
        <node9 id="i">Text node5</node9>
    </node7>
    <node10 id="j">Text node6
    </node10>
         <node11 id="h">Text node7
    </node11>  
</node1>

Input Param: List of node ids

<nodes> 
    <node>b</node> 
    <node>g</node> 
    <node>h</node> 
</nodes> 

If the input param has node 'a' then i need all the text nodes to be appended with selected.

Expected output:

<node1 id="a">
<node2 id="b">
    <node3 id="c">Text node0 - Selected<node3/>
    <node4 id="d">
        <node5 id="e">Text node1 - Selected</node5>
        <node6 id="f">Text node2 - Selected</node6>
    </node4>
</node2>
<node7 id="g">Text node3 - Selected
    <node8 id="h" align="center">Text node4 - Selected</node8>
    <node9 id="i">Text node5 - Selected</node9>
</node7>
<node10 id="j">Text node6
</node10>
 <node11 id="h">Text node7 - Selected
</node11>
 </node1>
+2  A: 

This XSLT 2.0 stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

    <xsl:param name="pNodes">
        <nodes>
            <node>b</node>
            <node>g</node>
            <node>h</node>
        </nodes>
    </xsl:param>

    <xsl:template match="text()" >
        <xsl:value-of select="." />
        <xsl:if test="ancestor::*[@id = $pNodes/*/*] and normalize-space()!=''">
            <xsl:text>  - Selected</xsl:text>
        </xsl:if>
    </xsl:template>

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

</xsl:stylesheet>

Uses a modified identity transform. The template match on text() has an if statement that evaluates whether the text node is a descendant of an element with an @id who's value matches one of the specified nodes, and that the text node is not all whitespace(you can adjust that as necessary, but otherwise). If it meets those criteria, it outputs " - selected".

Applied to the sample input it produces this output:

<?xml version="1.0" encoding="UTF-8"?><node1 id="a">
    <node2 id="b">
        <node3 id="c"/>
        <node4 id="d">
            <node5 id="e">Text node1  - Selected</node5>
            <node6 id="f">Text node2  - Selected</node6>
        </node4>
    </node2>
    <node7 id="g">Text node3
          - Selected<node8 id="h" align="center">Text node4  - Selected</node8>
        <node9 id="i">Text node5  - Selected</node9>
    </node7>
    <node10 id="j">Text node6
    </node10>
    <node11 id="h">Text node7
      - Selected</node11>  
</node1>
Mads Hansen
I am really sorry. I just edited by my input param to accept the ids of the element node
+3  A: 

This transformation:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:strip-space elements="*"/>

 <xsl:param name="pNodes">
    <nodes>
        <node>b</node>
        <node>g</node>
        <node>h</node>
    </nodes>
 </xsl:param>

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

 <xsl:template match="*[@id = $pNodes/*/*]//text()">
   <xsl:value-of select="concat(., ' Selected')"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML Document, produces the wanted, correct result.

Dimitre Novatchev