XSLT checking for an attribute, how do I make it “do nothing” if that attribute is not in a known set of values?

StackOverflow https://stackoverflow.com/questions/12624855

  •  04-07-2021
  •  | 
  •  

Question

I am working with a CCD XML document, it contains data like this:

<tr ID="encounter17" >
    <td>09/18/2012</td>
    <td ID="type41">Office Visit</td>
    <td>Family Practice</td>
    <td>
        <paragraph>REDACTED</paragraph>
    </td>
    <td />
</tr>
<tr ID="encounter18" styleCode="altRow">
    <td>09/17/2012</td>
    <td ID="type42">Office Visit</td>
    <td>Family Practice</td>
    <td>
        <paragraph>REDACTED</paragraph>
    </td>
    <td />
</tr>

I am trying to render this as HTML using the "standard" CCD.xsl, Derived from HL7 Finland R2 Tyylitiedosto: Tyyli_R2_B3_01.xslt Updated by Calvin E. Beebe, Mayo Clinic - Rochester Mn.

The element with the ID encounter17 will render, the element with the ID encounter18 will not render. Looking at the output of xsltproc this is happening because the XSLT does not recognize the styleCode attribute. Only Bold, Italic, Underline are supported.

How can I change this XSLT so that it tolerates an unknown styleCode?

<!--    Stylecode processing
  Supports Bold, Underline and Italics display-->
<xsl:template match="//n1:*[@styleCode]">

<xsl:if test="@styleCode='Bold'">
    <xsl:element name='b'>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:if>

<xsl:if test="@styleCode='Italics'">
    <xsl:element name='i'>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:if>

<xsl:if test="@styleCode='Underline'">
    <xsl:element name='u'>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:if>

<xsl:if test="contains(@styleCode,'Bold') and contains(@styleCode,'Italics') and not (contains(@styleCode, 'Underline'))">
    <xsl:element name='b'>
        <xsl:element name='i'>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:element>
</xsl:if>

<xsl:if test="contains(@styleCode,'Bold') and contains(@styleCode,'Underline') and not (contains(@styleCode, 'Italics'))">
    <xsl:element name='b'>
        <xsl:element name='u'>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:element>
</xsl:if>

<xsl:if test="contains(@styleCode,'Italics') and contains(@styleCode,'Underline') and not (contains(@styleCode, 'Bold'))">
    <xsl:element name='i'>
        <xsl:element name='u'>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:element>
</xsl:if>

<xsl:if test="contains(@styleCode,'Italics') and contains(@styleCode,'Underline') and contains(@styleCode, 'Bold')">
    <xsl:element name='b'>
        <xsl:element name='i'>
            <xsl:element name='u'>
                <xsl:apply-templates/>
            </xsl:element>
        </xsl:element>
    </xsl:element>
</xsl:if>

I have already tried adding:

<xsl:if test="not (contains(@styleCode,'Italics')) and not (contains(@styleCode,'Underline')) and not (contains(@styleCode, 'Bold'))">
    <xsl:apply-templates/>
</xsl:if>

This seems to just render the element, it doesn't "fall through" to the part of the XSLT that deals with the <tr> tag.

Was it helpful?

Solution

What horrible code! In 2.0 you want to do it like this:

<xsl:template match="n1:*[contains(@styleCode,'Bold')]" priority="100">
  <b><xsl:next-match/></b>
</xsl:template>

<xsl:template match="n1:*[contains(@styleCode,'Italic')]" priority="99">
  <i><xsl:next-match/></i>
</xsl:template>

<xsl:template match="n1:*[contains(@styleCode,'Underline')]" priority="98">
  <u><xsl:next-match/></u>
</xsl:template>

That's essentially all. This will ignore any stylecode not in the list.

You can do the same thing in 1.0 using xsl:apply-imports, but the three rules then need to be in separate stylesheet modules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top