繁体   English   中英

如何计算节点集的最大字符串长度?

[英]How to calculate max string-length of a node-set?

我正在尝试使用XSLT将XML文档转换为纯文本表供人类使用。 我正在使用xsltproc ,它仅实现XSLT 1.0(所以max实际上来自EXSLT)。

我尝试了以下操作,但注释掉的定义失败,因为string-length仅返回单个值( 第一个节点的字符串值的长度),而不是max想要的节点集。

转型:

<?xml version="1.0" encoding="utf-8"?>
<!-- vim: set sts=2 sw=2: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math" xmlns:str="http://exslt.org/strings">
  <xsl:output method="text"/>
  <xsl:template match="/root">
    <!-- <xsl:variable name="max_a_width" select="math:max(string-length(data/@a))"/> -->
    <xsl:variable name="max_a_width" select="string-length(data/@a)"/>
    <xsl:text>+-</xsl:text><xsl:value-of select="str:padding($max_a_width, '-')"/><xsl:text>-+&#10;</xsl:text>
    <xsl:for-each select="data">
      <xsl:text>| </xsl:text><xsl:value-of select="@a"/><xsl:value-of select="str:padding($max_a_width - string-length(@a), ' ')"/><xsl:text> |&#10;</xsl:text>
    </xsl:for-each>
    <xsl:text>+-</xsl:text><xsl:value-of select="str:padding($max_a_width, '-')"/><xsl:text>-+&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

输入:

<?xml version="1.0" encoding="utf-8"?>
<!-- vim: set sts=2 sw=2: -->
<root>
  <data a="aa"/>
  <data a="aaa"/>
  <data a="a"/>
</root>

输出:

+----+
| aa |
| aaa |
| a  |
+----+

为了使右边界对齐,我需要在变量中具有实际的最大值。 (在我的实际示例中,我将具有列标题和多列,但是重现该问题不是必需的)。

如果可以更轻松地找到解决方案,则可以保证数据值永远不会包含空格。

<xsl:variable name="max_a_width">
  <xsl:for-each select="data">
    <xsl:sort select="string-length(@a)" data-type="number" />
    <xsl:if test="position() = last()">
      <xsl:value-of select="string-length(@a)" />
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

这是从XSLT 1.0中的派生值的有序列表中进行选择的通用方法。

如果要从实际(可排序的本地)值中选择最小值/最大值,则可以采用以下捷径:

<xsl:variable name="max_a" select="//a[not(. &lt; //a)][1]" />

这是一个简单的递归模板。 使用与输入相同的XSLT文件运行它以对其进行测试。 使用XSLT 2或XQuery当然更容易...

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template name="maxlen"> <xsl:param name="input" /> <xsl:param name="max-so-far" select="0" /> <xsl:choose> <xsl:when test="not($input)"> <xsl:value-of select="$max-so-far" /> </xsl:when> <xsl:when test="string-length($input[1]) &gt; $max-so-far"> <xsl:call-template name="maxlen"> <xsl:with-param name="input" select="$input[position() &gt; 1]" /> <xsl:with-param name="max-so-far" select="string-length($input[1])" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:call-template name="maxlen"> <xsl:with-param name="input" select="$input[position() &gt; 1]" /> <xsl:with-param name="max-so-far" select="$max-so-far" /> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:variable name="input"> <test> <string>123456789012345678901234</string> <string>12345678901234</string> <string>1234567890</string> <string>12345678901234567890123456</string> </test> </xsl:variable> <xsl:template match="/"> <output> <xsl:call-template name="maxlen"> <xsl:with-param name="input" select="//test/string" /> </xsl:call-template> </output> </xsl:template> </xsl:stylesheet> 

这是使用EXSLT math:max()执行此操作的方法:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:math="http://exslt.org/math" 
xmlns:str="http://exslt.org/strings">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/root">
    <xsl:variable name="lengths">
        <xsl:for-each select="data">
            <length><xsl:value-of select="string-length(@a)"/></length>
        </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="max_a_width" select="math:max(exsl:node-set($lengths)/length)"/>
    <!-- the rest -->
</xsl:template>

</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM