XML/XSLT stylesheet/text
Содержание
Copy text with <xsl:text/>
<source lang="xml">
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> </source>
Get value with <xsl:text />
<source lang="xml">
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">
<xsl:apply-templates />
</xsl:template> <xsl:template match="publication"> <xsl:apply-templates /> </xsl:template> <xsl:template match="quote"> <xsl:text /> " <xsl:apply-templates /> " <xsl:text /> </xsl:template> <xsl:template match="work"> <xsl:apply-templates /> </xsl:template> <xsl:template match="role"> <xsl:apply-templates /> </xsl:template>
</xsl:stylesheet> Output: <html>
<body>
A B C D E F 1998 G " test " H
</body>
</html>
</source>
match an element and get text with text() function
<source lang="xml">
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">
Name | Position from sun | Diameter | Moons | Mean temp |
---|
</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"?>Name | Position from sun | Diameter | Moons | Mean temp |
---|---|---|---|---|
Venus | 2 | 12104 km (7505 miles) | 0 | 482C (900F) |
</source>
Start a new line with
<source lang="xml">
<?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> 1 2 </xsl:text> </xsl:template>
</xsl:stylesheet> Output:
1 2 </source>
Use text() function to get text
<source lang="xml">
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>
-
<xsl:for-each select="h2">
- <a> <xsl:attribute name="href"> #<xsl:value-of select="text()"/></xsl:attribute> <xsl:value-of select="text()"/> </a>
</xsl:for-each>
<xsl:apply-templates/> </body> </xsl:template> <xsl:template match="h2"> <a> <xsl:attribute name="name"><xsl:value-of select="text()"/></xsl:attribute>
<xsl:apply-templates/>
</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>
</source>