【问题标题】:In XSLT, How do I split the text content of an element into lines?在 XSLT 中,如何将元素的文本内容拆分为行?
【发布时间】:2012-03-21 14:01:45
【问题描述】:

我正在使用 XSLT 来解析 XML 元素的文本内容。此文本包含换行符,但我似乎无法正确解析它们。我正在使用我在网上找到的代码来分割文本。这是代码的相关部分。

<xsl:variable name="first">
  <xsl:value-of select="substring-before($source, $newline)"/>
</xsl:variable>
<xsl:variable name="rest">
  <xsl:value-of select="substring-after($source, $newline)"/>
</xsl:variable>

这是一个将 $rest 推入自身的 recusive 模板的一部分。

问题在于代码示例没有定义 $newline。 如果我将 $newline 设置为一个字母,例如 's',则文本会很好地拆分(例如,它将输入“响亮”变成“re”和“ounding”)。但是当我尝试将 $newline 设置为换行符时,即&amp;#xa;&amp;#xa0;,它会永远递归并给我一个堆栈溢出。我也尝试为换行定义一个实体,但没有区别。

输入在每行的末尾都有普通的 CR/LF(我在 Windows 盒子上)。

我做错了什么?

【问题讨论】:

    标签: string xslt character


    【解决方案1】:

    如果您可以使用 EXSLT,请尝试使用 str:tokenize

    <xsl:for-each select="str:tokenize($source, $newline)">
      <xsl:value-of select="."/>
      <xsl:text>&#x0a;</xsl:text>
    </xsl:for-each>
    

    或与 XSLT 2.0 类似:

    <xsl:for-each select="tokenize($source, $newline)">
      <xsl:sequence select="."/>
      <xsl:text>&#x0a;</xsl:text>
    </xsl:for-each>
    

    【讨论】:

    • 遗憾的是,我都没有。我正在从 Ant 脚本调用 XSLT。
    【解决方案2】:

    您也许可以使用以下内容。

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="/">
            <root>
                <xsl:for-each select="root/str">
                    <str>
                        <xsl:call-template name="strSplit">
                            <xsl:with-param name="str" select="."/>
                            <xsl:with-param name="seqno" select="1"/>
                        </xsl:call-template>
                    </str>
                </xsl:for-each>
            </root>
       </xsl:template>
    
        <xsl:template name="strSplit">
            <xsl:param name="str"/>
            <xsl:param name="seqno"/>
    
            <xsl:variable name="afterLeadingWS"
                select="substring-after($str, substring-before($str,substring-before(normalize-space($str), ' ')))"/>
    
            <xsl:choose>
                <xsl:when test="contains($afterLeadingWS, '&#xA;')">
                    <line>
                        <xsl:attribute name="seqno"><xsl:value-of select="$seqno"/></xsl:attribute>
                        <xsl:attribute name="length"><xsl:value-of select="string-length(substring-before($afterLeadingWS, '&#xA;'))"/></xsl:attribute>
                        <xsl:value-of select="substring-before($afterLeadingWS, '&#xA;')"/>
                    </line>
                    <xsl:call-template name="strSplit">
                        <xsl:with-param name="str" select="substring-after($afterLeadingWS, '&#xA;')"/>
                        <xsl:with-param name="seqno" select="$seqno + 1"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <line>
                        <xsl:attribute name="seqno"><xsl:value-of select="$seqno"/></xsl:attribute>
                        <xsl:value-of select="$afterLeadingWS"/>
                    </line>
                </xsl:otherwise>
            </xsl:choose>
       </xsl:template>
    </xsl:stylesheet>
    

    适用于

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <str>
            yigifgniuq  h 
            eukwgf kuew hgk.uhgku
            ,/v.,silghouihhg
        </str>
        <str>
            09734ymmnyr n.0808
            o149013483ymr7rg
            738924m c0 
    
        </str>
    </root>
    

    输出结果是

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <str>
            <line seqno="1" length="13">yigifgniuq  h </line>
            <line seqno="2" length="21">eukwgf kuew hgk.uhgku</line>
            <line seqno="3" length="18">        ,/v.,silghouihhg</line>
            <line seqno="4">    </line>
        </str>
        <str>
            <line seqno="1" length="18">09734ymmnyr n.0808</line>
            <line seqno="2" length="16">o149013483ymr7rg</line>
            <line seqno="3" length="11">738924m c0 </line>
            <line seqno="4" length="2">     </line>
            <line seqno="5">    </line>
        </str>
    </root>
    

    请注意,前导制表符(或空白)被视为行的一部分。

    【讨论】:

    • 这看起来很有帮助...但是我不想输出标记化的行,而是想知道最长行的长度。
    • 因此添加另一个属性: - 见上面的编辑
    • 太棒了,我能够修改它以将原始地址(由新行分隔)拆分为 &lt;Address1&gt;blah&lt;/Address1&gt;&lt;Address2&gt;...
    【解决方案3】:

    Maestro13 的回答让我最接近,我最终将我拥有的模板与他的模板合并,制作了这个,我在这里分享给后代。它是一个模板,它返回您传递给它的字符串中最长行的长度。

    <xsl:template name="longestCodeLine">
        <xsl:param name="str"/>
    
        <xsl:choose>
        <!-- Is this the last line? -->
        <xsl:when test="contains($str, '&#xA;')">
            <!-- No. First isolate all remaining lines, and recurse to find its longest line. -->
            <xsl:variable name="bestOfTheRest">
                <xsl:call-template name="longestCodeLine">
                    <xsl:with-param name="str" select="substring-after($str, '&#xA;')"/>                        
                </xsl:call-template>
            </xsl:variable>
            <xsl:choose>
                <!-- Compare the longest of the remaining lines to this one. Which one's longer? -->
                <!-- If the longest of the remaining lines is longer, return that line. -->
                <xsl:when test="string-length($bestOfTheRest) &gt; string-length(substring-before($str, '&#xA;'))">
                    <xsl:value-of select="$bestOfTheRest"/>
                </xsl:when>
                <!-- If this line longer, return this line. -->
                <xsl:otherwise>
                    <xsl:value-of select="substring-before($str, '&#xA;')"/>
                </xsl:otherwise>
            </xsl:choose>
                </xsl:when>
        <!-- If there are no \n's left, this is your last string. So it is by definition the longest one left. -->
        <xsl:otherwise>
            <xsl:value-of select="$str"/>
        </xsl:otherwise>
        </xsl:choose>   
    </xsl:template>
    

    【讨论】:

    • 您好 Mathijs,感谢您分享您的最终代码。我将研究这一点以及“#xA;”之间的区别“0x0a”对我来说不是很清楚。我也必须跟进。最好的问候,彼得 +1
    【解决方案4】:

    我用过这个模板一次。它是一个命名模板,因此您可以在需要时调用它。这里的文字被分成 70 个字符:

    <xsl:template name="Texts">
        <xsl:param name="string" select="TEXTITEM" />
        <xsl:param name="line-length" select="70"/>
        <xsl:variable name="line" select="substring($string,1,$line-length)"/>
        <xsl:variable name="rest" select="substring($string, $line-length+1)"/>
        <xsl:if test="$line">
            <MYTEXT> 
                <xsl:value-of select="$line"/>  
            </MYTEXT> 
        </xsl:if>
        <xsl:if test="$rest">
            <xsl:call-template name="Texts">
                <xsl:with-param name="string" select="$rest"/>
                <xsl:with-param name="line-length" select="$line-length"/>
            </xsl:call-template>
        </xsl:if>   
    </xsl:template>
    

    【讨论】:

    • 对不起,我忘了提到换行符:尝试使用 ASCII CR+LF 的“0x0d 0x0a”。所以 $newline="0x0d 0x0a" 最好的问候,彼得
    • 这会将它切成等长的块,这不是我需要的。我真正想要实现的是找出哪条线最长。
    • 如果我将 $newline 设置为值 0x0d0x0a,$first 和 $rest 都将变为空。如果我将其设置为 \n 或 \n\r,则相同。或者如果我说
    • ——或者如果我只是按 Enter 键。在 xsl:variable 标记内。
    【解决方案5】:

    我想我会添加一个在空格后添加换行符的行拆分代码。

    <xsl:function name="kode:splitLongLine">
      <xsl:param name="string"/>
      <xsl:variable name="regex">
       <xsl:text>(((.){1,55})( |$))</xsl:text>
      </xsl:variable>
    
      <xsl:variable name="result">
       <xsl:analyze-string select="$string" regex="{$regex}">
        <xsl:matching-substring>
         <xsl:value-of select="concat(regex-group(1),'&#10;')"/>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
         <xsl:value-of select="concat('REPORT ERROR: ', .)"/>
        </xsl:non-matching-substring>
       </xsl:analyze-string>
      </xsl:variable>
    
      <xsl:sequence select="$result"/>
     </xsl:function>
    

    【讨论】:

      猜你喜欢
      • 2014-07-22
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多