Alex W
Alex W

Reputation: 17

XSL Display attribute after a certain character

I Have a Attribute that I want to display, but I only want to display the last portion of it indicated by an "-". I am using substring-after to do this but this only works if there is one charactor. There are occasions where there might be one and some where there is two. I have seen some recursive templates for this but I have not seen them in a For-each Loop like I have it here and I am not sure where I would put everything in my XSL document.

Here is my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<JobList>
<Job T.number="28" />
<Job T.identifier="10mm Drill" />
<Job oper.jobName="2: T28-Contour Milling - Grind me back" />                
<Job T.number="3" />                                
<Job T.identifier="9mm Drill" />                  
<Job oper.jobName="3: T3 Contour Milling" />
</JobList>

Here is my XSL Document. I am using XSL 1.0. The result I am looking for is I want this to be displayed as "10mm Drill - Grind me back" not "10mm Drill-Contour Milling - Grind me back" which is what I am getting now using the substring-after function or something with the same result.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" encoding="UTF-8" method="xml" />
<xsl:param name="REPORT">joblist</xsl:param>
<xsl:param name="LOCALE">en-GB</xsl:param>
<xsl:param name="FORMAT">html</xsl:param>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Tool Report</title>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="JobList">
<div style="font-size:;">
<table width="100%" style="margin-bottom:50px;font:bold 10px arial;">
<thead>
<tr>
<th style="text-align:center;font-family:Arial;font-size:13;font:bold 7px arial;">
<xsl:value-of select="@month">
</xsl:value-of>
<span>.</span>
<xsl:value-of select="@day">
</xsl:value-of>
<span>.</span>
<xsl:value-of select="@year">
</xsl:value-of>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center;font:normal 7px arial;font-size:12px;">
<xsl:value-of select="//Job[position()=1]/@cfg.JOBLISTNAME" />
<span>
</span>
<span>
</span>
</td>
</tr>
</tbody>
<table width="100%" border="1" style="margin-bottom:50px;font:13px arial;">
<thead style="font:19;">
<tr style="font-size:19;">
<td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
          <td style="text-align:center;font-weight:bold;font-size:19;">
          </td>
        </tr>
      </thead>
      <tbody style="font-size:19;">
        <xsl:for-each select="//Job[not(@T.number=preceding::Job/@T.number)]">
          <tr style="font-size:19;">
            <td style="font-size:19;">
              <xsl:value-of select="@T.number" />
            </td>
            <td>
            </td>
            <td style="font-size:19;">
              <xsl:value-of select="@T.identifier" />
              <xsl:choose>
                <xsl:when test="contains(@T.toolComment3, 'GRIND') or contains(@T.toolComment3, 'Grind')">
                  <xsl:value-of select="@T.toolComment3" />
                </xsl:when>
              </xsl:choose>
              <xsl:choose>
                <xsl:when test="contains(@T.comment2, 'GRIND') or contains(@T.comment2, 'Grind')">
                  <xsl:value-of select="@T.comment2" />
                </xsl:when>
              </xsl:choose>
              <xsl:choose>
                <xsl:when test="contains(@oper.jobName, 'GRIND') or contains(@oper.jobName, 'Grind')">
                  <xsl:value-of select="substring-after(@oper.jobName, '-')" />
                </xsl:when>
              </xsl:choose>
            </td>
          </tr>
        </xsl:for-each>
      </tbody>
    </table>
  </table>
</div>
</xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 462

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

Use a named recursive template to get the last token of the text string.

<xsl:template name="last-token">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'-'"/>
    <xsl:choose>
        <xsl:when test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="last-token">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Example of call: http://xsltransform.net/bFWR5Ew

Upvotes: 2

Related Questions