2014-01-13 12 views
0

我想选择前的字符串的函数“:”还是像第一次)在XSLT的功能(选择之前串“:”还是像第一个()的XSLT

我输入的是

<E T="B">1.1.1-CES: Describe the normal anatomy.</E> 

预期输出是

<span class="emph_B"> 
    <span class="emph_Bcol">1.1.1-CES:</span> 
    Describe the normal anatomy. 
</span> 

我有麻烦:)

之前选择的第一个字符串即 (1.1.1-CES 210

目前做好以下工作:

 <xsl:template match="E"> 
    <span> 
     <xsl:attribute name="class">emph_I</xsl:attribute> 
     <xsl:if test="contains(.,':')"> 
     <xsl:variable name="token" select="tokenize(.,': ')[last()]"/> 
     <xsl:value-of select="$token"/> 
     <xsl:variable name="seprate" 
     select="translate(current(),$token,'')"/>  
     <xsl:value-of select="$seprate"/> 
     </xsl:if> 
     <xsl:apply-templates /> 
     </span> 
     </xsl:template> 

我得到的输出

  <div class="P"><img class="rarr" src="images/FFU1.gif"></img><span class="emph_I">Describe the 
       normal c anatomy.111-CES:1.1.1-CES: Describe the 
       normal anatomy.</span></div> 

是有这样的函数第一个()或()之前,像去年()的XSLT?

代码:

<xsl:template match="/"> 
    <xsl:apply-templates select="//CHAPTER" /> 
</xsl:template> 

<xsl:template match="CHAPTER"> 
    <xsl:result-document href="{$pDest}chapter{position()}.xhtml"> 
     <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE HTML></xsl:text> 
     <html> 
      <head> 
       <title> 
        <xsl:value-of select="CHSO/THD/HD" /> 
       </title> 
       <link rel="stylesheet" type="text/css" href="styles/stylesheet.css" /> 
      </head> 
      <body> 
       <div> 
        <xsl:attribute name="class">CHAPTER</xsl:attribute> 
        <div> 
         <xsl:attribute name="class">CHSO</xsl:attribute> 
         <div> 
          <xsl:attribute name="class">THD box-style</xsl:attribute> 
          <div> 
           <xsl:attribute name="class">HNUM</xsl:attribute> 
           <xsl:attribute name="style">text-align:center;color:white;</xsl:attribute> 
           <xsl:value-of select="CHSO/THD/HNUM" /> 
          </div> 
          <div> 
           <xsl:attribute name="class">HD</xsl:attribute> 
           <xsl:attribute name="style">text-align:center;color:white;</xsl:attribute> 
           <xsl:value-of select="CHSO/THD/HD" /> 
          </div> 
         </div> 
        </div> 
        <xsl:apply-templates /> 
       </div> 
      </body> 
     </html> 
    </xsl:result-document> 
</xsl:template> 

<xsl:template match="TBD"> 
    <div> 
     <xsl:attribute name="class">TBD</xsl:attribute> 
     <xsl:apply-templates /> 
    </div> 
</xsl:template> 

<xsl:template match="TLV1"> 
    <div> 
     <xsl:attribute name="class">TLV1</xsl:attribute> 
     <xsl:variable name="chapVal" select="tokenize(../../@CHAN,'-')[last()]" /> 
     <xsl:variable name="myId" 
      select="translate($chapVal,translate($chapVal, '1234567890', ''),'')" /> 
     <xsl:variable name="tlvId" select="translate(./@ID, '.', '-')" /> 
     <h2> 
      <xsl:attribute name="class">HD</xsl:attribute> 
      <xsl:attribute name="id">ch<xsl:value-of 
       select="$myId" />-<xsl:value-of select="$tlvId" /> 
      </xsl:attribute> 
      <xsl:attribute name="style">margin-bottom:0.5em;</xsl:attribute> 
      <xsl:value-of select="HD" /> 
     </h2> 
     <xsl:apply-templates /> 
    </div> 
</xsl:template> 

<xsl:template match="P"> 
    <div> 
     <xsl:attribute name="class">P</xsl:attribute> 
     <xsl:apply-templates /> 
    </div> 
</xsl:template> 

<xsl:template match="FILE"> 
    <img> 
     <xsl:attribute name="class">rarr</xsl:attribute> 
     <xsl:attribute name="src">images/<xsl:value-of 
      select="@NAME" /> 
     </xsl:attribute> 
    </img> 
</xsl:template> 

<xsl:template match="E"> 
    <span> 
     <xsl:attribute name="class">emph_I</xsl:attribute> 
     <xsl:if test="contains(.,':')"> 
     <xsl:variable name="token" select="tokenize(.,': ')[last()]"/> 
     <xsl:value-of select="$token"/> 
      <xsl:variable name="seprate" select="translate(current(),$token,'')"/> 
      <xsl:value-of select="$seprate"/> 
     </xsl:if> 
     <xsl:apply-templates /> 
    </span> 
</xsl:template> 

<xsl:template match="LK"> 
    <a> 
     <xsl:attribute name="class">LK</xsl:attribute> 
     <xsl:variable name="chapVal" 
      select="tokenize(../../../../@CHAN,'-')[last()]" /> 
     <xsl:variable name="myId" 
      select="translate($chapVal,translate($chapVal, '1234567890', ''),'')" /> 
     <xsl:attribute name="href">chapter<xsl:value-of 
      select="$myId" />.xhtml#<xsl:value-of select="@IDREF" /> 
     </xsl:attribute> 
     <xsl:apply-templates /> 
    </a> 
</xsl:template> 

<xsl:template match="SIDEBAR/TLV1"> 
    <div> 
     <xsl:attribute name="class">SIDEBAR <xsl:value-of 
      select="@CLASSF" /></xsl:attribute> 
     <div> 
      <xsl:attribute name="class">TLV1</xsl:attribute> 
      <div> 
       <xsl:attribute name="class">HD bg-col</xsl:attribute> 
       <span> 
        <xsl:attribute name="class">sb-head</xsl:attribute> 
        <span> 
         <xsl:attribute name="class">col-auto</xsl:attribute> 
         <xsl:value-of select="HD"></xsl:value-of> 
        </span> 
       </span> 
      </div> 
      <xsl:if test="P"> 
       <div> 
        <xsl:attribute name="class">sb-pad</xsl:attribute> 
        <xsl:for-each select="P"> 
         <div> 
          <xsl:attribute name="class">P</xsl:attribute> 
          <xsl:apply-templates select="ancestor-or-self::text()" /> 
          <xsl:if test="E"> 
           <xsl:for-each select="E"> 
            <span> 
             <xsl:attribute name="class">emph_Bcol</xsl:attribute> 
             <xsl:apply-templates select="node()" /> 
            </span> 
            <xsl:apply-templates select="following-sibling::node()[1]" /> 
           </xsl:for-each> 
          </xsl:if> 
         </div> 
        </xsl:for-each> 
       </div> 
      </xsl:if> 
     </div> 
    </div> 
</xsl:template> 

<xsl:template match="FG"> 
    <div> 
     <xsl:attribute name="class">FG</xsl:attribute> 
     <xsl:attribute name="id"><xsl:value-of select="@ID"></xsl:value-of> 
      </xsl:attribute> 
     <xsl:for-each select="P"> 
      <xsl:if test="FILE"> 
       <img> 
        <xsl:attribute name="class">center</xsl:attribute> 
        <xsl:attribute name="src">images/<xsl:value-of 
         select="FILE/@NAME" /> 
         </xsl:attribute> 
       </img> 
      </xsl:if> 
      <xsl:if test="E"> 
       <div> 
        <xsl:attribute name="class">caption</xsl:attribute> 
        <xsl:for-each select="E"> 
         <span> 
          <xsl:attribute name="class">emph_Bcol</xsl:attribute> 
          <xsl:apply-templates /> 
         </span> 
         <xsl:apply-templates select="following-sibling::node()[1]" /> 
        </xsl:for-each> 
       </div> 
      </xsl:if> 
     </xsl:for-each> 
    </div> 
</xsl:template> 

<xsl:template match="TB"> 
    <div> 
     <xsl:attribute name="class">TB</xsl:attribute> 
     <xsl:attribute name="id"><xsl:value-of select="@ID" /> </xsl:attribute> 
     <table> 
      <xsl:attribute name="class">FIGURE</xsl:attribute> 
      <xsl:attribute name="style">border-collapse:collapse;margin-bottom:1em;</xsl:attribute> 
      <tr style="background-color:#0768a9;"> 
       <td style="vertical-align:top;" class="FIGURE-COL2" colspan="2"> 
        <span class="emph_B"> 
         <xsl:value-of select="TI/E" /> 
        </span> 
        <span class="white"> 
         <xsl:value-of select="TI/node()[2]" /> 
        </span> 
       </td> 
      </tr> 
      <xsl:apply-templates /> 
     </table> 
    </div> 
</xsl:template> 

<xsl:template match="TI"> 
</xsl:template> 

<xsl:template match="tr"> 
    <tr> 
     <xsl:apply-templates /> 
    </tr> 
</xsl:template> 

<xsl:template match="th"> 
    <th> 
     <xsl:attribute name="style">vertical-align:bottom;text-align:left;</xsl:attribute> 
     <xsl:attribute name="rowspan">1</xsl:attribute> 
     <xsl:attribute name="colspan">1</xsl:attribute> 
     <xsl:choose> 
      <xsl:when test="contains(., '&#160;')"> 
       <xsl:value-of select="translate(., '&#160;', '')" /> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:apply-templates /> 
      </xsl:otherwise> 
     </xsl:choose> 
    </th> 
</xsl:template> 

<xsl:template match="td"> 
    <td> 
     <xsl:attribute name="style">vertical-align:top;</xsl:attribute> 
     <xsl:attribute name="rowspan">1</xsl:attribute> 
     <xsl:attribute name="colspan">1</xsl:attribute> 
     <xsl:choose> 
      <xsl:when test="contains(., '&#160;')"> 
       <xsl:value-of select="translate(., '&#160;', '')" /> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:apply-templates select="node()[1]" /> 
      </xsl:otherwise> 
     </xsl:choose> 
    </td> 
</xsl:template> 

回答

1

这是指在第一元件的方式:

<xsl:variable name="token" select="tokenize(.,': ')[1]"/> 

功能substring-before工程,以及:

<xsl:variable name="token" select="substring-before(.,':')"/> 

但既然你问了的last()等效,[1]更加贴合,我想。

请注意,tokenize()会考虑整个字符串并输出由:分隔的任何令牌。另一方面,substring-before()在第一次出现“:”之前返回子串。所以,这只适用于字符串中总是只有一个“:”的情况,或者如果它始终是第一个“:”的话。

从样式表判断,似乎是输出了字符串的两个部分(即在“:”之前和之后)。因此,这将是一个好主意,整个标记化存储在一个变量:

<xsl:variable name="token" select="tokenize(.,': ')"/> 

后来又指$token[1]$token[2]

编辑:编辑回复您的评论。希望这更好的解释它:

<?xml version="1.0" encoding="utf-8"?> 

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="xml" indent="yes"/> 

<xsl:template match="E"> 
    <xsl:variable name="tokens" select="tokenize(.,':')"/> 

    <span class="emph_B"> 
    <span class="emph_bcol"> 
     <xsl:value-of select="$tokens[1]"/> 
    </span> 
    <xsl:value-of select="$tokens[2]"/> 
    </span> 
</xsl:template> 

</xsl:stylesheet> 

输入

<E T="B">1.1.1-CES: Describe the normal anatomy.</E> 

输出

<span class="emph_B"> 
<span class="emph_bcol">1.1.1-CES</span> Describe the normal anatomy.</span> 
+0

试图子字符串之前我得到输出为

Describe and illustrate the normal cardiovascular anatomy.111-CES:1.1.1-CES: Describe and illustrate the normal cardiovascular anatomy.
user3111030

+0

这回答你的问题,但当然你的实现是另一回事。请发布您的整个XSLT代码。然后,我将能够帮助你。 –

0

substring-before(.,":")将选择字符串前:

+1

已经尝试过了,我得到的Xpath是无效的必须是双引号 – user3111030

+1

。使用单引号引号已经用''引号括起来了。 –

相关问题