Sunday, April 3, 2011

Change font in XML using XSLT

Hi,

I'm new to XSLT. I'm trying to change the font size of a specific text in XML file using XSLT. For eg- I have the CDCatalog.xml file with following data.

  <?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="cdcat.xsl"?>

<catalog>
 <cd>
  <title>Empire Burlesque</title> 
  <artist><SmallText>Bob Dylan</SmallText><LineBreak/>*</artist>
  <country>USA</country>
  <company>Columbia</company> 
  <price>10.90</price> 
  <year>1985</year> 
  </cd>
</catalog>

and the cdCat.XSL file is-

<?xml version="1.0" encoding="ISO-8859-1" ?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:include href="cdCatalog.xsl" /> <!-- I added this -->
<xsl:template match="/">
 <html>
 <body>
  <h2>My CD Collection</h2> 
 <table border="1">
 <tr bgcolor="#9acd32">
  <th align="left">Title</th> 
  <th align="left">Artist</th> 
  </tr>
 <xsl:for-each select="catalog/cd">
 <tr>
 <td>
  <xsl:value-of select="title" /> 
  </td>
 <td>
 <xsl:value-of select="artist" /> 
  </td>
  </tr>
  </xsl:for-each>

  </table>
  </body>
  </html>
  </xsl:template>

</xsl:stylesheet>

I added a new xsl file cdCatalog.XSL file with following details-

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="LineBreak">
    <br/>
</xsl:template>

<xsl:template match="Superscript">
    <sup>
     <xsl:value-of select="."/>
    </sup>
</xsl:template>

<xsl:template match="SmallText">
    <font size="1">
     <xsl:value-of select="."/>
    </font>
</xsl:template>
</xsl:stylesheet>

and included this file in the CDCat.xsl file.and added the tags - <smallText>, <LineBreak> in the CdCatalog.xml file. now when I open the xml file i dont see the LineBreak nor the font size difference. Can anyone please suggest if I'm missing something.

Thanks in advance Sai

From stackoverflow
  • XML says nothing about presentation, that's the whole point. It's a data format.

    If you want your XSLT to output to something where presentation matters I suggest you transform to HTML and get let HTML/CSS handle the styling.


    Having seen your actual code now (hint: use the formatting when creating questions) don't use the font tag. What you want semantically and in practice is just headers <h1>, <h2>, <h3> etc, and I'd still suggest you add a CSS link in there. Oh and <xsl:output method="html" />

  • You need to use apply-templates to indicate where your template matches should take effect.

    annakata : good point, couldn't even see that in the code
    annakata : please don't use font though...
  • In-between these two opening tags:

    <html>
    <body>
    

    ...I'd place a link to a style sheet that defines the font sizes. Alternatively (and useful if you want a self contained HTML file to email around) you could put a style block there instead.

0 comments:

Post a Comment