XML/XSLT stylesheet/text

Материал из Web эксперт
Перейти к: навигация, поиск

Copy text with <xsl:text/>

File: Data.xml
<wine>
  <grape>A</grape>
  <brand>B</brand>
</wine>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="grape">
    <product>
      <xsl:value-of select="../brand" />
      <xsl:text/>
      <xsl:apply-templates />
    </product>
  </xsl:template>
  <xsl:template match="brand" />
</xsl:stylesheet>
Output:

  <product>BA</product>



Get value with <xsl:text />

File: Data.xml
<?xml version="1.0"?>
<cv>
  <para>
    <performance>
      <publication>A</publication>
      B
      <venue>C</venue>
      D
      <group>E</group>
      F
      <date>1998</date>
      G
      <quote>
        test
      </quote>
      H
    </performance>
  </para>
</cv>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="para">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>
  <xsl:template match="publication">
    <font face="arial">
      <xsl:apply-templates />
    </font>
  </xsl:template>
  <xsl:template match="quote">
    <xsl:text />
    "
    <xsl:apply-templates />
    "
    <xsl:text />
  </xsl:template>
  <xsl:template match="work">
    <i>
      <xsl:apply-templates />
    </i>
  </xsl:template>
  <xsl:template match="role">
    <u>
      <xsl:apply-templates />
    </u>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
        
      <p>
             
               <font face="arial">A</font>
               B
               C
               D
               E
               F
               1998
               G
               
             "
             
                 test
               
             "
             
               H
             
           
      </p>
      
   </body>
</html>



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> 482C (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> 482C (900F)</td></tr></table>



Start a new line with

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>&#xA; 1 &#xA; 2 &#xA; </xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:

 1 
 2



Use text() function to get text

File: Data.xml 
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt" ?>
<html>
  <head>
  <title>A simple HTML page</title>
  <style type="text/css">
    body { font-family: Verdana, Arial, sans-serif; font-size: 12px;}
  </style>
  </head>
</html>
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:output method="html" version="4.0" indent="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="body">
    <body>
      <ul>
        <xsl:for-each select="h2">
          <li>
            <a>
              <xsl:attribute name="href">
                #<xsl:value-of select="text()"/></xsl:attribute>
              <xsl:value-of select="text()"/>
            </a>
          </li>
        </xsl:for-each>
      </ul>
      <xsl:apply-templates/>
    </body>
  </xsl:template>
  <xsl:template match="h2">
    <a>
      <xsl:attribute name="name"><xsl:value-of select="text()"/></xsl:attribute>
      <h2>
        <xsl:apply-templates/>
      </h2>
    </a>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml-stylesheet type="text/xsl" href="Transform.xslt" ><html>
   
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      
      <title>A simple HTML page</title>
      <style type="text/css">
    body { font-family: Verdana, Arial, sans-serif; font-size: 12px;}
  </style>
      </head>
   
   
</html>