Topic
9 replies Latest Post - ‏2012-11-11T09:26:53Z by SystemAdmin
SystemAdmin
SystemAdmin
6772 Posts
ACCEPTED ANSWER

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
    1300 Posts
    ACCEPTED ANSWER

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

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

    Ken
    • SystemAdmin
      SystemAdmin
      6772 Posts
      ACCEPTED ANSWER

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

      ‏2012-11-10T11:39:15Z  in response to kenhygh
      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
        ACCEPTED ANSWER

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

        ‏2012-11-10T12:03:58Z  in response to SystemAdmin
        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
        1300 Posts
        ACCEPTED ANSWER

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

        ‏2012-11-10T13:01:54Z  in response to SystemAdmin
        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
          ACCEPTED ANSWER

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

          ‏2012-11-10T13:12:05Z  in response to kenhygh
          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
            ACCEPTED ANSWER

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

            ‏2012-11-10T13:45:17Z  in response to SystemAdmin
            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
              4119 Posts
              ACCEPTED ANSWER

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

              ‏2012-11-10T14:25:50Z  in response to SystemAdmin
              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
                4119 Posts
                ACCEPTED ANSWER

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

                ‏2012-11-10T14:37:20Z  in response to HermannSW
                $ 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
                  ACCEPTED ANSWER

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

                  ‏2012-11-11T09:26:53Z  in response to HermannSW
                  Thanks Hermann and Ken .... you guys helped alot in resoving the issue