how to refer to data stored in xml from xml itself?

H

haigang0708

hello,everyone!
Maybe the title is a little confusing, but that's my problem.

Now my xml file has two parts. The first part stores data which might
be changed. The second part stores some "actions" which use the data
in first part as its parameters. I need to ensure that when I execute
"actions" of second part, it can always get the newest parameters. It
sounds like "pointer", right?

What I do now is storing the XPath expression of wanted data of first
part in second part where need it. So I can find right value of
parameters according to these paths. But in this way, many XPath
expressions are stored in xml file. I think this is a bad style.

Can anyone give me some advice about this?

Thanks.
 
A

Armel

hello,everyone!
Maybe the title is a little confusing, but that's my problem.

Now my xml file has two parts. The first part stores data which might
be changed. The second part stores some "actions" which use the data
in first part as its parameters. I need to ensure that when I execute
"actions" of second part, it can always get the newest parameters. It
sounds like "pointer", right?

What I do now is storing the XPath expression of wanted data of first
part in second part where need it. So I can find right value of
parameters according to these paths. But in this way, many XPath
expressions are stored in xml file. I think this is a bad style.

Can anyone give me some advice about this?
using id="" where a parameter is defined and ref="" where it is used?

Armel
 
D

Daniel Lee

using id="" where a parameter is defined and ref="" where it is used?

Armel

In my xml, a simple id is not enough to locate the parameter. For
example:
<Parameter name="config">
<Module id="1">
<Part id="1">
<port>53</port>
<protocol>UDP</protocol>
......
</Part>
<Part id="2">
<ip>192.168.1.1</ip>
<port>21</port>
........
</Part>
</Module>
<Module id="2">
....something like module 1....
</Module>
</Parameter>
<Parameter name="action">
<Module id="1">
<Part id="1">
<step count="1">
<operation>send_packet</operation>
<port>ref={//Parameter[@name="config"]/
Module[@id="1"]/Part[@id="1"]/port/text()}</port>
<protocol>ref={//Parameter
[@name="config"]/Module[@id="1"]/Part[@id="1"]/prototol/text()}</
protocol>
</step>
<step count="2">
......
</step>
.......
<Part id="2">
something like above
</Part>
</Module>
<Module id="2">
....something like module 1....
</Module>
</Parameter>

Is there any techniques have the same effect as XPath expression in my
xml file?
Or is there any better methods?
 
A

Armel

"Daniel Lee" <[email protected]> a ¨¦crit dans le message de
(e-mail address removed)...
<[email protected]> a ¨¦crit dans le message de (e-mail address removed)...>
hello,everyone!




using id="" where a parameter is defined and ref="" where it is used?

Armel

In my xml, a simple id is not enough to locate the parameter. For
example:
<Parameter name="config">
<Module id="1">
<Part id="1">
[...]

in my mind a "xml id" was expected to be unique in a document (as an
attribute with the ID validity constraint of xml). You could generate such
an ID by simply incrementing a counter when you generate your file (and get
ids such as: "id1", "id2" ...), and use an appropriate attribute to store it
(e.g. document-wide-id='...')

Armel
 
D

Daniel Lee

"Daniel Lee" <[email protected]> a ¨¦crit dans le message de (e-mail address removed)...
<[email protected]> a ¨¦crit dans le message de (e-mail address removed)...>
hello,everyone!
using id="" where a parameter is defined and ref="" where it is used?

In my xml, a simple id is not enough to locate the parameter. For
example:
<Parameter name="config">
        <Module id="1">
                <Part id="1">
[...]

in my mind a "xml id" was expected to be unique in a document (as an
attribute with the ID validity constraint of xml). You could generate such
an ID by simply incrementing a counter when you generate your file (and get
ids such as: "id1", "id2" ...), and use an appropriate attribute to store it
(e.g. document-wide-id='...')

Armel- éšè—被引用文字 -

- 显示引用的文字 -

Thank you for your advice.

But in this way, if I need to add parameters later, I have to update
all the references. It leads to mistakes easily.
 
P

Philippe Poulard

Hi,

Daniel Lee a ¨¦crit :
Not so much. This kind of document is like a template populated by some
data defined in itself.

In RefleX, they are called Active Documents, and I've just run your
example from the command line (with few syntax fixes: an end tag was
missing and "protocol" was mispelled in the XPath expression), it works
fine and I get the expected result (that is to say, the part in curly
braces replaced by the right value)

