Question

I have an XML with following format.

<RootNode>
    <Parent>
        <Child1>Some text about child 1</Child1>
        <Child2>Some text about child 2</Child2>
        ....
        ....
        <Child5>Some text about child 5</Child5>
        ....
        ....
        <Childn>Some text about child n</Childn>
    </Parent>
</RootNode>

Now I want to validate this xml against xsd.

But the problem is the number and node name of children are not fixed.

Please check following samples of my file

Sample 1: Two children with node name Child1 and Child2

<RootNode>
    <Parent>
        <Child1>Some text about child 1</Child1>
        <Child2>Some text about child 2</Child2>
    </Parent>
</RootNode>

Sample 2: Three children with node name Child4 Child5, and Child8

<RootNode>
    <Parent>
        <Child4>Some text about child 4</Child4>
        <Child5>Some text about child 5</Child4>
        <Child8>Some text about child 8</Child8>
    </Parent>
</RootNode>

I want a to validate the text inside the Child node (which is shown as "Some text about child n") with following rule

Rule: The xml is invalid if any Child node - (Children of Parent) has iner text length more then 256.

It means consider the xml invalid if any child of the "Parent" node has inner text longer then 256 characters

Is this possible using XSD schema validation? Could you please help me creating .XSD file to validate this?

Thanks in advance

Was it helpful?

Solution

You can't solve that with XSD as said before simply because you don't know what all the element names will be in advance. If you are not totally tied to XSD validation you may wish to take a look at Schematron which can do exactly this sort of validation.

OTHER TIPS

It's not possible to validate this automatically because the maximum number of child nodes is unknown. You may be able to get by writing some manual validation.

If you are able to change the format of this XML, you should, because it's badly designed (not least of all because it's not validatable...). You should shoot for something more like:

<RootNode>
    <Parent>
        <Child num="1">...</Child>
        <Child num="2">...</Child>
    </Parent>
</RootNode>

or, if the parent can contain other things as well as the children, then having a container for the children is useful:

<RootNode>
    <Parent>
        <OtherStuff/>
        <Children>
            <Child num="1">...</Child>
            <Child num="2">...</Child>
        </Children>
        <MoreStuff/>
    </Parent>
</RootNode>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top