Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using more than one apply-templates with the same select filter

Tags:

xslt-1.0

I would like to apply some templates to my xml using apply-templates, but I can't seem to work out how to have more than one template type per "data type". For example, with this xml:

<?xml version="1.0" encoding="UTF-8"?>
<items>
<item name='1'>
first
</item>
<item name='2'>
second
</item>
<item name='3'>
third
</item>
</items>

I use the following xslt to get the output I want:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

<xsl:template match="items/item">
    <xsl:value-of select='.'></xsl:value-of>
</xsl:template>
<xsl:template match="/"> 
    <html> 
      <body>
      <font color="blue">
      <xsl:apply-templates select="items/item[@name='1']"></xsl:apply-templates>
      </font>
      <font color="red">      
      <xsl:apply-templates select="items/item[@name='1']"></xsl:apply-templates>
      </font>
      </body> 
    </html> 
  </xsl:template> 
</xsl:stylesheet>

which is:

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <body><font color="blue">
         first
         </font><font color="red">
         first
         </font></body>
</html>

The first item colored blue followed by the same item colored red. But with this, I still end up with a lot of cut-n-paste boilerplate that I would love to move into the template "items/item", but I can't figure out how to get the same template to select one of the two colors. Is there a way to do this with out the wrappers in the code above?

like image 799
Gary Handy Avatar asked Oct 28 '25 02:10

Gary Handy


1 Answers

This question is quite similar, but perhaps not as clear:

XSL apply more than one template

If you use the "mode" attribute in xsl:template, you can specialize the template with the same select attribute:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

<xsl:template match="items/item" mode="blue">
    <font color="blue"><xsl:value-of select='.'></xsl:value-of></font>
</xsl:template>

<xsl:template match="items/item" mode="red">
    <font color="red"><xsl:value-of select='.'></xsl:value-of></font>
</xsl:template>

<xsl:template match="/"> 
    <html> 
      <body>
      <xsl:apply-templates select="items/item[@name='1']" mode="blue"></xsl:apply-templates>
      <xsl:apply-templates select="items/item[@name='1']" mode="red"></xsl:apply-templates>
      </body> 
    </html> 
  </xsl:template> 
</xsl:stylesheet>

This should allow you to move the red and blue font wrappers into the template definition and allow you to chose them based on the mode attribute.

like image 146
rkh Avatar answered Oct 31 '25 12:10

rkh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!