如何检查给定的逗号分隔值是否是升序的数字序列

时间:2019-01-18 17:04:27

标签: xslt-2.0

我想检查给定的逗号分隔值是否是升序的数字序列。

输入XML:

  <?xml version="1.0" encoding="UTF-8"?>
  <root>
     <isSequence>
        <seq name="a" value="1, 2, 3"/>
        <seq name="b" value="3, 1, 5, 6"/>
        <seq name="c" value="15, 16, 18, 0"/>
        <seq name="d" value="21, 22, 23, 24, 25"/>
        <seq name="e" value="A, B, C"/>
     </isSequence>
  </root>

我尝试了以下代码

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
        version="2.0">
        <xsl:template match="/">
            <xsl:for-each select="root/isSequence/seq">
                <xsl:if test="subsequence(@value, 0)">
                    <xsl:value-of select="@name"/> is an ascending numeric sequence
                </xsl:if>             
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>

预期结果

a is an ascending numeric sequence
d is an ascending numeric sequence

1 个答案:

答案 0 :(得分:1)

假设您正在寻找整数序列,那么我认为(在XSLT 3中)可以表示为

functions.php

完整示例(https://xsltfiddle.liberty-development.net/6r5Gh2H

add_filter( 'allowed_http_origins', 'add_allowed_origins' );
function add_allowed_origins( $origins ) {
    $origins[] = 'https://site1.example.com';
    $origins[] = 'https://site2.example.com';
    return $origins;
}

在带有XPath 2的XSLT 2中,我认为您不能以匹配模式来紧凑地表达它,但是您当然可以在XSLT级别使用变量:

  <xsl:template match="seq[let $items := tokenize(@value, ',\s+')!xs:integer(.) return every $p in 1 to count($items) satisfies $items[$p] = $items[1] + ($p - 1)]">
      <xsl:value-of select="@name"/><xsl:text> is an ascending numeric sequence
</xsl:text>
  </xsl:template>

http://xsltransform.hikmatu.com/bFDb2BU