match an element and get text with text() function : text « XSLT stylesheet « XML

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XSLT stylesheet » text 
match an element and get text with text() function


File: Data.xml 

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt" ?>
<neighbours>
  <planet name="Venus">
    <description>
    Venus
    </description>
    <positionFromSun>2</positionFromSun>
    <diameter> 12104 km (7505 miles)</diameter>
    <moons> 0</moons>
    <meanTemp> 482(900F)</meanTemp>
    <oneDay> 243.01 earth days</oneDay>
    <oneYear> 224.7 earth days</oneYear>
  </planet>
</neighbours>

File: Transform.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="neighbours">
        <table border="1" id="planetsTable">
          <tr>
            <th>Name</th>
            <th>Position from sun</th>
            <th>Diameter</th>
            <th>Moons</th>
            <th>Mean temp</th>
          </tr>
          <xsl:apply-templates>
            <xsl:sort select="@name" order="ascending"/>
          </xsl:apply-templates>
        </table>
  </xsl:template>
  <xsl:template match="planet">
    <tr><td><xsl:value-of select="@name"/></td><xsl:apply-templates/></tr>
  </xsl:template>
  <xsl:template match="positionFromSun">
    <td><xsl:value-of select="text()"/></td>
  </xsl:template>
  <xsl:template match="diameter">
    <td><xsl:value-of select="text()"/></td>
  </xsl:template>
  <xsl:template match="moons">
    <td><xsl:value-of select="text()"/></td>
  </xsl:template>
  <xsl:template match="meanTemp">
    <td><xsl:value-of select="text()"/></td>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><table border="1" id="planetsTable"><tr><th>Name</th><th>Position from sun</th><th>Diameter</th><th>Moons</th><th>Mean temp</th></tr><tr><td>Venus</td><td>2</td><td> 12104 km (7505 miles)</td><td> 0</td><td> 482(900F)</td></tr></table>

 
Related examples in the same category
1. Start a new line with
2. Copy text with
3. Use text() function to get text
4. Get value with
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.