2014-02-14 65 views
0

我試圖總結所有的文本節點在<文本>元素,但面臨的挑戰時遇到一個內聯元素(I,B,強調),這應該是在同一個<文本>節點(換句話說,它應該被視爲文本)...請參閱下面的輸入和所需的輸出:查找下面的文本節點的兄弟

(注意:我必須爲特定的內聯元素只做,因此保持它參數(它可能是任何東西),其餘的元素標準<文本>規則應該適用(請參閱我的xslt的詳細信息)

輸入X ML:

<?xml version="1.0" encoding="utf-8"?> 
    <root> 
     <para>XML Translation is a format that's used to <emphasis>exchange <i>localisation</i></emphasis>data</para> 
     <para>The process can now be reformulated with more detail as follows:<ul> 
       <li>Text extraction <note>Separation of translatable text from layout data</note></li> 
       <li>Pre-translation</li> 
       <li>Translation</li> 
       <li>Reverse conversion</li> 
       <li>Translation memory improvement</li> 
      </ul>above mentioned steps should <b>executed</b> sequentially</para> 
    </root> 

輸出應該是:

  <?xml version="1.0" encoding="utf-8"?> 
      <root> 
        <para> 
          <text xid="d0t3">XML Translation is a format that's used to <g ctype="emphasis">exchange <g ctype="i">localisation</g></g>data </text> 
        </para> 
        <para> 
          <text xid="d0t10">The process can now be reformulated with more detail as follows:</text> 
          <ul> 
            <li><text xid="d0t13">Text extraction <g ctype="note">Separation of translatable text from layout data</g></text></li> 
            <li><text xid="d0t17">Pre-translation</text></li> 
            <li><text xid="d0t19">Translation</text></li> 
            <li><text xid="d0t21">Reverse conversion</text></li> 
            <li><text xid="d0t23">Translation memory improvement</text></li> 
          </ul> 
          <text xid="d0t24">above mentioned steps should <g ctype="b">executed</g> sequentially</text>    
        </para> 
      </root> 

我想這樣的事情,但沒能獲得正確的結果:

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     version="2.0"> 
     <xsl:strip-space elements="*"/> 
     <xsl:param name="inlineElement" select="('emphasis', 'i', 'note', 'b')"/> 
     <xsl:template match="@*|node()"> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()"/> 
      </xsl:copy> 
     </xsl:template> 
     <xsl:template match="text()"> 
      <text> 
       <xsl:attribute name="xid"> 
        <xsl:value-of select="generate-id()"/> 
       </xsl:attribute> 
       <xsl:value-of select="."/> 
       <xsl:if test="following-sibling::node()[local-name()=$inlineElement]"> 
        <g> 
        <xsl:apply-templates select="following-sibling::node()[local-name()=$inlineElement]/text()"/> 
        </g> 
       </xsl:if> 
      </text> 
     </xsl:template> 
    </xsl:stylesheet> 
+0

不應使用_both_ XSLT-1.0和XSLT-2.0標記問題 - 這些標記是獨佔的。 –

回答

3

我會用for-each-group group-adjacent

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     version="2.0"> 
     <xsl:strip-space elements="*"/> 
     <xsl:output indent="yes"/> 
     <xsl:param name="inlineElement" select="('emphasis', 'i', 'note', 'b')"/> 
     <xsl:template match="@*|node()"> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()"/> 
      </xsl:copy> 
     </xsl:template> 

     <xsl:template match="*[not(local-name() = $inlineElement)]"> 
      <xsl:copy> 
      <xsl:for-each-group select="node()" group-adjacent="boolean(self::text() | self::*[local-name() = $inlineElement])"> 
       <xsl:choose> 
       <xsl:when test="current-grouping-key()"> 
        <text xid="{generate-id(current-group()[self::text()][1])}"> 
        <xsl:apply-templates select="current-group()"/> 
        </text> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:apply-templates select="current-group()"/> 
       </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each-group> 
      </xsl:copy> 
     </xsl:template> 

     <xsl:template match="*[local-name() = $inlineElement]"> 
      <g ctype="{local-name()}"> 
      <xsl:apply-templates/> 
      </g> 
     </xsl:template> 

    </xsl:stylesheet> 

那個w ay,使用Saxon 9.5,我得到

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <para> 
     <text xid="d1t3">XML Translation is a format that's used to <g ctype="emphasis">exchange <g ctype="i">localisation</g> 
     </g>data</text> 
    </para> 
    <para> 
     <text xid="d1t10">The process can now be reformulated with more detail as follows:</text> 
     <ul> 
     <li> 
      <text xid="d1t13">Text extraction <g ctype="note">Separation of translatable text from layout data</g> 
      </text> 
     </li> 
     <li> 
      <text xid="d1t17">Pre-translation</text> 
     </li> 
     <li> 
      <text xid="d1t19">Translation</text> 
     </li> 
     <li> 
      <text xid="d1t21">Reverse conversion</text> 
     </li> 
     <li> 
      <text xid="d1t23">Translation memory improvement</text> 
     </li> 
     </ul> 
     <text xid="d1t24">above mentioned steps should <g ctype="b">executed</g> sequentially</text> 
    </para> 
</root> 
+0

謝謝馬丁,這對我有用。我有另一個挑戰來恢復它。如果你能評論我發佈的另一個問題,比如「使用ID更新另一個XML」。萬分感謝。 – Pankaj