John
John

Reputation: 99

Validating xml attribute value with dynamic length

I have the following xml part:

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

I would like to check with XSLT 2.0 that a customervalue doesn't exceed the specified length (that is also present in the XML).

What I have so far is this

<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>

at first I only used '>@length' but I changed it to 'number(@length)' thinking that it might be interpreted as string but that didn't help. When I change the 'number(@length)' or the '@length' to a fixed number let's say 17 it works.

Any ideas are very welcome.

Upvotes: 0

Views: 221

Answers (1)

Tim C
Tim C

Reputation: 70638

When number(@length) is evaluated, it is in the context of the companyvalue attribute. Effectively it is looking for the length attribute on the companyvalue attribute, not the Company element.

You need to do this...

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

Or maybe this...

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

Upvotes: 1

Related Questions