Mega Code Archive

 
Categories / XML / XSLT StyleSheet
 

Define entity in style sheet

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