web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Validate XML using multiple XSD

(0) ShareShare
ReportReport
Posted on by 1,421

Hi all,

I need to validate an XML file, that contains different XSD target namespaces, using a related XSD. I tried to merge XSD files and run the XMLReader class (method validate) passing the merged file but I received validation error (although the XML is correct).

Can anyone suggest me a way to validate my XML?

Thanks and regards,

Giuseppe

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Does it work elsewhere, e.g. if you run the validation in a XML editor? It seems more likely that your XSD file isn't correct rather than that XMLReader is broken.

  • Community Member Profile Picture
    on at

    Hi,

    I have the same problem; I'm sure the XSDs are well formed and the XML file is valid, but the validation in AX fails.

    I have tried to validate the XML with:

    I have the XSDs like these:

    <!-- File1.xsd -->
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="www.w3.org/.../XMLSchema" xmlns:PMRQ="TargetNamespace2" xmlns="TargetNamespace1" targetNamespace="TargetNamespace1" elementFormDefault="qualified">
    <xs:import namespace="TargetNamespace2" schemaLocation="File2.xsd"/>
    <!-- XSD -->
    </xs:schema>

    <!-- File2.xsd -->
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="www.w3.org/.../XMLSchema" xmlns="TargetNamespace2" xmlns:ns1="urn:CBI:xsd:CBISEDADReqLogMsg" targetNamespace="TargetNamespace2" elementFormDefault="qualified">
    <!-- XSD -->
    </xs:schema>

    <!-- File XML -->
    <root xmlns:xsi="www.w3.org/.../XMLSchema-instance" xmlns="TargetNamespace1" xsi:schemaLocation="TargetNamespace1 File1.xsd">
    <!-- ... -->
    <ns2:Req xmlns:ns2="TargetNamespace1">
                <ns2:LogMsg>
                    <ns3:GrpHdr xmlns:ns3="TargetNamespace2">
    <!-- ... -->
    </root>


    I tried to merge the XSDs into one and validate the XML with it (using http://www.utilities-online.info/xsdvalidation), but the validation still fails. The only way that i found to validate with success the XML is to edit the XML file like this:

    <!-- File XML -->
    <root xmlns:xsi="www.w3.org/.../XMLSchema-instance" xmlns="TargetNamespace1" xsi:schemaLocation="TargetNamespace1 MergedFile.xsd">
    <!-- ... -->
    <ns2:Req xmlns:ns2="TargetNamespace1">
                <ns2:LogMsg>
                    <ns3:GrpHdr xmlns:ns3="TargetNamespace1">
    <!-- ... -->
    </root>

    But, since I receive many XML files, I don't want to process each files in order to replace each attribute xmlns:nsX="TargetNamespace2" with xmlns:nsX="TargetNamespace1"


    Thanks and regards,

    Stefano

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Do I understand correctly that your merged XSD doesn't work anywhere and you test in Altova a .NET was actually about a different scenario?

    I really wouldn't expect MergedFile.xsd to get used if the schema location is set to a different file.

  • Community Member Profile Picture
    on at

    I have successfully validated the XML with Altova and C# class using the two XSDs, but AX seems not work with this scenario, because the validation always fails.

    So I have tried to merge the xsd editing the File1.xsd by copying in it the definitions of the File2.xsd; the result was

    <!-- MergedFile.xsd -->
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="www.w3.org/.../XMLSchema" xmlns="TargetNamespace1" targetNamespace="TargetNamespace1" elementFormDefault="qualified">
    <!-- <xs:import namespace="TargetNamespace2" schemaLocation="File2.xsd"/> -->
    <!-- XSD -->
    </xs:schema>

    Why I should not use the MergetFile.xsd? It does not work with the original XML, but after the edit, the MergedFile.xsd succesfully validates the XML ... I think the problem is related to the referenced targetNamespace inside the XML attributes; I'm wrong?

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    If you have to replace one namespace to another to fulfil your schema, then your schema obviously doesn't match the file. If you don't want to change the file, you have to change the schema.

  • Community Member Profile Picture
    on at

    Yes, it's true. So the question is: why Dynamics AX can't validate the original xml file using the two original XSDs?
    I've do this:

    Set                         permissionSet;
    FileName                    xsdFile;
    XMLReader                   xmlReader;
    XMLSchema                   xmlSchema;
    
    xsdFile = ".../myFolder/File1.xsd";
    permissionSet =  new Set(Types::Class);
    permissionSet.add(new FileIoPermission(xsdFile, #io_read));
    permissionSet.add(new FileIoPermission((".../myFolder/File.xml", #io_read));
    CodeAccessPermission::assertMultiple(permissionSet);
    
    xmlSchema   = XmlSchema::newFile(xsdFile);
    if(xmlSchema)
    {
        xmlReader = XMLReader::newFile(".../myFolder/File.xml");
    
        try
        {
            validationError = xmlReader.validate(xmlSchema.writeToString());
        }
        catch
        {
            error("Error!");
        }
    }

    The validate throws an error: Type 'TargetNamespace2' is not defined

    Why does this happens? The XSD file with the TargetNamespace2 is imported into the File1.xsd, used for the validation. The files (both XSDs and XML) are in the same folder.

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Could you give me sample files that I could use, without writing them by myself? The snippets above are not useful for this purpose.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans