XML/XSLT stylesheet/axis — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:26, 26 мая 2010
Содержание
- 1 for-each and select="."
- 2 for-each select="listitem[position() > 1]"
- 3 List All customers: /report/customer
- 4 Match all from root
- 5 match element with certain attribute value
- 6 match="text()|@*"
- 7 select="." (dot)
- 8 select down to a certain level and its attribute
- 9 tag with index
- 10 template match="//customer"
- 11 template match="//customer[1]/name"
- 12 template match="//processing-instruction()"
for-each and select="."
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <root ref="rootref" name="blue">
<glossary> <item ref="blue" name="rootref">rootref</item> </glossary> <glossary> <item ref="itemref" name="itemref">itemref</item> </glossary>
</root> 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:for-each select="root/node1/node2/node3"> <xsl:value-of select="." /> </xsl:for-each> </xsl:template>
</xsl:stylesheet>
</source>
for-each select="listitem[position() > 1]"
<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"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title><xsl:value-of select="list/title"/></title> </head> <body style="font-family: sans-serif;"> <xsl:apply-templates select="list"/> </body> </html> </xsl:template> <xsl:template match="list">
<xsl:if test="count(listitem) > 1"> <xsl:attribute name="rowspan"> <xsl:value-of select="count(listitem)"/> </xsl:attribute> </xsl:if> <xsl:value-of select="title"/> |
<xsl:value-of select="listitem[1]"/> |
<xsl:value-of select="."/> |
</xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>title 1</title> </head> <body style="font-family: sans-serif;">
title 1 | item 1 |
item 2 | |
item 3 | |
item 4 | |
item 5 | |
item 6 | |
item 7 |
</body>
</html>
</source>
List All customers: /report/customer
<source lang="xml">
<?xml version="1.0" standalone="yes"?> <report type="Unfilled Orders">
<customer number="CUST111" type="VIP"> <name>name 1</name> <order-list count="2"> <order number="ORD200" owner="CUST111" total="650.00" status="late"> <item-list> <item quantity="5" price="100"> item 1 </item> <item quantity="2" price="50">item 2</item> <item quantity="1" price="50">item 3</item> </item-list> </order> <order number="ORD105" owner="CUST111" total="150.00" status="backordered"> <item-list> <item quantity="6" price="25"> item 4 </item> </item-list> </order> </order-list> </customer> <customer number="CUST222" type="normal"> <name>Alice Liddle</name> <order-list count="2"> <order number="ORD102" owner="CUST222" total="3490.00" status="late"> <item-list> <item quantity="20" price="100"> item 1 </item> <item quantity="10" price="50">item 2</item> <item quantity="10" price="50">item 3</item> <item quantity="10" price="25"> item 4 </item> <item quantity="2" price="120"> item 5 </item> </item-list> </order> </order-list> </customer>
</report> 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="/report/customer"> <xsl:apply-templates /> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
name 1 item 1 item 2 item 3 item 4 Alice Liddle item 1 item 2 item 3 item 4 item 5 </source>
Match all from root
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <message>
You can add processing instructions to a document with the <courier>processing-instruction</courier> element.
</message>
File: Transform.xslt <stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform"> <output method="text" /> <template match="/"> Message: <apply-templates /> </template>
</stylesheet> Output:
Message: You can add processing instructions to a document with the processing-instruction element. </source>
match element with certain attribute value
<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[@hireDate="10/16/2000"]" />
</xsl:stylesheet> Output:
A B 100000 C D 95000 E F 89000 </source>
match="text()|@*"
<source lang="xml">
File: Data.xml <winelist>
<wine grape="Chardonnay"> <winery>shop 1</winery> <product>product 1</product> <year>1998</year> <prices> <list>6.99</list> <discounted>5.99</discounted> <case>71.50</case> </prices> </wine> <wine grape="Chardonnay"> <winery>shop 2</winery> <product>product 2</product> <year>1997</year> <prices> <list>10.99</list> <discounted>9.50</discounted> <case>114.00</case> </prices> </wine>
</winelist> 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="*|/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="text()|@*"> <xsl:value-of select="." /> </xsl:template> <xsl:template match="processing-instruction()|comment()" />
</xsl:stylesheet> Output:
shop 1 product 1 1998 6.99 5.99 71.50 shop 2 product 2 1997 10.99 9.50 114.00 </source>
select="." (dot)
<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="wine"> <xsl:copy-of select="." /> </xsl:template>
</xsl:stylesheet> Output: <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>
</source>
select down to a certain level and its attribute
<source lang="xml">
File: Data.xml <shirts>
<colors> <color cid="c1">yellow</color> <color cid="c2">black</color> <color cid="c3">red</color> <color cid="c4">blue</color> <color cid="c5">purple</color> <color cid="c6">white</color> <color cid="c7">orange</color> <color cid="c7">green</color> </colors> <shirt colorCode="c4">item 1</shirt> <shirt colorCode="c1">item 2</shirt> <shirt colorCode="c6">item 3</shirt>
</shirts> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="text" /> <xsl:template match="shirt"> <xsl:variable name="shirtColorCode" select="@colorCode" /> <xsl:value-of select="/shirts/colors/color[@cid = $shirtColorCode]" /> <xsl:text> </xsl:text> <xsl:apply-templates /> </xsl:template> <xsl:template match="color" />
</xsl:stylesheet> Output:
blue item 1 yellow item 2 white item 3 </source>
tag with index
<source lang="xml">
File: Data.xml <html>
<body>
A | B | 100000 | 4/23/1999 |
C | D | 95000 | 09/01/1998 |
E | F | 97000 | 10/16/2000 |
</body>
</html> 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="table"> <employees> <xsl:apply-templates /> </employees> </xsl:template> <xsl:template match="tr"> <employee hireDate="{td[4]}"> <last> <xsl:value-of select="td[1]" /> </last> <first> <xsl:value-of select="td[2]" /> </first> <salary> <xsl:value-of select="td[3]" /> </salary> </employee> </xsl:template>
</xsl:stylesheet> Output:
<employees> <employee hireDate="4/23/1999"><last>A</last><first>B</first><salary>100000</salary></employee> <employee hireDate="09/01/1998"><last>C</last><first>D</first><salary>95000</salary></employee> <employee hireDate="10/16/2000"><last>E</last><first>F</first><salary>97000</salary></employee> </employees> </source>
template match="//customer"
<source lang="xml">
<?xml version="1.0" standalone="yes"?> <report type="Unfilled Orders">
<customer number="CUST111" type="VIP"> <name>name 1</name> <order-list count="2"> <order number="ORD200" owner="CUST111" total="650.00" status="late"> <item-list> <item quantity="5" price="100"> item 1 </item> <item quantity="2" price="50">item 2</item> <item quantity="1" price="50">item 3</item> </item-list> </order> <order number="ORD105" owner="CUST111" total="150.00" status="backordered"> <item-list> <item quantity="6" price="25"> item 4 </item> </item-list> </order> </order-list> </customer> <customer number="CUST222" type="normal"> <name>Alice Liddle</name> <order-list count="2"> <order number="ORD102" owner="CUST222" total="3490.00" status="late"> <item-list> <item quantity="20" price="100"> item 1 </item> <item quantity="10" price="50">item 2</item> <item quantity="10" price="50">item 3</item> <item quantity="10" price="25"> item 4 </item> <item quantity="2" price="120"> item 5 </item> </item-list> </order> </order-list> </customer>
</report> 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="//customer"> <xsl:apply-templates /> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
name 1 item 1 item 2 item 3 item 4 Alice Liddle item 1 item 2 item 3 item 4 item 5 </source>
template match="//customer[1]/name"
<source lang="xml">
<?xml version="1.0" standalone="yes"?> <report type="Unfilled Orders">
<customer number="CUST111" type="VIP"> <name>name 1</name> <order-list count="2"> <order number="ORD200" owner="CUST111" total="650.00" status="late"> <item-list> <item quantity="5" price="100"> item 1 </item> <item quantity="2" price="50">item 2</item> <item quantity="1" price="50">item 3</item> </item-list> </order> <order number="ORD105" owner="CUST111" total="150.00" status="backordered"> <item-list> <item quantity="6" price="25"> item 4 </item> </item-list> </order> </order-list> </customer> <customer number="CUST222" type="normal"> <name>Alice Liddle</name> <order-list count="2"> <order number="ORD102" owner="CUST222" total="3490.00" status="late"> <item-list> <item quantity="20" price="100"> item 1 </item> <item quantity="10" price="50">item 2</item> <item quantity="10" price="50">item 3</item> <item quantity="10" price="25"> item 4 </item> <item quantity="2" price="120"> item 5 </item> </item-list> </order> </order-list> </customer>
</report> 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="//customer[1]/name"> <xsl:apply-templates /> </xsl:template>
</xsl:stylesheet>
Output: <?xml version="1.0" encoding="UTF-8"?>
name 1 item 1 item 2 item 3 item 4 Alice Liddle item 1 item 2 item 3 item 4 item 5 </source>
template match="//processing-instruction()"
<source lang="xml">
<?xml version="1.0" standalone="yes"?> <report type="Unfilled Orders">
<customer number="CUST111" type="VIP"> <name>name 1</name> <order-list count="2"> <order number="ORD200" owner="CUST111" total="650.00" status="late"> <item-list> <item quantity="5" price="100"> item 1 </item> <item quantity="2" price="50">item 2</item> <item quantity="1" price="50">item 3</item> </item-list> </order> <order number="ORD105" owner="CUST111" total="150.00" status="backordered"> <item-list> <item quantity="6" price="25"> item 4 </item> </item-list> </order> </order-list> </customer> <customer number="CUST222" type="normal"> <name>Alice Liddle</name> <order-list count="2"> <order number="ORD102" owner="CUST222" total="3490.00" status="late"> <item-list> <item quantity="20" price="100"> item 1 </item> <item quantity="10" price="50">item 2</item> <item quantity="10" price="50">item 3</item> <item quantity="10" price="25"> item 4 </item> <item quantity="2" price="120"> item 5 </item> </item-list> </order> </order-list> </customer>
</report> 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="//processing-instruction()"> <xsl:apply-templates /> </xsl:template>
</xsl:stylesheet>
</source>