Newbie. xml "dynamically" including another xml document

C

Clive

Hi,

I have an xml document with customer data. A test example starts
like this:

<?xml version="1.0" encoding="UTF-8"?>
<customer-info xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="cust-05.xsd">
<!-- Contain all properties of the customer in document element
-->
<customer cust-number="38273-815">
<cust-name title="Mr.">
<first>Clive</first>
<middle>Robert</middle>
<last>Long</last>

etc.

I have another xml document with a completely different structure:

<?xml version="1.0" encoding="UTF-8"?>
<occupation-list>
<occupation d="" tv="1" r="" s="" i="" p="" descr="ARCHITECTURE,
ENGINEERING, AND SURVEYING">arch-eng-surv</occupation>
<occupation d="" tv="2" r="" s="" i="" p="" descr="MATHEMATICS AND
PHYSICAL SCIENCES">math-phys</occupation>

etc.

Now I have an xslt that processes the data in the first xml document
but want to "refer" to the second xml document so that when the
occupations in the first doc are "looked up" in the second, I can
"read" the values of the attributes from the second document (d, i,
tv, etc ...) and process them. Follow me?

I want to keep the two documents and their structure separate so
they can more easily be updated.

I can't work out how to do this? Can anyone give me a hint?

Thanks

Clive
 
P

Peter Flynn

Clive said:
Hi,

I have an xml document with customer data. A test example starts
like this:

<?xml version="1.0" encoding="UTF-8"?>
<customer-info xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="cust-05.xsd">
<!-- Contain all properties of the customer in document element
-->
<customer cust-number="38273-815">
<cust-name title="Mr.">
<first>Clive</first>
<middle>Robert</middle>
<last>Long</last>

etc.

I have another xml document with a completely different structure:

<?xml version="1.0" encoding="UTF-8"?>
<occupation-list>
<occupation d="" tv="1" r="" s="" i="" p="" descr="ARCHITECTURE,
ENGINEERING, AND SURVEYING">arch-eng-surv</occupation>
<occupation d="" tv="2" r="" s="" i="" p="" descr="MATHEMATICS AND
PHYSICAL SCIENCES">math-phys</occupation>

etc.

Now I have an xslt that processes the data in the first xml document
but want to "refer" to the second xml document so that when the
occupations in the first doc are "looked up" in the second, I can
"read" the values of the attributes from the second document (d, i,
tv, etc ...) and process them. Follow me?

Yep. As with any cross-referencing between files, you need to identify
exactly which piece of information is the link value between entries. I
don't see one in your example, but perhaps there is an <occup> element
further down the first file which contains the name of the occupation that
can be looked up in the second file (eg math-phys or arch-eng-survey).

Assuming that you can identify such a linking value, all you need is the
document() function in XSLT, eg while processing a <customer> element,
something like

<xsl:value-of select="document('secondfile.xml')/occupation-list/
occupation[.=current()/occup]/@tv"/>

will return the value "2" if it's processing your entry.

However, doing this individually for everything you want to look up would be
very resource-intensive on a long run. There are several solutions, such as
(a) retrieve the matching element node (occupation) into a variable at an
earlier stage, from where the attributes can be retrieved when needed
without the need for separate lookups; or (b) use lookup keys to identify
the set of nodes you will need at the start of the run (the <xsl:key>
element of XSLT and the key() function).

///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,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top