469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Xsd - xml must contain element with specific attribute value

7
Hi,
I have the following 3rd party xml which has to be validated:

Expand|Select|Wrap|Line Numbers
  1. <Items>
  2. <Item Id="1" Value=""/>
  3. <Item Id="2" Value="someValue"/>
  4. <Item Id="3" Value="someOtherValue"/>
  5. </Items>
  6.  
For later transformation (xslt) it is required that item Id=2 exists and its attribute Value may not be emptyString. All other items can and must be ignored since the set items can be enlarged or diminished in the future.
Is this possible with xsd? any clues?

Your help would greatly be appreciated!
May 11 '09 #1
9 7647
Dormilich
8,651 Expert Mod 8TB
I'm not sure if all your requirements are possible…

what you can do is
- force an attribute to be present (regardless of its content)
- same as above + define the type (string/number/…) of its content
- you can force a (one) specific content value (or a selection thereof) to be used

XML Schema Specification

you can "omit" the other elements by using the ANY declaration.
May 11 '09 #2
Jaguex
7
Hi Dormilich, thanx for your reply.

The structure of the xml is indeed easily validated with xsd as are type and content values with patterns or restrictions.
I can't get the "any" declaration to work tho since i have to declare element item in order to validate its structure and the any would allow "item" resulting in a conflict.


Thusfar I am considering 2 options for the problem:
Xpath but I am not sure it can be done in xsd: e.g. Items/Item[@Id='2'] != null
still googling about this option ;)

Using Xslt to transform xml first so only item[@Id='2'] remains in the xml and then applying the scheme.
May 11 '09 #3
Dormilich
8,651 Expert Mod 8TB
@Jaguex
If you're doing that, there's no need to validate the result, just let return the the content and check the result.

say,
Expand|Select|Wrap|Line Numbers
  1. //item[@Id='2']/@Value
if this returns some text (i.e. not the empty string or empty node-set), your requirement was fulfilled. you only need to make sure "Id" is a ID type attribute.
May 11 '09 #4
Jaguex
7
You are right but I forgot to mention that this is a bit in a recursive xml structure. It would be far more easy to transform first regardless and than validate. the Xml has to be imported and the process has to stop further transformations, db storage if the file is not valid. Ofcourse after informing the uploading user.

But, I prefer to do all validation in a scheme: xsd and existence of item[@Id=2] seems to me part of validation not of transformation. Do you have any clues how to intergrate such kind of xpath expression within the xsd?

Maybe I should break the problem into 2:
1) Exists(Item[@Id='2']
2] Exists(Item[@Id='2' and @Value != '']

Can any of the two be solved within xsd?
Cheers J.
May 11 '09 #5
Dormilich
8,651 Expert Mod 8TB
if you indeed transform the XML first, then you can easily apply an XSD. your Id can only have the value 2 (otherwise the XSLT is faulty), there must be only one such element and you should be able to check for a content in Value.

but I'm not aware, that XSD is capable of XPath (well, why should it?)
May 11 '09 #6
Jaguex
7
Xsd is capable of xpath to validate key integrity (constraints)
It seems to me an essential part of validation of structure.

Therefor it would be strange if xsd is not capable to check on a External Id being present in the document .. I still didn't figure out how to "hack" this :)
May 11 '09 #7
Jaguex
7
It can not be done in xsd. It lacks full Xpath support. :(

http://www.xml.com/pub/a/2002/04/10/beyondwxs.html

The solution is to apply xslt to validate complex constraints.
I hope this is integrated into xsd very soon! So I don't need to transform/evaluate a document twice.

I opt for first Xslt and then Xsd validation.
Use the xslt for transforming to constrained xml and then validate this xml with the xsd in order to know weather or not the constraints are all valid.
Doing xslt first, will allow the removal of all redundant data before validation.

e.g.

Xslt
Expand|Select|Wrap|Line Numbers
  1.  <items>
  2.     <xsl:copy-of select="/Items/Item[@Id='2' and @Value != '']/>
  3.  <items>
  4.  
Xsd
Expand|Select|Wrap|Line Numbers
  1.  
  2.   <xs:element name="Items">
  3.     <xs:complexType>
  4.       <xs:sequence>
  5.         <xs:element name="Item">
  6.           <xs:complexType>
  7.             <xs:attribute name="Id" type="xs:positiveInteger" use="required" />
  8.           </xs:complexType>
  9.         </xs:element>
  10.       </xs:sequence>
  11.     </xs:complexType>
  12.   </xs:element>
  13.  
May 11 '09 #8
Dormilich
8,651 Expert Mod 8TB
if you do the XSLT by means of a script/programming language (C, Java, PHP to name some) you can simplify that by checking the result of the XSLT, because the XSLT already contains your constraints… or did I misunderstand something there?
May 11 '09 #9
Jaguex
7
You are quite right.. but if the xml is quite large it would be nice to immediately strip all redundancy in the first step. So my xslt would produce a constrained xml for further processing instead of an error list.
Eg. item id= 2 is only copied when exists and value != '' and all items with other id's are discarded.

I would continue to work with this non-redundant constrained xml. Remember it has to be transformed again. Also saves a lot of diskspace when it needs to be stored ;)

Tho xslt will produce a constrained xml it might be incosistent in format.. thus it has to be checked for proper format by means of xsd. Eg item id=2 still has to be present throughout document before proceding!

But this is just the way I think about addressing the problem.
Otherwise what is the point of having xsd at all? All can be done through xslt!
May 11 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Gordon Dickens | last post: by
6 posts views Thread by Michael Hill | last post: by
9 posts views Thread by Philipp | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.