473,699 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Xsd - xml must contain element with specific attribute value

7 New Member
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>
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 8018
8,658 Recognized Expert Moderator Expert
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
7 New Member
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
8,658 Recognized Expert Moderator Expert
If you're doing that, there's no need to validate the result, just let return the the content and check the result.

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
7 New Member
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
8,658 Recognized Expert Moderator Expert
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
7 New Member
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
7 New Member
It can not be done in xsd. It lacks full Xpath support. :(


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.


Expand|Select|Wrap|Line Numbers
  1.  <items>
  2.     <xsl:copy-of select="/Items/Item[@Id='2' and @Value != '']/>
  3.  <items>
Expand|Select|Wrap|Line Numbers
  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>
May 11 '09 #8
8,658 Recognized Expert Moderator Expert
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
7 New Member
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

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

Similar topics

by: Ingrid | last post by:
Am I right in thinking that datatyping at element level ie <xs:element name="num" type="xs:integer"> and specifying a choice of attribute values ie <xs:attribute name="kind"> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value="volume_number"/> <xs:enumeration value="page_range"/>
by: Gordon Dickens | last post by:
I have target xml to generate from schema. All of the XML instances have the same global element i.e. <base>. I would like to combine all of the schemas into a single schema where I could generate any of the specific instances. sample schema one: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="base">
by: Michael Hill | last post by:
I have this code that adds a table row and within the cells I create some input elements. When I go back using javascript the functionaliuty is seeing the form element. Anyone tell why I can see them? <form name='update'> myTR=document.createElement("TR"); myTR.setAttribute("id","RESULTS4"); // create cell 1 and content myTD=document.createElement("TD");
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a schema for it with Visual Studio, I get the error "Failed to create a schema for this data file because:
by: GU | last post by:
How can one get a list of enumeration values for a given element in an XML schema? I have been looking at the Schema Object Model, but I'm making very little headway. Can anyone give me a nudge in the right direction? Specifically, I am using Actions Pane to assist in creating a WordML document. For elements with enumerated values, I want to provide the user with the list of valid values from the schema. For example, if I have an...
by: Philipp | last post by:
Hello. OK I know this is the most asked question in XML (it says in some tutorial), but still. Please give me your insight on this (as I'm a newbie). I want to store parameters for a programm in an XML file. I can see 3 intelligent ways to this. 1) <?xml version="1.0" ?> <PARAMETERS>
by: BillAtWork | last post by:
Hi, I'm trying to validate an XML document against an XSD schema and I receive the following error: ---------- MyCode.CreateValidRequest : System.Web.Services.Protocols.SoapException : Validation error: The element 'http://xmlns.somewhere.com/something:rDetail' cannot contain text. Expected 'http://xmlns.somewhere.com/something:AList'. An error occurred at , (1, 533). ----------
by: BillAtWork | last post by:
Hi, I'm trying to validate an XML document against an XSD schema and I receive the following error: ---------- MyCode.CreateValidRequest : System.Web.Services.Protocols.SoapException : Validation error: The element 'http://xmlns.somewhere.com/something:rDetail' cannot contain text. Expected 'http://xmlns.somewhere.com/something:AList'. An error occurred at , (1, 533). ----------
by: mlb5000 | last post by:
I seem to be having issues validating an XML document using my schema. Both are below: The Schema: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Receivers" > <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element ref="MulticastReceiver"/>
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.