Topic
  • 18 replies
  • Latest Post - ‏2015-03-18T14:52:03Z by Satish.K.M
SystemAdmin
SystemAdmin
6772 Posts

Pinned topic validating XML against a XSD in Data Power using dp:schema-validate()

‏2012-11-10T00:22:42Z |
I am trying to validate an XML which is in xml nodeset against an XSD.
Below mentioned is code snippet of my xslt.

Here cv_innerXML is the context variable which is loaded with XML and i am trying to validate this against some XSD.
But every time it returns value of varCount as '0' as per below code.
As per my understanding dp:schema-validate function will return validated XML and will retun NULL if XML cannot be validated.

I tried below code by loading $cv_innerXML with both valid and invalid XML (Checked using online XML validator) but it has never returned any validated XML . And varCount always returned as '0'.

<dp:set-variable name="'var://context/var/cv_innerXML'" value="$innerXML" />
<xsl:variable name = "validate">
<xsl:copy-of select ="dp:schema-validate('local:///abc.xsd',$cv_innerXML)'/>
</xsl:variable>
<xsl:variable name="varCount" select="count($validate/*)"/>

Can anyone suggest me here, how to use schema-validate function using some example code.
Updated on 2012-11-11T09:26:53Z at 2012-11-11T09:26:53Z by SystemAdmin
  • kenhygh
    kenhygh
    1531 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T11:26:10Z  
    I suspect $cv_innerXML is not valid XML. What is its content?

    Ken
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T11:39:15Z  
    • kenhygh
    • ‏2012-11-10T11:26:10Z
    I suspect $cv_innerXML is not valid XML. What is its content?

    Ken
    Hi Ken As per DP Probe representation of cv_innerXM is as below

    Name Type Value
    var://context/var/cv_innerXML node-set (show nodeset)

    When i am Clicking on this Show nodeset , i can see the complete XML and separate i can validate this against an XSD.

    I am not able to figure out exactly what is the issue
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T12:03:58Z  
    Hi Ken As per DP Probe representation of cv_innerXM is as below

    Name Type Value
    var://context/var/cv_innerXML node-set (show nodeset)

    When i am Clicking on this Show nodeset , i can see the complete XML and separate i can validate this against an XSD.

    I am not able to figure out exactly what is the issue
    I am using DP Firmware Rev:XI50.3.8.2.11. Does this function support this version of DP?

    Hermann/ Ken Please suggest me how to proceed?
  • kenhygh
    kenhygh
    1531 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T13:01:54Z  
    Hi Ken As per DP Probe representation of cv_innerXM is as below

    Name Type Value
    var://context/var/cv_innerXML node-set (show nodeset)

    When i am Clicking on this Show nodeset , i can see the complete XML and separate i can validate this against an XSD.

    I am not able to figure out exactly what is the issue
    Can you post your XML? The real XML? and maybe your XSD so I can try it on my own? They'll probably work best as attachments.

    In your code snippet, you're validating $cv_innerXML but in your probe you're looking at $innerXML.

    Ken
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T13:12:05Z  
    • kenhygh
    • ‏2012-11-10T13:01:54Z
    Can you post your XML? The real XML? and maybe your XSD so I can try it on my own? They'll probably work best as attachments.

    In your code snippet, you're validating $cv_innerXML but in your probe you're looking at $innerXML.

    Ken
    Hi Ken

    As per my probe i am looking on
    Name Type Value
    var://context/var/*cv_innerXML* node-set (show nodeset)

    and as per my code i am also validating
    <xsl:variable name="validate">
    <xsl:copy-of select="dp:schema-validate('local:///host/dcs/SimplificationSchema.xsd', *$cv_innerXML*)" />
    </xsl:variable>

    So i think i am validating correct variable.
    Due some security policy i cannot post XSD and XML. I know without those its very difficult to help.

    If you can share any sample XML which can be validated against an XSD using schema validate method. I can also try that.
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T13:45:17Z  
    Hi Ken

    As per my probe i am looking on
    Name Type Value
    var://context/var/*cv_innerXML* node-set (show nodeset)

    and as per my code i am also validating
    <xsl:variable name="validate">
    <xsl:copy-of select="dp:schema-validate('local:///host/dcs/SimplificationSchema.xsd', *$cv_innerXML*)" />
    </xsl:variable>

    So i think i am validating correct variable.
    Due some security policy i cannot post XSD and XML. I know without those its very difficult to help.

    If you can share any sample XML which can be validated against an XSD using schema validate method. I can also try that.
    Hi Ken,

    Please find attached sample files and below is the code snippet

    <xsl:variable name="validate">
    <xsl:copy-of select="dp:schema-validate('local:///RnD/shiporder.xsd', $cv_innerXML)" />
    </xsl:variable>

    shiporder.xml is getting passed as inner XML which is loaded in $cv_innerXML variable which i am trying to validate against shiporder.xsd .

    Please help me in resolving this.

    Attachments

  • HermannSW
    HermannSW
    4657 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T14:25:50Z  
    Hi Ken,

    Please find attached sample files and below is the code snippet

    <xsl:variable name="validate">
    <xsl:copy-of select="dp:schema-validate('local:///RnD/shiporder.xsd', $cv_innerXML)" />
    </xsl:variable>

    shiporder.xml is getting passed as inner XML which is loaded in $cv_innerXML variable which i am trying to validate against shiporder.xsd .

    Please help me in resolving this.
    Hi Pradeep,

    you do not have a Schema validation problem, you just have a variable access probelem.

    You can use XSLT variables and Context variables in DataPower XSLT processor.

    XSLT variables are much more performant, but are only usable in a single Transform action.
    Context variables can store values across the whole processing policy.
    And in contrast to XSLT variables, you can modify the variable's value.

     
    You define XSLT variables by:
    <xsl:variable name="varname" select="something"/>
    or
    <xsl:variable name="varname">
    ...
    </xsl:variable>

    You access the just defined XSLT variable by "$varname".

     
    You define or change the value of a context variable by:
    <dp:set-variable name="var://context/var/varname" value="something"/>

    You access the value of that context variable by:
    <xsl:copy-of select="dp:variable('var://context/var/varname')"/>

     
    What does this mean for your code above?
    Both of these expressions will work:
    
    <xsl:copy-of select=
    "dp:schema-validate('local:///abc.xsd',$innerXML)"/> <xsl:copy-of select=
    "dp:schema-validate('local:///abc.xsd',dp:variable('var://context/var/cv_innerXML'))"/>
    


     
    Let me use this very simple Schema as an example (it is store in local:/// folder on DataPower box):
    
    $ cat default.xsd <xs:schema xmlns:xs=
    "http://www.w3.org/2001/XMLSchema"> <xs:element name=
    "elem" type=
    "xs:string" default=
    "def"/> </xs:schema> $
    

    The first sample just shows you the return of the validated XML.
    The second sample shows the empty nodeset being returned for an invalid input.
    The last sample shows that the "validated nodeset" returned sometimes is NOT the identity.
    In this case the default (string) value from the Schema got added.
    
    $ echo 
    "<elem>123</elem>" | coproc2 default.xsl - http:
    //dp5-l3:2223 ; echo <elem>123</elem> $ $ echo 
    "<elem1/>" | coproc2 default.xsl - http:
    //dp5-l3:2223 ; echo   $ echo 
    "<elem/>" | coproc2 default.xsl - http:
    //dp5-l3:2223 ; echo <elem>def</elem> $
    


     
    Hermann<myXsltBlog/> <myXsltTweets/>
  • HermannSW
    HermannSW
    4657 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-10T14:37:20Z  
    • HermannSW
    • ‏2012-11-10T14:25:50Z
    Hi Pradeep,

    you do not have a Schema validation problem, you just have a variable access probelem.

    You can use XSLT variables and Context variables in DataPower XSLT processor.

    XSLT variables are much more performant, but are only usable in a single Transform action.
    Context variables can store values across the whole processing policy.
    And in contrast to XSLT variables, you can modify the variable's value.

     
    You define XSLT variables by:
    <xsl:variable name="varname" select="something"/>
    or
    <xsl:variable name="varname">
    ...
    </xsl:variable>

    You access the just defined XSLT variable by "$varname".

     
    You define or change the value of a context variable by:
    <dp:set-variable name="var://context/var/varname" value="something"/>

    You access the value of that context variable by:
    <xsl:copy-of select="dp:variable('var://context/var/varname')"/>

     
    What does this mean for your code above?
    Both of these expressions will work:
    <pre class="jive-pre"> <xsl:copy-of select= "dp:schema-validate('local:///abc.xsd',$innerXML)"/> <xsl:copy-of select= "dp:schema-validate('local:///abc.xsd',dp:variable('var://context/var/cv_innerXML'))"/> </pre>

     
    Let me use this very simple Schema as an example (it is store in local:/// folder on DataPower box):
    <pre class="jive-pre"> $ cat default.xsd <xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema"> <xs:element name= "elem" type= "xs:string" default= "def"/> </xs:schema> $ </pre>
    The first sample just shows you the return of the validated XML.
    The second sample shows the empty nodeset being returned for an invalid input.
    The last sample shows that the "validated nodeset" returned sometimes is NOT the identity.
    In this case the default (string) value from the Schema got added.
    <pre class="jive-pre"> $ echo "<elem>123</elem>" | coproc2 default.xsl - http: //dp5-l3:2223 ; echo <elem>123</elem> $ $ echo "<elem1/>" | coproc2 default.xsl - http: //dp5-l3:2223 ; echo $ echo "<elem/>" | coproc2 default.xsl - http: //dp5-l3:2223 ; echo <elem>def</elem> $ </pre>

     
    Hermann<myXsltBlog/> <myXsltTweets/>
    $ cat default.xsl
    <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:copy-of select ="dp:schema-validate('local:///default.xsd', *)"/>
      </xsl:template>
      
    </xsl:stylesheet>
    $
    
    Updated on 2014-03-25T02:45:59Z at 2014-03-25T02:45:59Z by iron-man
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2012-11-11T09:26:53Z  
    • HermannSW
    • ‏2012-11-10T14:37:20Z
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">$ cat default.xsl <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:copy-of select ="dp:schema-validate('local:///default.xsd', *)"/> </xsl:template> </xsl:stylesheet> $ </pre>
    Thanks Hermann and Ken .... you guys helped alot in resoving the issue
  • Satish.K.M
    Satish.K.M
    5 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-17T17:55:49Z  
    • HermannSW
    • ‏2012-11-10T14:37:20Z
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">$ cat default.xsl <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:copy-of select ="dp:schema-validate('local:///default.xsd', *)"/> </xsl:template> </xsl:stylesheet> $ </pre>

    Hermann,

    I'm unable to validate schema with the schema in a variable and xml in a variable. the schema is located on the database table.

    <xsl:copy-of select="dp:schema-validate($schemaDoc, $xmlDoc" />

    Getting this error ==> "Internal error: Expected root event". Please suggest.

     

     

  • HermannSW
    HermannSW
    4657 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-17T19:39:16Z  

    Hermann,

    I'm unable to validate schema with the schema in a variable and xml in a variable. the schema is located on the database table.

    <xsl:copy-of select="dp:schema-validate($schemaDoc, $xmlDoc" />

    Getting this error ==> "Internal error: Expected root event". Please suggest.

     

     

    Please add

    <xsl:message dp:priority="alert"><xsl:value-of select="..."></xsl:message>
    

    statements for $schemaDoc and $xmlDoc to your stylesheet and verify 1st that your variables contain what you want them to contain. Missing root event typically means that the Schema document provided is no valid Schema document.


    Hermann.

    Updated on 2015-03-17T19:39:59Z at 2015-03-17T19:39:59Z by HermannSW
  • Satish.K.M
    Satish.K.M
    5 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-17T21:26:14Z  
    • HermannSW
    • ‏2015-03-17T19:39:16Z

    Please add

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr"><xsl:message dp:priority="alert"><xsl:value-of select="..."></xsl:message> </pre>

    statements for $schemaDoc and $xmlDoc to your stylesheet and verify 1st that your variables contain what you want them to contain. Missing root event typically means that the Schema document provided is no valid Schema document.


    Hermann.

    Same issue. I tried validating the schema and the XML outside using XMLSpy & Java, it worked just fine. I have no clue how to solve this now. Anyway thank you for the reply.

  • HermannSW
    HermannSW
    4657 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-17T23:56:30Z  

    Same issue. I tried validating the schema and the XML outside using XMLSpy & Java, it worked just fine. I have no clue how to solve this now. Anyway thank you for the reply.

    please add the messages as requested and let us know the log output.

  • HermannSW
    HermannSW
    4657 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-18T08:14:11Z  
    • HermannSW
    • ‏2015-03-17T23:56:30Z

    please add the messages as requested and let us know the log output.

    Looking into this again your previous statements show what you do wrong:
    with the schema in a variable 

    http://www-01.ibm.com/support/knowledgecenter/SS9H2Y_5.0.0/com.ibm.dp.xs.doc/extensionfunctions80.htm%23schema-validate_function

    Parameters

    schema
    (xs:string) Identifies the XSD schema to perform the validation.

    ...

    Examples

    
    ...
    <xsl:when test="$Ready">
      <xsl:copy-of select="dp:schema-validate("store:///headers.xsd",
        $headerOutput)" />
    </xsl:when>
    ...
    
     

     

    So the first argument of  dp:schema-validate() identifies the Schema to be used, it is not the Schema content.

     

    Hermann.

  • Satish.K.M
    Satish.K.M
    5 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-18T14:12:36Z  
    • HermannSW
    • ‏2015-03-18T08:14:11Z

    Looking into this again your previous statements show what you do wrong:
    with the schema in a variable 

    http://www-01.ibm.com/support/knowledgecenter/SS9H2Y_5.0.0/com.ibm.dp.xs.doc/extensionfunctions80.htm%23schema-validate_function

    Parameters

    schema
    (xs:string) Identifies the XSD schema to perform the validation.

    ...

    Examples

    <pre class="xmp"> ... <xsl:when test="$Ready"> <xsl:copy-of select="dp:schema-validate("store:///headers.xsd", $headerOutput)" /> </xsl:when> ... </pre>
     

     

    So the first argument of  dp:schema-validate() identifies the Schema to be used, it is not the Schema content.

     

    Hermann.

    Thanks Herman,

    but that's not our desing to read from a file, schema varies per client.

    The DP documentation seems to be very poor. 

    Here is the error message.

    dp:schema-validate(): Fatal Error at local:///DataPower/Environment/DevelopmentTest/Custom/schemavalidator.xsl:29: Internal error: Expected root event. 

  • HermannSW
    HermannSW
    4657 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-18T14:21:20Z  

    Thanks Herman,

    but that's not our desing to read from a file, schema varies per client.

    The DP documentation seems to be very poor. 

    Here is the error message.

    dp:schema-validate(): Fatal Error at local:///DataPower/Environment/DevelopmentTest/Custom/schemavalidator.xsl:29: Internal error: Expected root event. 

    but that's not our desing
    >
    then your design is wrong, and you need to change it.

    The documentation clearly stated  (xs:string) Identifies the XSD schema to perform the validation. and you have to live with that.

    Alternatively you can read the Schema and store it into a context, and then refer to that context in a validate action.


    Hermann.

  • kenhygh
    kenhygh
    1531 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-18T14:35:25Z  

    Thanks Herman,

    but that's not our desing to read from a file, schema varies per client.

    The DP documentation seems to be very poor. 

    Here is the error message.

    dp:schema-validate(): Fatal Error at local:///DataPower/Environment/DevelopmentTest/Custom/schemavalidator.xsl:29: Internal error: Expected root event. 

    uh, if your schema changes for each client, you may want to rethink that. You're building a point-to-point solution.

  • Satish.K.M
    Satish.K.M
    5 Posts

    Re: validating XML against a XSD in Data Power using dp:schema-validate()

    ‏2015-03-18T14:52:03Z  
    • HermannSW
    • ‏2015-03-18T14:21:20Z

    but that's not our desing
    >
    then your design is wrong, and you need to change it.

    The documentation clearly stated  (xs:string) Identifies the XSD schema to perform the validation. and you have to live with that.

    Alternatively you can read the Schema and store it into a context, and then refer to that context in a validate action.


    Hermann.

    Context variable didn't work either. So I'm out of luck I guess. Thanks all for your help.