查找文本节点的后续兄弟

时间:2014-02-14 10:30:34

标签: xslt xslt-2.0

我正在尝试将所有文​​本节点包装在< text>中元素,但遇到内联元素(i,b,强调)时面临挑战,应该是相同的< text>节点(换句话说,它应被视为文本)...请参阅下面的输入和所需输出:

(注意:我必须仅对特定的内联元素执行此操作,因此将其保存在param中(可能是任何内容),对于其余的元素标准< text>规则应该应用。(请参阅我的xslt for详情)

输入XML:

    <?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>

OutPut应该是:

            <?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>

1 个答案:

答案 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>

就这样,撒克逊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>