Question about tag and value

mittra

Usenet Archive
Joined
Nov 5, 2007
Messages
4
Reaction score
0
Hello All,

I am a newbie to XML. I have the following question: how do I express something in XML when the subsequent tag structures depends on the value of a preceeding tag?

For example, when TAG2 has a value of 1, then TAG3 exists.

<TAG1>
<TAG2>1</TAG2>
<TAG3>some_value</TAG3>
</TAG1>

But when TAG2 has a value of 2, then TAG4 exists.

<TAG1>
<TAG2>2</TAG2>
<TAG4>some_value2</TAG4>
</TAG1>

Can the above two be combined into a single structure for TAG1?

Thanks for your insight.
 

Joseph Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
733
Reaction score
0
I am a newbie to XML. I have the following question: how do I express something in XML when the subsequent tag structures depends on the value of a preceeding tag?

Neither DTDs nor XML Schema allow you to use the value of an element or attribute to constrain the presence or another -- with the exception of the xsi:type attribute; see a good XML Schema tutorial (or a great deal of recent discussion in this very newsgroup) for details of how to use that sub-typing feature.

You *can* structure things so the presence or absence of your <TAG2> element gates whether TAG3 or TAG4 was expected after it, by writing the appropriate content models in your DTD or Schema.

Or you can enforce the cross-constraint in your application rather than expecting the document formalism to do it all. Remember, neither DTDs nor schemas are intended to capture all the semantics of a document, and some checking will always have to be done by the application -- vaidation only provides an initial layer and some "machine-readable documentation" of what's expected.
 

Joseph Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
733
Reaction score
0
Joseph said:
vaidation only provides an initial layer and some "machine-readable documentation" of what's expected.

Putting it another way -- Validity, if you're enforcing it, documents things that are Necessary for a correct document, but may not enumerate everything that is Sufficient for it to be correct.
 

mittra

Usenet Archive
Joined
Nov 5, 2007
Messages
4
Reaction score
0
Neither DTDs nor XML Schema allow you to use the value of an element or attribute to constrain the presence or another -- with the exception of the xsi:type attribute; see a good XML Schema tutorial (or a great deal of recent discussion in this very newsgroup) for details of how to use that sub-typing feature.

You *can* structure things so the presence or absence of your <TAG2> element gates whether TAG3 or TAG4 was expected after it, by writing the appropriate content models in your DTD or Schema.

Or you can enforce the cross-constraint in your application rather than expecting the document formalism to do it all. Remember, neither DTDs nor schemas are intended to capture all the semantics of a document, and some checking will always have to be done by the application -- vaidation only provides an initial layer and some "machine-readable documentation" of what's expected.

Thanks. I will look into XML Schema tutorial.

However, from your respnse it seems I might not have communicated the objective behind doing this. While I can enforce the check of the subsequent tag structure (TAG3 or TAG4) in the application domain, the primary objective is to define *one* XML structure that (based on TAG2) defines the *entire* structure. This will be akin to saying, in this example,

if (TAG2 value is 1) then
<TAG3>some_value</TAG3>
else
<TAG4>some_value2</TAG4>

Perhaps, XML Schema does it?
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
if (TAG2 value is 1) then
<TAG3>some_value</TAG3>
else
<TAG4>some_value2</TAG4>

Neither XML Schema nor DTDs can express that constraint, except by using xsi:type. Some of the less-widely-supported schema languages can (I think both RelaxNG and Schematron can do this) -- but then you have the problem of whether your users will accept those solutions.

Again: The simplest answer is to fix it in documentation and in application code.
 

Philippe Poulard

Usenet Archive
Joined
Dec 20, 2001
Messages
412
Reaction score
0
hi,

a écrit :
if (TAG2 value is 1) then
<TAG3>some_value</TAG3>
else
<TAG4>some_value2</TAG4>

Perhaps, XML Schema does it?

this is exactly the way the Active Schema Language works : you can define dynamically the content model that suits your needs, potentially with the help of if-then-else statements

although ASL is experimental, it is implemented and works fine : you can use it for your own needs if you don't have to share your own schemas with third-party users ; ASL will allow you to express much more complex constraints than legacy schema technologies can (DTD, W3C XML Schema, RelaxNG, Schematron)

the implementation :
http://reflex.gforge.inria.fr/
the specification :
http://ns.inria.fr/active-tags/active-schema/active-schema.html some tutorials :
http://reflex.gforge.inria.fr/tutorial-schemas.html

if you are new to XML, you'll have to understand namespaces in XML and learn XPath before (which are widely used in XML technologies)

some courses :
http://disc.inria.fr/perso/philippe.poulard/cours/01c-xml-namespaces.pdf http://disc.inria.fr/perso/philippe.poulard/cours/04-xml-xpath.pdf ....if you are new to french, you'll have to learn french before :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top