使用动态长度验证xml属性值

时间:2015-03-16 20:06:10

标签: xml xslt substring

我有以下xml部分:

<Column customerfield="Title" companyfield="2241" 
        datatype="alphanumeric" length="17" 
        customervalue="Manager Sales Compensation Head Office" 
        companyvalue="Manager Sales Compensation Head Office" 
        remark=""/>

我想用XSLT 2.0检查客户值是否超过指定的长度(也存在于XML中)。

到目前为止我所拥有的是

<xsl:template match="Column[@companyfield='2241' and @datatype='alphanumeric' and @companyvalue[string-length()>number(@length)]]">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
            <xsl:attribute name="companyvalue">
                <xsl:value-of select="substring(@customervalue,1,17)"/>
            </xsl:attribute>
        <xsl:attribute name="remark">String value too long.</xsl:attribute>
    </xsl:copy>
</xsl:template>

起初我只使用'&gt; @length',但我将其更改为'number(@length)',认为它可能被解释为字符串,但没有帮助。当我将'number(@length)'或'@length'更改为固定数字时,让我们说17可行。

非常欢迎任何想法。

1 个答案:

答案 0 :(得分:1)

评估number(@length)时,它位于companyvalue属性的上下文中。实际上,它会在length属性上查找companyvalue属性,而不是Company元素。

你需要这样做......

Column[@companyfield='2241' 
       and @datatype='alphanumeric' 
       and @companyvalue[string-length() > number(../@length)]]

或许这......

Column[@companyfield='2241' 
       and @datatype='alphanumeric' 
       and string-length(@companyvalue) > number(@length)]
相关问题