Don't output text nodes unless explicitly told to : parent « XSLT stylesheet « XML






Don't output text nodes unless explicitly told to


File: Data.xml

<wine grape="A">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1998</year>
  <desc>description</desc>
  <prices>
    <list>6.99</list>
    <discounted>5.99</discounted>
    <case>71.50</case>
  </prices>
</wine>


File: Transform.xslt

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

  <xsl:template match="text()" />
  <xsl:template match="list">
    ~~~~ Start of list element's template ~~~~ 1. List price
    (current node): {
    <xsl:apply-templates />
    } 2. Parent element (prices) contents: {
    <xsl:value-of select=".." />
    } 3. Grandparent element contents: {
    <xsl:value-of select="../.." />
    } 4. Attribute of grandparent: {
    <xsl:value-of select="../../@grape" />
    } 5. Sibling node {
    <xsl:value-of select="../discounted" />
    } 6. Uncle node {
    <xsl:value-of select="../../product" />
    } 7. Parent node's name: {
    <xsl:value-of select="name(..)" />
    } 8. Grandparent node's name: {
    <xsl:value-of select="name(../..)" />
    } ~~~~ End of list element's template ~~~~
  </xsl:template>
  

</xsl:stylesheet>

Output:


    ~~~~ Start of list element's template ~~~~ 1. List price
    (current node): {
    
    } 2. Parent element (prices) contents: {
    
    6.99
    5.99
    71.50
  
    } 3. Grandparent element contents: {
    
  shop 1
  product 1
  1998
  description
  
    6.99
    5.99
    71.50
  

    } 4. Attribute of grandparent: {
    A
    } 5. Sibling node {
    5.99
    } 6. Uncle node {
    product 1
    } 7. Parent node's name: {
    prices
    } 8. Grandparent node's name: {
    wine
    } ~~~~ End of list element's template ~~~~
  

 








Related examples in the same category

1.Use .. to indicate level
2.Get sibling with ../
3.Select element out of parent tag