在xlst中为每一行应用标记?

时间:2017-12-12 05:35:43

标签: xslt

如何为每一行应用标记

输入:

<预格式化>
GRANT JOHN阅读
EMP.SALARY
GRANT JOHN阅读
EMP.NAME
< /预格式化>

输出:

<预>
< code> GRANT JOHN READ< / code>
<代码> EMP.SALARY< /代码>
< code> GRANT JOHN READ< / code>
<代码> EMP.NAME< /代码>
< /预>

2 个答案:

答案 0 :(得分:0)

预格式标记的内容将是单个文本块。传递给模板,substring-before($ text,' ')将为您提供第一行,然后使用substring-after调用相同的函数,直到它为空。

答案 1 :(得分:0)

XSLT 2.0

你可以使用

<xsl:template match="preformat">
    <pre>
    <xsl:for-each select="tokenize(., '&#xa;')">
        <xsl:if test="normalize-space(.) ne ''">
             <code>
                 <xsl:value-of select="normalize-space(.)"/>
             </code>
        </xsl:if>
    </xsl:for-each>
    </pre>
</xsl:template>

您可以在http://xsltransform.hikmatu.com/eiQZDbh

看到此转化

也适用于 XSLT 1.0

<xsl:template match="preformat">
    <pre>
    <xsl:call-template name="code">
        <xsl:with-param name="data" select="."/>
    </xsl:call-template>
    </pre>
</xsl:template>

<xsl:template name="code">
    <xsl:param name="data"/>
    <xsl:choose>
        <xsl:when test="contains($data, '&#xa;')">
               <xsl:choose>
                   <xsl:when test="normalize-space(substring-before($data, '&#xa;')) = ''">
                       <xsl:if test="substring-after($data, '&#xa;')">
                           <xsl:call-template name="code">
                               <xsl:with-param name="data" select="substring-after($data, '&#xa;')"/>
                           </xsl:call-template>
                       </xsl:if>
                   </xsl:when>
                   <xsl:otherwise>
                       <code>
                           <xsl:value-of select="normalize-space(substring-before($data, '&#xa;'))"/>
                       </code>
                       <xsl:if test="substring-after($data, '&#xa;')">
                           <xsl:call-template name="code">
                               <xsl:with-param name="data" select="substring-after($data, '&#xa;')"/>
                           </xsl:call-template>
                       </xsl:if>
                   </xsl:otherwise>
               </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <code>
                <xsl:value-of select="normalize-space($data)"/>
            </code>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

您可以在http://xsltransform.hikmatu.com/gWcDMej

看到转型