0

I have a xml drafted as follows

<node1>
  <node2>
    <node3>
      val3
    </node3>
    <node4>
      val4
    </node4>
    <node5>
     val5
    </node5>
    <node6>
      val6
    </node6>
  </node2>
</node1>

I m using xslt to loop over <node2> now I want to include values only for <node4> and <node5> in the results. What Im doing is

<xsl:for-each select="/node1/node2[.= node4 or .= node5]/*>
  <newNode>
    value of selected nodes
  </newNode>
</xsl:for-each>

have also tried

<xsl:for-each select="/node1/node2[name() = node4 or name() = node5]/*>
      <newNode>
        value of selected nodes
      </newNode>
    </xsl:for-each>

and

<xsl:for-each select="/node1/node2[.name()= node4 or .name()= node5]/*>
      <newNode>
        value of selected nodes
      </newNode>
    </xsl:for-each>

but I m getting values of all the 4 nodes i.e node3, node4, node5 and node6

can any one please put me in the right direction?

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
Gautam
  • 1,728
  • 8
  • 32
  • 67
  • You can refer http://stackoverflow.com/questions/1791108/xpath-expression-to-select-all-xml-child-nodes-except-a-specific-list. Or you can use an if condition inside the For loop to exclude those nodes – Rajan Panneer Selvam May 23 '13 at 04:58

2 Answers2

0

If you want to get all child of node2 except node4 and node5, you should try:

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

  <xsl:output method="xml"/>

  <xsl:template match="/">
    <xsl:for-each select="/node1/node2">
      <newNode>
        <xsl:apply-templates/>
      </newNode>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="node4[parent::node2]"/>
  <xsl:template match="node5[parent::node2]"/>

</xsl:stylesheet>

output:

<newNode>val3 val6</newNode>
Navin Rawat
  • 3,208
  • 1
  • 19
  • 31
-1

I myself got the answer without using templates The answer above may also be right but the following worked for me like a charm

<xsl:for-each select="/node1/node2/*[name()= node4 or name()= node5]>
      <newNode>
        value of selected nodes
      </newNode>
    </xsl:for-each>
Gautam
  • 1,728
  • 8
  • 32
  • 67
  • 1
    Your answer suggests that you are trying to avoid using templates at all costs. Anyone with XSLT experience would prefer a solution using templates, and avoid xsl:for-each at all (well, most) costs. – Michael Kay May 23 '13 at 07:33