Source (snippet):
<port>ref={$this//Parameter[@name="config"]/Module[@id="1"]/Part[@id="1"]/port/text()}</port>
<protocol>ref={$this//Parameter[@name="config"]/Module[@id="1"]/Part[@id="1"]/protocol/text()}</protocol>
(just notice "$this" that refers to the active document itself)

Output (snippet):
<port>ref=53</port>
<protocol>ref=UDP</protocol>
(I suspect that in fact you don't want "ref=" to appear in the output,
just remove it in the source if necessary)

If you need an engine that can produce what you need, download RefleX
and try it !
http://reflex.gforge.inria.fr/

Here is the command line that was used to run your example:
java -jar reflex-0.3.2.jar run source.xml

Have fun !!!
using id="" where a parameter is defined and ref="" where it is used?

Armel

In my xml, a simple id is not enough to locate the parameter. For
example:
<Parameter name="config">
<Module id="1">
<Part id="1">
<port>53</port>
<protocol>UDP</protocol>
......
</Part>
<Part id="2">
<ip>192.168.1.1</ip>
<port>21</port>
........
</Part>
</Module>
<Module id="2">
....something like module 1....
</Module>
</Parameter>
<Parameter name="action">
<Module id="1">
<Part id="1">
<step count="1">
<operation>send_packet</operation>
<port>ref={//Parameter[@name="config"]/
Module[@id="1"]/Part[@id="1"]/port/text()}</port>
<protocol>ref={//Parameter
[@name="config"]/Module[@id="1"]/Part[@id="1"]/prototol/text()}</
protocol>
</step>
<step count="2">
......
</step>
.......
<Part id="2">
something like above
</Part>
</Module>
<Module id="2">
....something like module 1....
</Module>
</Parameter>

Is there any techniques have the same effect as XPath expression in my
xml file?
Or is there any better methods?


--
Cordialement,

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

Peter Flynn

Daniel said:
"Daniel Lee" <[email protected]> a ¨¦crit dans le message de (e-mail address removed)...
<[email protected]> a ¨¦crit dans le message de (e-mail address removed)...>
hello,everyone!
Maybe the title is a little confusing, but that's my problem.
Now my xml file has two parts. The first part stores data which might
be changed. The second part stores some "actions" which use the data
in first part as its parameters. I need to ensure that when I execute
"actions" of second part, it can always get the newest parameters. It
sounds like "pointer", right?
What I do now is storing the XPath expression of wanted data of first
part in second part where need it. So I can find right value of
parameters according to these paths. But in this way, many XPath
expressions are stored in xml file. I think this is a bad style.
Can anyone give me some advice about this?
using id="" where a parameter is defined and ref="" where it is used?
Armel
In my xml, a simple id is not enough to locate the parameter. For
example:
<Parameter name="config">
<Module id="1">
<Part id="1">
[...]

in my mind a "xml id" was expected to be unique in a document (as an
attribute with the ID validity constraint of xml). You could generate such
an ID by simply incrementing a counter when you generate your file (and get
ids such as: "id1", "id2" ...), and use an appropriate attribute to store it
(e.g. document-wide-id='...')

Armel- éšè—被引用文字 -

- 显示引用的文字 -

Thank you for your advice.

Consider using an IDREFS attribute on port and protocol:

<Parameter name="config">
<Module id="cm1">
<Part id="cp1">
<port>53</port>
<protocol>UDP</protocol>
......
</Part>
<Part id="cp2">
<ip>192.168.1.1</ip>
<port>21</port>
........
</Part>
</Module>
<Module id="cm2">
....something like module 1....
</Module>
</Parameter>
<Parameter name="action">
<Module id="am1">
<Part id="ap1">
<step count="1">
<operation>send_packet</operation>
<port ref="cm1 cp1"/>
<protocol ref="cm1 cp1"/>
</step>
<step count="2">
......
</step>
.......
<Part id="ap2">
something like above
</Part>
</Module>
<Module id="am2">
....something like module 1....
</Module>
</Parameter>

It's not clear why you want to store "ref=" as the content of the port
and protocol elements in the actions: your processor should be able to
generate this, eg in XSLT 1.0

<xsl:template match="port[ancestor::parameter[@name='action']]">
<xsl:text>ref=</xsl:text>
<xsl:value-of select="//Module
[@id=substring-before(current()/@ref,' ')]
/Part
[@id=substring-after(current()/@ref,' ')]/port"/>
</xsl:template>

You don't need the /text(): the default for value-of is to return the
string value of the content.
> But in this way, if I need to add parameters later, I have to update
> all the references. It leads to mistakes easily.

You're always going to have to update stuff when things change, no
matter what way you do it. Here at least it is kept to a minumum, and
the ID/IDREF mechanism will ensure you can't point to a non-existent
element.

///Peter
 

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
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top