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

Validating xml attribute value with dynamic length

我有以下 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>

起初我只使用'>@length',但我将其更改为'number(@length)',认为它可能被解释为字符串,但没有帮助。当我将 'number(@length)' 或“@length”更改为固定数字时,假设 17 有效。

非常欢迎任何想法。

number(@length) 被评估时,它在 companyvalue 属性的上下文中。实际上,它是在 companyvalue 属性上寻找 length 属性,而不是 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)]