XML/XSLT stylesheet/xml output
Содержание
- 1 achieves in a different and laborious way.
- 2 <xsl:value-of select="."/>
- 3 Header1
- 3.1 Header3
- 3.2 Add 1 hyphen to result tree
- 3.3 Add Comment to generated xml document
- 3.4 Add indenting to make result line up better
- 3.5 Add new parent tag
- 3.6 Add processing-instruction to generated xml document
- 3.7 attribute name="example" select="1 to 7" separator=", "
- 3.8 Create new element with attribute
- 3.9 Create new xml document with namespace
- 3.10 Disable escape
- 3.11 disable-output-escaping="yes"
- 3.12 output doctype-system="testOut.dtd"
- 3.13 Output element with Namespace
- 3.14 output method="xml" omit-xml-declaration="yes"
- 3.15 output method="xml" version="1.1" encoding="utf-16"
- 3.16 Output new xml tag
- 3.17 Output xml element in various form
- 3.18 Rename tag
- 3.19 Restructure xml document
- 3.20 Set Encoding
- 3.21 Use attribute value to create new attribute
- 3.22 Use concat function to create name of Element
- 3.23 use for-each to output xml tags
- 3.24 use new tag to wrap
- 3.25 Using xsl:element and xsl:attribute
- 3.26 Using xsl:element and xsl:attribute to create xml tags
- 3.27 xsl:attribute generates elements
- 3.28 xsl:element generates elements in time of processing.
- 4 Header1
achieves in a different and laborious way.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<text size="H1">Header1</text> <text size="H3">Header3</text> <text size="b">Bold text</text> <text size="sub">Subscript</text> <text size="sup">Superscript</text>
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="/"> <xsl:for-each select="//text"> <xsl:choose> <xsl:when test="@size="H1"">
<xsl:value-of select="."/>
</xsl:when> <xsl:when test="@size="H3"">
<xsl:value-of select="."/>
</xsl:when> <xsl:when test="@size="b""> <xsl:value-of select="."/> </xsl:when> <xsl:when test="@size="sub""> <xsl:value-of select="."/> </xsl:when> <xsl:when test="@size="sup""> <xsl:value-of select="."/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>Header1
Header3
Bold textSubscriptSuperscript</source>
Add 1 hyphen to result tree
<source lang="xml">
File: Data.xml <sample>test</sample> 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 name="hyphens"> <xsl:param name="howMany">1</xsl:param> <xsl:if test="$howMany > 0"> <xsl:text>-</xsl:text> <xsl:call-template name="hyphens"> <xsl:with-param name="howMany" select="$howMany - 1" /> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template match="sample"> Print 1 hyphen: <xsl:call-template name="hyphens" /> Print 3 hyphens: <xsl:call-template name="hyphens"> <xsl:with-param name="howMany" select="3" /> </xsl:call-template> Print 20 hyphens: <xsl:call-template name="hyphens"> <xsl:with-param name="howMany" select="20" /> </xsl:call-template> Print 0 hyphens: <xsl:call-template name="hyphens"> <xsl:with-param name="howMany" select="0" /> </xsl:call-template> </xsl:template>
</xsl:stylesheet> Output:
Print 1 hyphen: - Print 3 hyphens: --- Print 20 hyphens: -------------------- Print 0 hyphens: </source>
Add Comment to generated xml document
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <message>You can insert comments in your output.</message> 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:comment>comment & msg element</xsl:comment> <msg> <xsl:apply-templates /> </msg> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <msg>You can insert comments in your output.</msg>
</source>
Add indenting to make result line up better
<source lang="xml">
File: Data.xml <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="figure/title"> <xsl:apply-templates /> </xsl:template> <xsl:template match="para" />
</xsl:stylesheet>
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="chapter"> Pictures: <xsl:for-each select="descendant::figure"> <xsl:value-of select="title" /> <xsl:text> </xsl:text> </xsl:for-each> Chapter: <xsl:apply-templates /> </xsl:template>
</xsl:stylesheet>
</source>
Add new parent tag
<source lang="xml">
File: Data.xml <customer>
<last>Hill</last> <first>Phil</first> <phone>212-555-1212</phone> <address>166 Main St.</address> <city>New York</city> <state>NY</state> <zip>10001</zip>
</customer> 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="customer"> <client> <xsl:apply-templates /> </client> </xsl:template>
</xsl:stylesheet> Output: <client>
Hill Phil 212-555-1212 166 Main St. New York NY 10001
</client>
</source>
Add processing-instruction to generated xml document
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <message>test</message> 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:processing-instruction name="xml-stylesheet"> href="processing.css" type="text/css" </xsl:processing-instruction> <xsl:element name="doc"> <xsl:element name="paragraph"> <xsl:apply-templates /> </xsl:element> </xsl:element> </xsl:template> <xsl:template match="courier"> <xsl:element name="code"> <xsl:apply-templates /> </xsl:element> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="processing.css" type="text/css"
?><doc> <paragraph>test</paragraph>
</doc>
</source>
attribute name="example" select="1 to 7" separator=", "
<source lang="xml">
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" encoding="utf-8"?> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="yes"/> <xsl:template match="/"> <sampledoc> <xsl:attribute name="example" select="1 to 7" separator=", "/> </sampledoc> </xsl:template>
</xsl:stylesheet> Output: <sampledoc example="1, 2, 3, 4, 5, 6, 7"/>
</source>
Create new element with attribute
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet href="Transform.xslt" type="text/xsl"?> <message>Message.</message> 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:attribute name="date">2003-09-23</xsl:attribute> <xsl:attribute name="doc:style" namespace="http://www.wbex.ru/documents">classic</xsl:attribute> <xsl:apply-templates /> </xsl:element> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <paragraph xmlns:doc="http://www.wbex.ru/documents" priority="medium" date="2003-09-23"
doc:style="classic">Message.</paragraph> </source>
Create new xml document with namespace
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet href="Transform.xslt" type="text/xsl"?> <message>Message.</message> File: Transform.xslt <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:attribute-set name="paragraph"> <xsl:attribute name="priority">medium</xsl:attribute> <xsl:attribute name="date">2003-09-23</xsl:attribute> <xsl:attribute name="doc:style" namespace="http://www.wbex.ru/documents">classic</xsl:attribute> </xsl:attribute-set> <xsl:template match="/"> <xsl:element name="paragraph" use-attribute-sets="paragraph"> <xsl:apply-templates /> </xsl:element> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <paragraph xmlns:doc="http://www.wbex.ru/documents" priority="medium" date="2003-09-23"
doc:style="classic">Message.</paragraph> </source>
Disable escape
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <message>test</message>
File: Transform.xslt <stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform"> <output method="xml" omit-xml-declaration="yes" /> <template match="/"> <publisher xmlns=""> <value-of select="title" xmlns="http://www.w3.org/1999/XSL/Transform" /> <text disable-output-escaping="no" xmlns="http://www.w3.org/1999/XSL/Transform"> & Associates</text> </publisher> </template>
</stylesheet> Output: <publisher> & Associates</publisher>
</source>
disable-output-escaping="yes"
<source lang="xml">
File: Data.xml <title>wbex</title> File: Transform.xslt <stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform"> <output method="xml" omit-xml-declaration="yes" /> <template match="/"> <publisher xmlns=""> <value-of select="title" xmlns="http://www.w3.org/1999/XSL/Transform" /> <text disable-output-escaping="yes" xmlns="http://www.w3.org/1999/XSL/Transform"> & Associates</text> </publisher> </template>
</stylesheet> Output: <publisher>wbex & Associates</publisher>
</source>
output doctype-system="testOut.dtd"
<source lang="xml">
File: Data.xml <test>test</test> File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output doctype-system="testOut.dtd" /> <xsl:template match="test"> <testOut> test. <xsl:apply-templates /> </testOut> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE testOut
SYSTEM "testOut.dtd">
<testOut>
test. test</testOut> </source>
Output element with Namespace
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <message>test</message> 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="doc:paragraph" namespace="http://www.wbex.ru/documents"> <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">test</doc:paragraph>
</source>
output method="xml" omit-xml-declaration="yes"
<source lang="xml">
File: Data.xml
<test>test</test> 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"/>
</xsl:stylesheet> Output: test
</source>
output method="xml" version="1.1" encoding="utf-16"
<source lang="xml">
File: Data.xml
<test>test</test> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="xml" version="1.1" encoding="utf-16" />
</xsl:stylesheet>
</source>
Output new xml tag
<source lang="xml">
File: Data.xml
<employees>
<employee hireDate="04/23/1999" officer="yes"> <last>A</last> <first>B</first> <salary>100000</salary> </employee> <employee hireDate="09/01/1998" officer="no"> <last>C</last> <first>D</first> <salary>95000</salary> </employee> <employee hireDate="08/20/2000"> <last>E</last> <first>F</first> <salary>89000</salary> </employee>
</employees> 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="employee"> <client> <xsl:apply-templates /> </client> </xsl:template>
</xsl:stylesheet> Output:
<client> A B 100000 </client> <client> C D 95000 </client> <client> E F 89000 </client> </source>
Output xml element in various form
<source lang="xml">
File: Data.xml <test>test</test>
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="test"> <test> 1. <sample /> 2. <sample></sample> 3. <xsl:element name="sample" /> 4. <sample color="green" /> 5. <sample color="green"></sample> 6. <xsl:element name="sample"> <xsl:attribute name="color">green</xsl:attribute> </xsl:element> 7. <sample></sample> <xsl:apply-templates /> </test> </xsl:template>
</xsl:stylesheet> Output: <test>
1. <sample/> 2. <sample/> 3. <sample/> 4. <sample color="green"/> 5. <sample color="green"/> 6. <sample color="green"/> 7. <sample/>test</test> </source>
Rename tag
<source lang="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: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.0" encoding="UTF-8"?><name><family>A</family><given>B</given></name>
</source>
Restructure xml document
<source lang="xml">
File: Data.xml <wine grape="Chardonnay">
<product>product 2</product> <year>1997</year> <price>10.99</price>
</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="wine"> <wine vintage="{year}"> <product> <xsl:apply-templates select="product" /> </product> <category> <xsl:value-of select="@grape" /> </category> <price> <xsl:apply-templates select="price" /> </price> </wine> </xsl:template>
</xsl:stylesheet> Output: <wine vintage="1997"><product>product 2</product><category>Chardonnay</category><price>10.99</price></wine>
</source>
Set Encoding
<source lang="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 encoding="UTF-16" /> <xsl:template match="name"> <name> <family> <xsl:apply-templates select="last" /> </family> <given> <xsl:apply-templates select="first" /> </given> </name> </xsl:template>
</xsl:stylesheet>
</source>
Use attribute value to create new attribute
<source lang="xml">
File: Data.xml <wine grape="Cabernet">
<winery>shop 1</winery> <product>product 1</product> <year>1996</year> <prices date="12/1/01"> <list>13.99</list> <discounted>11.00</discounted> </prices>
</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="prices"> <prices date="{@date}"> <xsl:apply-templates /> </prices> </xsl:template>
</xsl:stylesheet> Output:
shop 1 product 1 1996 <prices date="12/1/01"> 13.99 11.00 </prices> </source>
Use concat function to create name of Element
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <message>
You can use the element element to create elements on the result tree.
</message>
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="message"> <xsl:element name="{concat("my", name())}"> <xsl:apply-templates /> </xsl:element> </xsl:template>
</xsl:stylesheet>
Output: <?xml version="1.0" encoding="UTF-8"?> <mymessage>
You can use the element element to create elements on the result tree.
</mymessage>
</source>
use for-each to output xml tags
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?> <emailList>
<person> <name>person1</name> <email>p@hotmail.ru</email> </person> <person> <name>person2</name> <email>p@hotmail.ru</email> </person> <person> <name>person3</name> <email>p3@hotmail.ru</email> </person>
</emailList>
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" indent="yes"/>
<xsl:template match="/"> <contacts> <xsl:for-each select="emailList/person"> <contact> <fullName> <xsl:value-of select="name"/> </fullName> <eMail> <xsl:value-of select="email"/> </eMail> </contact> </xsl:for-each> </contacts> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <contacts>
<contact> <fullName>person1</fullName> <eMail>p@hotmail.ru</eMail> </contact> <contact> <fullName>person2</fullName> <eMail>p@hotmail.ru</eMail> </contact> <contact> <fullName>person3</fullName> <eMail>p3@hotmail.ru</eMail> </contact>
</contacts>
</source>
use new tag to wrap
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <message>test</message> 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="/"> <paragraph> <xsl:apply-templates /> </paragraph> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <paragraph>test</paragraph>
</source>
Using xsl:element and xsl:attribute
<source lang="xml">
File: Data.xml <?xml version = "1.0" encoding = "UTF-8"?> <sports>
<game title="cricket"> <id>243</id> <paragraph>para 1</paragraph> </game>
</sports> 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="/sports"> <sports> <xsl:apply-templates select="game" /> </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="paragraph" /> </comment> </xsl:element> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><sports><cricket id="243"><comment>para 1</comment></cricket></sports>
</source>
Using xsl:element and xsl:attribute to create xml tags
<source lang="xml">
File: Data.xml <?xml version = "1.0" encoding = "UTF-8"?> <sports>
<game title = "cricket"> <id>243</id> <paragraph> para 1 </paragraph> </game>
</sports> 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 = "/sports"> <sports> <xsl:apply-templates select = "game"/> </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 = "paragraph"/> </comment> </xsl:element> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><sports><cricket id="243"><comment>
para 1 </comment></cricket></sports> </source>
xsl:attribute generates elements
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<color>blue</color> <color>navy</color> <color>green</color> <color>lime</color> <color>red</color>
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="color">
<xsl:attribute name="style"><xsl:text>color:</xsl:text><xsl:value-of select="."/></xsl:attribute> <xsl:value-of select="."/> |
</xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
blue |
navy |
green |
lime |
red |
</source>
xsl:element generates elements in time of processing.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<text size="H1">Header1</text> <text size="H3">Header3</text> <text size="b">Bold text</text> <text size="sub">Subscript</text> <text size="sup">Superscript</text>
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="/"> <xsl:for-each select="//text"> <xsl:element name="{@size}"> <xsl:value-of select="."/> </xsl:element> </xsl:for-each> </xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>Header1
Header3
Bold textSubscriptSuperscript</source>