2016-08-01 3 views
2

上記のテキストをtokenize内で取得する方法をお勧めします。このサンプルでは、​​先行するテキストWeb属性が適用されます。例 'SKY1996'タイプのテキストの前に文字列 'Moon'がある場合、属性値は 'Moon'、それ以外の場合は 'Sun'です。xsl tokenizeで、上記のテキスト値を取得する方法

XML:

<article> 
<text1>The solar eclipse Sun: SKY1996 is happenned in 1996</text1> 
<text2>The moon eclipse Moon: SKY1997 is happenned in 1997</text2> 
</article> 

XSLT 2.0:

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

    <xsl:template match="@*|node()"> 
     <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy> 
    </xsl:template> 

    <xsl:template match="text1/text()|text2/text()"> 
     <xsl:for-each select="tokenize(., ' ')"> 
      <xsl:variable name="varPrecededText"><xsl:value-of select=".[preceding-sibling::node()[1]]"/></xsl:variable><!--Within tokenize, preceded text value --><!--Requesting suggestion to get this --> 
      <xsl:choose> 
       <xsl:when test="matches(., '^([S][K][Y])\d{4}$')"> 
        <xsl:element name="web"> 
         <xsl:attribute name="href"> 
          <xsl:choose> 
           <xsl:when test="contains($varPrecededText, 'Moon')"> 
            <xsl:text>MoonEclipse:</xsl:text> 
           </xsl:when> 
           <xsl:otherwise><xsl:text>SunEclipse:</xsl:text></xsl:otherwise> 
          </xsl:choose> 
          <xsl:value-of select="."/> 
         </xsl:attribute> 
         <xsl:value-of select="."/> 
        </xsl:element><xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="."/> 
        <xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each> 
    </xsl:template> 

必要な結果:

<article> 
<text1>The solar eclipse Sun: <web href="SunEclipse:SKY1996">SKY1996</web> is happenned in 1996</text1> 
<text2>The moon eclipse Moon: <web href="MoonEclipse:SKY1997">SKY1997</web> is happenned in 1997</text2> 
</article> 

答えて

3

私は単に解析ストリングを使用しますマッチングサブ:

<xsl:template match="text1/text()|text2/text()"> 
    <xsl:analyze-string select="." regex="((\w+):)\s+([S][K][Y]\d{{4}})"> 
     <xsl:matching-substring> 
      <web href="{regex-group(2)}Eclipse:{regex-group(3)}"> 
       <xsl:value-of select="regex-group(3)"/> 
      </web> 
     </xsl:matching-substring> 
     <xsl:non-matching-substring> 
      <xsl:value-of select="."/> 
     </xsl:non-matching-substring> 
    </xsl:analyze-string> 
</xsl:template> 

((\w+):)\s+([S][K][Y]\d{{4}})これらの一致がXSLで処理されたグループ3にSun: SKY1996Moon: SKY1997、グループ2にSunMoonSKY1996SKY1997を捕捉を使用して、それらを目的の構造に置き換えます。不一致はそのまま維持されます。

+0

あなたの貴重なご提案に感謝します。 –

関連する問題