Display an XML attribute value only on the first encounter with XSL

It looks like you only want to output the paragraph for the “Result observation – warning validation phase” message for the first svrl:failed-assert element you find in your XML, but not for any of the others.

What you need to do is have a template to match the first such element, which can output the first paragraph, and then calls the named template svrl:failed-assert which will also match all the other elements too.

<xsl:template match="svrl:failed-assert[1]">
   <p>
      <u>
         <xsl:value-of select="(preceding-sibling::svrl:active-pattern)/@name[1]"/>
      </u>
   </p>
   <xsl:call-template name="svrl:failed-assert"/>
</xsl:template>

<xsl:template match="svrl:failed-assert" name="svrl:failed-assert">
    <!-- Existing code here -->

Here is a simplified XSLT (without any find-and-replace code)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:svrl="oh:no" exclude-result-prefixes="svrl">
   <xsl:output method="html" indent="yes"/>
   <xsl:template match="svrl:failed-assert[1]">
      <p>
         <u>
            <xsl:value-of select="(preceding-sibling::svrl:active-pattern)/@name[1]"/>
         </u>
      </p>
      <xsl:call-template name="svrl:failed-assert"/>
   </xsl:template>
   <xsl:template match="svrl:failed-assert" name="svrl:failed-assert">
      <table width="800">
         <tr>
            <td colspan="2">
               <font color="red">
                  <xsl:value-of select="svrl:text"/>
               </font>
            </td>
         </tr>
         <tr>
            <td width="50">Test:</td>
            <td width="750">
               <i>
                  <xsl:value-of select="@test"/>
               </i>
            </td>
         </tr>
      </table>
   </xsl:template>
</xsl:stylesheet>

When applied to the following simplified XML:

<a xmlns:svrl="oh:no">
   <svrl:active-pattern name="Result observation - warning validation phase"/>
   <svrl:fired-rule context="Context 1"/>
   <svrl:failed-assert test="Test 1" location="Location 1">
      <svrl:text>Text 1</svrl:text>
   </svrl:failed-assert>
   <svrl:failed-assert test="Test 2" location="Location 2">
      <svrl:text>Text 2</svrl:text>
   </svrl:failed-assert>
   <svrl:fired-rule context="Context 2"/>
   <svrl:failed-assert test="Test 3" location="Location 3">
      <svrl:text>Text 3</svrl:text>
   </svrl:failed-assert>
</a>

The following HTML is output:

<p>
   <u>Result observation - warning validation phase</u>
</p>
<table width="800">
   <tr>
      <td colspan="2">
         <font color="red">Text 1</font>
      </td>
   </tr>
   <tr>
      <td width="50">Test:</td>
      <td width="750">
         <i>Test 1</i>
      </td>
   </tr>
</table>
<table width="800">
   <tr>
      <td colspan="2">
         <font color="red">Text 2</font>
      </td>
   </tr>
   <tr>
      <td width="50">Test:</td>
      <td width="750">
         <i>Test 2</i>
      </td>
   </tr>
</table>
<table width="800">
   <tr>
      <td colspan="2">
         <font color="red">Text 3</font>
      </td>
   </tr>
   <tr>
      <td width="50">Test:</td>
      <td width="750">
         <i>Test 3</i>
      </td>
   </tr>
</table>

Leave a Comment