XML Tutorial/XSLT stylesheet/xml output
Версия от 18:22, 25 мая 2010; (обсуждение)
Содержание
Add new tags to xml
File: Data.xml
<name>
<last>A</last>
<first>W</first>
</name>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:output cdata-section-elements="notes" />
<xsl:template match="name">
<name>
<family>
<xsl:apply-templates select="last" />
</family>
<given>
<xsl:apply-templates select="first" />
</given>
<notes>& </notes>
</name>
</xsl:template>
</xsl:stylesheet>
Add new tags to xml document
File: Data.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<name>
<last>A</last>
<first>B</first>
</name>
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" indent="yes" encoding="UTF-8" />
<xsl:output version="1.1" />
<xsl:template match="name">
<name>
<family>
<xsl:apply-templates select="last" />
</family>
<given>
<xsl:apply-templates select="first" />
</given>
</name>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.1" encoding="UTF-8"?>
<name>
<family>A</family>
<given>B</given>
</name>
Convert xml structure
File: Data.xml
<?xml version="1.0"?>
<list xml:lang="en">
<title>title 1</title>
<listitem>item 1</listitem>
<listitem>item 2</listitem>
<listitem>item 3</listitem>
<listitem xml:lang="sw">item 4</listitem>
<listitem xml:lang="en-gb">item 5</listitem>
<listitem xml:lang="zu">item 6</listitem>
<listitem xml:lang="jz">item 7</listitem>
</list>
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" indent="yes" encoding="ISO-8859-1"/>
<xsl:template match="/">
<catalog>
<xsl:for-each select="/list/listitem">
<album>
<xsl:apply-templates/>
</album>
</xsl:for-each>
</catalog>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<album>item 1</album>
<album>item 2</album>
<album>item 3</album>
<album>item 4</album>
<album>item 5</album>
<album>item 6</album>
<album>item 7</album>
</catalog>
Output element with default namespace
File: Data.xml
<?xml version="1.0"?>
<message>You can use literal result elements in stylesheets.</message>
File: Transform.xslt
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform">
<output method="xml" indent="yes" />
<template match="/">
<msg xmlns="http://www.wbex.ru/msg">
<apply-templates
xmlns="http://www.w3.org/1999/XSL/Transform" />
</msg>
</template>
</stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<msg xmlns="http://www.wbex.ru/msg">You can use literal result elements in stylesheets.</msg>
output xml element with namespace
File: Data.xml
<?xml version="1.0"?>
<message>You can use literal result elements in stylesheets.</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:doc="http://www.wbex.ru/documents">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:element name="doc:paragraph">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<doc:paragraph xmlns:doc="http://www.wbex.ru/documents">You can use literal result elements in stylesheets.</doc:paragraph>
Output xml tags
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<root>
<first position="1">
<level1 children="0">This is level 1 of the nested elements</level1>
</first>
<second position="2">
<level1 children="1">
<level2>This is level 2 of the nested elements</level2>
</level1>
</second>
</root>
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="xml"/>
<xsl:template match="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><root><first><position>1</position><level1><children>0</children></level1></first><second><position>2</position><level1><children>1</children><level2/></level1></second></root>
Output xml with namespace
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<europe>
<states>
<state>Finland</state>
<state>Sweden</state>
<state>Iceland</state>
<state>Norway</state>
<state>Denmark</state>
</states>
</europe>
File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sc="http://www.wbex.ru/scand">
<xsl:output method="xml" indent="yes" encoding="ISO-8859-1" />
<xsl:template match="europe">
<xsl:apply-templates select="states" />
</xsl:template>
<xsl:template match="states">
<sc:states xmlns:scand="http://www.wbex.ru/states"
xmlns:nr="http://www.wbex.ru/states"
xsl:exclude-result-prefixes="scand nr">
<xsl:apply-templates select="state">
<xsl:sort />
</xsl:apply-templates>
</sc:states>
</xsl:template>
<xsl:template match="state">
<sc:country>
<xsl:value-of select="." />
</sc:country>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<sc:states xmlns:sc="http://www.wbex.ru/scand">
<sc:country>Denmark</sc:country>
<sc:country>Finland</sc:country>
<sc:country>Iceland</sc:country>
<sc:country>Norway</sc:country>
<sc:country>Sweden</sc:country>
</sc:states>
Restructure xml
File: Data.xml
<name>
<last>A</last>
<first>B</first>
</name>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="name">
<name>
<family>
<xsl:apply-templates select="last" />
</family>
<given>
<xsl:apply-templates select="first" />
</given>
</name>
</xsl:template>
</xsl:stylesheet>
Output:
<name>
<family>A</family>
<given>B</given>
</name>
Set cdata-section-elements
File: Data.xml
<name>
<last>A</last>
<first>W</first>
</name>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:output cdata-section-elements="notes" />
<xsl:template match="name">
<name>
<family>
<xsl:apply-templates select="last" />
</family>
<given>
<xsl:apply-templates select="first" />
</given>
<notes>& </notes>
</name>
</xsl:template>
</xsl:stylesheet>
Using xsl:element and xsl:attribute
File: Data.xml
<?xml version = "1.0"?>
<sports>
<game title = "cricket">
<id>243</id>
<para>
para 1
</para>
</game>
<game title = "baseball">
<id>431</id>
<para>
para 2
</para>
</game>
<game title = "soccer">
<id>123</id>
<para>
para 3
</para>
</game>
</sports>
File: Transform.xslt
<?xml version = "1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="sports">
<sports>
<xsl:apply-templates />
</sports>
</xsl:template>
<xsl:template match="game">
<xsl:element name="{@title}">
<xsl:attribute name="id">
<xsl:value-of select="id" />
</xsl:attribute>
<comment>
<xsl:value-of select="para" />
</comment>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><sports>
<cricket id="243"><comment>
para 1
</comment></cricket>
<baseball id="431"><comment>
para 2
</comment></baseball>
<soccer id="123"><comment>
para 3
</comment></soccer>
</sports>