Jump to content
Sign in to follow this  
talsnir

Complicated Attributes validation

Recommended Posts

Hello Gurus,I am rather new to XSD so I apologize if my question seems a little dumb.Anyway, I have this problem with validating attributes. I have a case where I need to assure that one attribute is shown if and only if another attribute is shown.For example:<Report searchable="true" searchSize="10"/> Is OK and:<Report searchable="true"/> is also OKbut, in a case like this: <Report searchSize="10"/>I expect the schema to alert me.Can anyone please let me know how it is done?Thanks a lot,T.

Share this post


Link to post
Share on other sites

I say use XSLT:

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="reports">	<xsl:for-each select="Report">			   <xsl:value-of select="@searchSize" /><br />	</xsl:for-each></xsl:template></xsl:stylesheet>

...this is if you xml is simliar to:

<?xml version="1.0" encoding="iso-8859-1"?><reports>	<Report searchSize="10"/> 	<Report searchSizse="20"/>	<Report anotherAttribute="Whatever"/>  </reports>

You can also use a choose with when tests/otherwise

Share this post


Link to post
Share on other sites

WOW... Thanks a lot kvnmck18.I've been waiting for an answer for so long.First, I wasn't even thinking about XSLT because I didn't understand I can actually use it in order to validate XML documents so thank you for that.Second, I need further more clarications... Where is the "searchable" attribute? I need to promise that the searchSize only appears if the searchable attribute exists and its value is "true". I didn't see any refernces in your reply for the "searchable" attribute.Third. the Report element has a lot more attibutes beside the above two - Is it a problem?Thanks a lot...T.

Share this post


Link to post
Share on other sites

Actually I don't know what I was saying. You don't need that true()...when I responded I was in the middle of something else....I corrected the XSLT code. The for-each creates a "loop" for the nodes called "Report" and the value-of ONLY selects attributes with the name of "searchSize".Also to respond, Your XML can have as many nodes as you want...I was just using an example.If you want the true attribute to be away of determing the XSLT then do this:(the xml):

<?xml version="1.0" encoding="iso-8859-1"?><reports>	<Report searchable="true" searchSize="10"/> 	<Report searchable="true" searchSize="20"/>	<Report searchable="false" searchSize="25"/>  </reports>

So in the XML above there are two "true" values for the searchable attribute. This should result with a 10 20. The 25 should not be shown. If all were false, none would be shown. If you made searchSize="25" "searchable" to true...then all three of those would be shown. Here's the XSL:

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="reports">	<xsl:for-each select="Report">	<xsl:if test="@searchable = 'true'">		<xsl:value-of select="@searchSize" /><br />	</xsl:if>	</xsl:for-each></xsl:template></xsl:stylesheet>

What exactly are you doing? What determines if one of those numbers (searchSize) are not true?

Share this post


Link to post
Share on other sites

Actually, you can't use XSLT to validate an XML. I think what kvnmck18's stylesheet does is to filter the invalid ones, which though useful is not exactly what you want.If searchable is always present, you could simply make it a requred attribute. The document won't be valid if it's absent. Like so:

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">	<xsd:attributeGroup name="search">		<xsd:attribute name="searchable" type="xsd:boolean" use="required"/>		<xsd:attribute name="searchSize" type="xsd:integer"/>	</xsd:attributeGroup>	<xsd:element name="Report" nillable="true">		<xsd:complexType>			<xsd:attributeGroup ref="search"/>		</xsd:complexType>	</xsd:element></xsd:schema>

Notice the use="requred" attribute.

Share this post


Link to post
Share on other sites

Hey you guys,Thank you both so much, I appriciate your help.But I am afraid I was still left with no answers... since the searchable attribute can't be mandatory.I am starting to feel that this is far more complicated problem than I was intially thought, I don't have the time to figure it right now but thanks a lot anyway!!!T.

Share this post


Link to post
Share on other sites

Well, in that case, I'm afraid Schema 1.0 can't do what you're asking for (changing what's valid of one node depending on the existence/value of another node). Schema 1.1 is supposed to do this, but it's still not a reccomendation, and as such is not supported in any validator yet.

Share this post


Link to post
Share on other sites

Hi boen_robot,I guess you're right. But I was introduced to a method that allows me to do what I wanted with elements instead of attibutes.Using the <xsd:choice> I can promise that only the legal combinations are allowed.But unfortunately, I can't use the <choice> with attributes, so I am left with nothing again...Thanks any how...T.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...