XML/XSLT stylesheet/entity
Содержание
Define and use entites
<source lang="xml">
File: Data.xml <employee hireDate="09/01/1998">
<last>A</last> <first>B</first> <salary>950</salary>
</employee> File: Transform.xslt <!DOCTYPE stylesheet [ <!ENTITY space "<xsl:text> </xsl:text>"> <!ENTITY cr "<xsl:text> </xsl:text>"> ]> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:template match="employee"> <xsl:apply-templates select="@hireDate" /> &cr; <xsl:apply-templates select="first" /> &space; <xsl:apply-templates select="last" /> </xsl:template>
</xsl:stylesheet> Output: 09/01/1998 B A
</source>
Define entity in style sheet
<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 <!DOCTYPE stylesheet [ <!ENTITY space "<xsl:text> </xsl:text>"> ]> <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:apply-templates select="winery" /> &space; <xsl:apply-templates select="product" /> &space; <xsl:apply-templates select="year" /> &space; <xsl:apply-templates select="@grape" /> <xsl:if test="@grape = "Chardonnay""> <xsl:text> other Chardonnays: </xsl:text> <xsl:for-each select="preceding-sibling::wine[@grape = "Chardonnay"] | following-sibling::wine[@grape = "Chardonnay"]"> <xsl:sort select="winery" /> <xsl:text> </xsl:text> <xsl:value-of select="winery" /> &space; <xsl:value-of select="product" /> </xsl:for-each> </xsl:if> </xsl:template>
</xsl:stylesheet> Output:
shop 1 product 1 1998 Chardonnay other Chardonnays: shop 2 product 2
shop 2 product 2 1997 Chardonnay
other Chardonnays: shop 1 product 1 </source>
Output entities
<source lang="xml">
File: Data.xml <employees>
<employee hireDate="04/23/1999"> <last>Hill</last> <first>Phil</first> <salary>100000</salary> </employee> <employee hireDate="09/01/1998"> <last>Herbert</last> <first>Johnny</first> <salary>95000</salary> </employee>
</employees> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="text" /> <xsl:strip-space elements="*" /> <xsl:template match="employees"> Last First Salary Hire Date ---- ----- ------ ---------- <xsl:apply-templates /> </xsl:template> <xsl:template match="employee"> <xsl:apply-templates select="last" /> <xsl:text> </xsl:text> <xsl:apply-templates select="first" /> <xsl:text> </xsl:text> <xsl:apply-templates select="salary" /> <xsl:text> </xsl:text> <xsl:apply-templates select="@hireDate" /> </xsl:template>
</xsl:stylesheet> Output:
Last First Salary Hire Date ---- ----- ------ ---------- Hill Phil 100000 04/23/1999Herbert Johnny 95000 09/01/1998 </source>
Renerence entities
<source lang="xml">
File: Data.xml <employee hireDate="09/01/1998">
<last>A</last> <first>B</first> <salary>9500</salary>
</employee> File: Transform.xslt <!DOCTYPE stylesheet [ <!ENTITY space "<xsl:text> </xsl:text>"> <!ENTITY cr "<xsl:text> </xsl:text>"> ]> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:template match="employee"> Hire Date: <xsl:apply-templates select="@hireDate" /> &cr; Name: <xsl:apply-templates select="first" /> &space; <xsl:apply-templates select="last" /> </xsl:template>
</xsl:stylesheet> Output:
Hire Date: 09/01/1998 Name: B A </source>
Use entities to wrap style sheet
<source lang="xml">
File: Data.xml <employee hireDate="09/01/1998">
<last>A</last> <first>B</first> <salary>9500</salary>
</employee> File: Transform.xslt <!DOCTYPE stylesheet [ <!ENTITY space "<xsl:text> </xsl:text>"> <!ENTITY cr "<xsl:text> </xsl:text>"> ]> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:template match="employee"> <xsl:text>Hire Date: </xsl:text> <xsl:apply-templates select="@hireDate" /> &cr; <xsl:text>Name: </xsl:text> <xsl:apply-templates select="first" /> &space; <xsl:apply-templates select="last" /> </xsl:template>
</xsl:stylesheet>
Output: Hire Date: 09/01/1998 Name: B A
</source>