Question about tag and value

M

mittra

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.
 
J

Joseph Kesselman

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.
 
J

Joseph Kesselman

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.
 
M

mittra

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?
 
J

Joe Kesselman

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.
 
P

Philippe Poulard

hi,

(e-mail address removed) 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 :)

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top