Topic
  • 1 reply
  • Latest Post - ‏2013-01-12T23:25:09Z by HermannSW
samanderson
samanderson
172 Posts

Pinned topic check if xml element is integer in DP xslt code

‏2013-01-11T13:37:35Z |
Can I check if one value of whole xml is integer and reject request if it is not using integer using stylesheet.
Updated on 2013-01-12T23:25:09Z at 2013-01-12T23:25:09Z by HermannSW
  • HermannSW
    HermannSW
    4651 Posts

    Re: check if xml element is integer in DP xslt code

    ‏2013-01-12T23:25:09Z  
    > Can I check if one value of whole xml is integer and reject request if it is not using integer using stylesheet.
    >
    For the XML Schema spec:
    http://www.w3.org/TR/xmlschema-2/#integer

    integer has a lexical representation consisting of a finite-length sequence of decimal
    digits (#x30-#x39) with an optional leading sign. If the sign is omitted, "+" is assumed.

    This XPath expression looks good, but it is not:
    "not(concat(translate(substring(.,1,1),'0123456789+-',''),translate(substring(.,2),'0123456789','')))"

    
    $ echo 
    "<num>123456</num>" | xpath++ 
    "not(concat(translate(substring(.,1,1),'0123456789+-',''),translate(substring(.,2),'0123456789','')))" - 
    
    true $ echo 
    "<num>-273</num>" | xpath++ 
    "not(concat(translate(substring(.,1,1),'0123456789+-',''),translate(substring(.,2),'0123456789','')))" - 
    
    true $ echo 
    "<num>+12</num>" | xpath++ 
    "not(concat(translate(substring(.,1,1),'0123456789+-',''),translate(substring(.,2),'0123456789','')))" - 
    
    true $ echo 
    "<num>+</num>" | xpath++ 
    "not(concat(translate(substring(.,1,1),'0123456789+-',''),translate(substring(.,2),'0123456789','')))" - 
    
    true $
    


    "boolean(regexp:match(.,'^[+-]?[0-9]+$'))" is better:
    
    $ echo 
    "<num>+12</num>" | xpath++ 
    "boolean(regexp:match(.,'^[+-]?[0-9]+$'))" - 
    
    true $ echo 
    "<num>+</num>" | xpath++ 
    "boolean(regexp:match(.,'^[+-]?[0-9]+$'))" - 
    
    false $
    


    Alternatively *dp:schema-validate()" can be used (for more complicated Schema types).
    Find attached the numeric types Schemata I placed on my website for easy use.
    
    $ coproc2 validate.integer.xsl 123456.xml http:
    //dp3-l3:2223 ; echo 
    
    true $ $ coproc2 validate.short.xsl 123456.xml http:
    //dp3-l3:2223 ; echo 
    
    false $ $ cat validate.short.xsl <!DOCTYPE xsl:stylesheet [ <!ENTITY hsw 
    "'http://stamm-wilbrandt.de/en/forum/num/'"> ]> <xsl:stylesheet version=
    "1.0" xmlns:xsl=
    "http://www.w3.org/1999/XSL/Transform" xmlns:dp=
    "http://www.datapower.com/extensions" extension-element-prefixes=
    "dp" > <xsl:output omit-xml-declaration=
    "yes" /> <xsl:template match=
    "/"> <xsl:variable name=
    "result"> <xsl:copy-of select=
    "dp:schema-validate(concat(&hsw;,'short.xsd'),.)"/> </xsl:variable>   <xsl:value-of select=
    "count($result/*) > 0"/> </xsl:template> </xsl:stylesheet> $ $ diff validate.integer.xsl validate.short.xsl 14c14 <         select=
    "dp:schema-validate(concat(&hsw;,'integer.xsd'),.)"/> --- >         select=
    "dp:schema-validate(concat(&hsw;,'short.xsd'),.)"/> $
    


     
    Hermann<myXsltBlog/> <myXsltTweets/>

    Attachments