Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 0.94 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using XSL/XPath to match nodes with any name having a given attribute and child-node
  2. <SomeRandomNode>
  3.     <Type>SomeRandomType</Type>
  4.     <Child>
  5.       <Count type="int32">2</Count>      
  6.       <!-- This node should be matched -->
  7.       <Key id="5">
  8.         <Type>Identifier</Type>
  9.         <SomeValue type="string">Hello</SomeValue>
  10.         <SomeOtherValue type="string">World</SomeOtherValue>
  11.       </Key>
  12.     </Child>
  13.   </SomeRandomNode>
  14. </Project>
  15.        
  16. <xsl:template match="*[@id][.//Typename='Identifier']">
  17.   <xsl:copy>
  18.     <xsl:attribute name="id">
  19.       <xsl:value-of select="@id"/>
  20.     </xsl:attribute>
  21.  
  22.     <!-- Copy nodes -->
  23.     <xsl:copy-of select="Type" />
  24.     <xsl:copy-of select="SomeValue" />
  25.     <xsl:copy-of select="SomeOtherValue" />
  26.     <!-- Add new -->
  27.     <NewValue type="string">This node was added</NewValue>
  28.   </xsl:copy>
  29. </xsl:template>
  30.        
  31. *[@id][Type='Identifier']
  32.        
  33. *[@id][Type='Identifier']
  34.        
  35. *[@id and (Type='Identifier')]