XML/XSLT stylesheet/stylesheet
Содержание
Embedded style sheet into xml document
<?xml version="1.0" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="#id(transcriptstyle)"?>
<transcript>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
id="transcriptstyle">
<xsl:template match="/">
<html>
<xsl:apply-templates />
</html>
</xsl:template>
<xsl:template match="report">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="order">
<P>
<xsl:value-of select="." />
</P>
</xsl:template>
</xsl:stylesheet>
<student id="STU12345" name="name 1" status="active">
<home_address>35 Wall Street, Wonderland, NJ</home_address>
<interests>
<interest>interest 1</interest>
<interest>interest 2</interest>
<interest>interest 3</interest>
</interests>
</student>
<term>
<heading name="Winter 1999" />
<course>
<course-name>course 1</course-name>
<grade>A-</grade>
<credits>4</credits>
</course>
<course>
<course-name>course 2</course-name>
<grade>B+</grade>
<credits>3</credits>
</course>
</term>
<summary>summary</summary>
<comments>
comments
</comments>
</transcript>
Empty transform
File: Data.xml
<?xml version="1.0"?>
<people>
<person born="1912" died="1954">
<name>
<first_name>A</first_name>
<last_name>B</last_name>
</name>
<profession>C</profession>
<profession>D</profession>
<profession>E</profession>
</person>
<person born="2008" died="2008">
<name>
<first_name>F</first_name>
<middle_initial>G</middle_initial>
<last_name>H</last_name>
</name>
<profession>I</profession>
<hobby>J</hobby>
</person>
</people>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
Generate html tags
File: Data.xml
<?xml version="1.0"?>
<greeting>
Hello, World!
</greeting>
File: Transform.xslt
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="greetings/greeting"/>
</body>
</html>
</xsl:template>
<xsl:template match="greeting">
<h1>
<xsl:value-of select="."/>
</h1>
</xsl:template>
</xsl:stylesheet>
Generate Java code
File: Data.xml
<?xml version="1.0"?>
<greeting>
Hello, World!
</greeting>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>
public class Greeting
{
public static void main(String[] argv)
{
</xsl:text>
<xsl:apply-templates select="greeting"/>
<xsl:text>
}
}
</xsl:text>
</xsl:template>
<xsl:template match="greeting">
<xsl:text>System.out.println("</xsl:text>
<xsl:value-of select="normalize-space()"/>
<xsl:text>");</xsl:text>
</xsl:template>
</xsl:stylesheet>
Generate SVG document
File: Data.xml
<?xml version="1.0"?>
<greeting>
Hello, World!
</greeting>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
doctype-public="-//W3C//DTD SVG 20001102//EN"
doctype-system="http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd"/>
<xsl:template match="/">
<svg width="10cm" height="4cm">
<g>
<defs>
<radialGradient id="MyGradient"
cx="4cm" cy="2cm" r="3cm" fx="4cm" fy="2cm">
<stop offset="0%" style="stop-color:red"/>
<stop offset="50%" style="stop-color:blue"/>
<stop offset="100%" style="stop-color:red"/>
</radialGradient>
</defs>
<rect style="fill:url(#MyGradient); stroke:black"
x="1cm" y="1cm" width="8cm" height="2cm"/>
<text x="5.05cm" y="2.25cm" text-anchor="middle"
style="font-family:Verdana; font-size:24;
font-weight:bold; fill:black">
<xsl:apply-templates select="greeting"/>
</text>
<text x="5cm" y="2.2cm" text-anchor="middle"
style="font-family:Verdana; font-size:24;
font-weight:bold; fill:white">
<xsl:apply-templates select="greeting"/>
</text>
</g>
</svg>
</xsl:template>
<xsl:template match="greeting">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Transform another style sheet
File: Data.xml
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:element name="paragraph">
<xsl:attribute name="priority">medium</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:element name="paragraph">
<xsl:attribute name="priority">medium</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph priority="medium">
medium
</paragraph>