XML/XSLT stylesheet/para

Материал из Web эксперт
Перейти к: навигация, поиск

Change these global parameter values

   <source lang="xml">

File: Data.xml <x>

  <input>7</input>
  <input>27</input>

</x> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" indent="yes" version="1.0" />
 <xsl:param name="parInput_1" select="x/input[1]" />
 <xsl:param name="parInput_2" select="x/input[2]" />
 <xsl:template match="x">
   <xsl:param name="total">
     <xsl:call-template name="y">
       <xsl:with-param name="z" select="$parInput_2" />
     </xsl:call-template>
   </xsl:param>
   <xsl:value-of select="$total - $parInput_1" />
 </xsl:template>
 <xsl:template name="y">
   <xsl:param name="z" select="$parInput_2" />
   <xsl:value-of select="$z" />
 </xsl:template>

</xsl:stylesheet> Output: 20

</source>
   
  


Compare value with defined parameter

   <source lang="xml">

File: Data.xml <wine grape="Cabernet">

 <winery>shop 1</winery>
 <product>product 1</product>
 <year>1996</year>
 <price>11.99</price>

</wine> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="html" />
 <xsl:param name="bodyTextSize">10pt</xsl:param>
 <xsl:template match="/">
   <xsl:if test="$bodyTextSize != "10pt"">
     <xsl:message>
       bodyTextSize default value overridden with value of
       <xsl:value-of select="$bodyTextSize" />
       .
     </xsl:message>
   </xsl:if>
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="winery">
   
     
       <xsl:apply-templates />
       <xsl:text> </xsl:text>
       <xsl:value-of select="../@grape" />
     
   
   
</xsl:template> <xsl:template match="product"> <xsl:apply-templates />
</xsl:template> <xsl:template match="year | price"> <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet> Output:

 shop 1 Cabernet
product 1
1996
11.99
</source>


Define a parameter and use it

   <source lang="xml">

File: Data.xml

<wine grape="Cabernet">

 <winery>shop 1</winery>
 <product>product 1</product>
 <year>1996</year>
 <price>11.99</price>

</wine> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="html" />
 <xsl:param name="bodyTextSize">10pt</xsl:param>
 <xsl:template match="/">
   <xsl:if test="not(contains($bodyTextSize,"pt"))">
     <xsl:message terminate="yes">
       bodyTextSize must be specified in points (pt).
     </xsl:message>
   </xsl:if>
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="winery">
   
     
       <xsl:apply-templates />
       <xsl:text> </xsl:text>
       <xsl:value-of select="../@grape" />
     
   
   
</xsl:template> <xsl:template match="product"> <xsl:apply-templates />
</xsl:template> <xsl:template match="year | price"> <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet> Output:

 shop 1 Cabernet
product 1
1996
11.99
</source>


Define a parameter and use it later

   <source lang="xml">

File: Data.xml

<wine grape="Type 1">

 <winery>Los Vascos</winery>
 <year>1998</year>
 <prices>
   <list>13.99</list>
   <discounted>11.99</discounted>
   <case>143.50</case>
 </prices>

</wine> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="html" />
 <xsl:param name="bodyTextSize">10pt</xsl:param>
 <xsl:template match="winery">
   
     
       <xsl:apply-templates />
       <xsl:text> </xsl:text>
       <xsl:value-of select="../@grape" />
     
   
   
</xsl:template> <xsl:template match="product"> <xsl:apply-templates />
</xsl:template> <xsl:template match="year | price"> <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet> Output:

 Los Vascos Type 1
1998
13.99 11.99 143.50 </source>