XML Tutorial/XSLT stylesheet/copy of — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:26, 26 мая 2010
Содержание
copy-of copies everything
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <results group="A">
<match> <date>10-Jun-98</date> <team score="2">team 1</team> <team score="1">team 2</team> </match> <match> <date>10-Jun-98</date> <team score="2">team 3</team> <team score="2">team 4</team> </match>
</results> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="table-heading"> <tr> <td> Date </td> <td> Home Team </td> <td> Away Team </td> <td> Result </td> </tr> </xsl:variable> <xsl:template match="/"> <html> <body>
Matches in Group <xsl:value-of select="/*/@group" />
<xsl:for-each select="//match">
<xsl:value-of select="concat(team[1], " versus ", team[2])" />
<xsl:value-of select="date" /> |
<xsl:value-of select="team[1]" /> |
<xsl:value-of select="team[2]" /> |
<xsl:value-of select="concat(team[1]/@score, "-", team[2]/@score)" /> |
</xsl:for-each> </body> </html> </xsl:template>
</xsl:stylesheet> Output: <html>
<body>
Matches in Group A
team 1 versus team 2
Date | Home Team | Away Team | Result |
10-Jun-98
|
team 1
|
team 2
|
2-1
|
team 3 versus team 4
Date | Home Team | Away Team | Result |
10-Jun-98
|
team 3
|
team 4
|
2-2
|
</body>
</html></source>
specify element to copy
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
GREETING
<paragraph>Hello, world!</p>
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:apply-templates select="/source/*"/> </xsl:template> <xsl:template match="h1"> <xsl:copy use-attribute-sets="H1"> <xsl:value-of select="."/> </xsl:copy> </xsl:template> <xsl:template match="p"> <xsl:copy use-attribute-sets="P "> <xsl:value-of select="."/> </xsl:copy> </xsl:template> <xsl:attribute-set name="H1"> <xsl:attribute name="align">center</xsl:attribute> <xsl:attribute name="style">color:red</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="P"> <xsl:attribute name="align">left</xsl:attribute> <xsl:attribute name="style">color:blue</xsl:attribute> </xsl:attribute-set>
</xsl:stylesheet></source>
<xsl:copy-of select="."/>
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <eu>
<member> <state>Austria</state> <state founding="yes">Belgium</state> </member> <candidate> <state>Bulgaria</state> <state>Cyprus</state> </candidate>
</eu>
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="eu"> <xsl:apply-templates select="member" /> </xsl:template> <xsl:template match="member"> <xsl:apply-templates select="state[2]" /> </xsl:template> <xsl:template match="state"> <xsl:copy-of select="." /> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><state founding="yes">Belgium</state></source>