XSL 显示某个字符后的属性 [英] XSL Display attribute after a certain character

查看:20
本文介绍了XSL 显示某个字符后的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要显示的属性,但我只想显示它的最后一部分,用-"表示.我正在使用 substring-after 来执行此操作,但这仅在有一个字符时才有效.有时可能有一个,有时可能有两个.我已经看到了一些递归模板,但我没有在 For-each 循环中看到它们,就像我在这里看到的那样,我不确定我会把所有东西放在我的 XSL 文档中的什么位置.

这是我的 XML 文档:

这是我的 XSL 文档.我正在使用 XSL 1.0.我正在寻找的结果是我希望将其显示为10mm Drill - Grind me back"而不是10mm Drill-Contour Milling - Grind me back",这是我现在使用 substring-after 函数或其他同样的结果.

<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"><头><meta http-equiv="content-type" content="text/html; charset=UTF-8"/><title>工具报告</title><身体><xsl:apply-templates/></html></xsl:模板><xsl:template match="JobList"><div style="font-size:;"><table width="100%" style="margin-bottom:50px;font:bold 10px arial;"><头><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><tr><td style="text-align:center;font:normal 7px arial;font-size:12px;"><xsl:value-of select="//Job[position()=1]/@cfg.JOBLISTNAME"/><跨度></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:when test="contains(@T.toolComment3, 'GRIND') 或 contains(@T.toolComment3, 'Grind')"><xsl:value-of select="@T.toolComment3"/></xsl:when></xsl:选择><xsl:when test="contains(@T.comment2, 'GRIND') 或 contains(@T.comment2, 'Grind')"><xsl:value-of select="@T.comment2"/></xsl:when></xsl:选择><xsl:when test="contains(@oper.jobName, 'GRIND') 或 contains(@oper.jobName, 'Grind')"><xsl:value-of select="substring-after(@oper.jobName, '-')"/></xsl:when></xsl:选择></td></tr></xsl:for-each></tbody>

</xsl:模板></xsl:stylesheet>

解决方案

使用命名递归模板获取文本字符串的最后一个标记.

<xsl:param name="text"/><xsl:param name="delimiter" select="'-'"/><xsl:when test="contains($text, $delimiter)"><!-- 递归调用--><xsl:call-template name="last-token"><xsl:with-param name="text" select="substring-after($text, $delimiter)"/></xsl:call-template></xsl:when><xsl:否则><xsl:value-of select="$text"/></xsl:否则></xsl:选择></xsl:模板>

调用示例:http://xsltransform.net/bFWR5Ew

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>

解决方案

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

这篇关于XSL 显示某个字符后的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