Search code examples
xsltxsl-stylesheet

Differential formatting for elements according to attribute values


I'm putting together a simple XSL stylesheet so colleagues can preview XML that they are editing in their browser. One element has many different attribute values, each of which needs to be rendered differently.

   <hi rend="b">

needs to be bold,

   <hi rend="b i"> 

needs to be bold and italic, and so on.

What do I need to do in the XSL to make this happen?

I've done a good deal of Googling and haven't found a solution yet; perhaps it's a very basic question, but any help very gratefully received.


Solution

  • Because you wrote your colleagues preview XML in their browser I assume that you expect an XSLT-1.0 solution. The following templates copy the hi element and replaces the attribute with b and i tags. The copied hi tags are ignored by the browser.

    However, in this solution you have to create combinations of each attribute value.

    <xsl:template match="hi[contains(@rend,'i')]">
        <xsl:copy>
          <i><xsl:apply-templates /></i>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="hi[contains(@rend,'b')]">
        <xsl:copy>
          <b><xsl:apply-templates /></b>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="hi[contains(@rend,'i') and contains(@rend,'b')]">
        <xsl:copy>
          <i><b><xsl:apply-templates /></b></i>
        </xsl:copy>
    </xsl:template>
    

    Output:

    <hi><i><b> 
      ...3...
    </b></i></hi>      
    
    <hi><i> 
      ...1...
    </i></hi>
    
    <hi><b> 
      ...2...
    </b></hi>