Additive parsing? (for want of a better subject)

C

Chris Vendel

I'm in doubt as to how best to handle the following 'challenge'...

I want to render an XML file from two XML sources. One is a template
and the other (if available) contains data I wish to add. The
resulting (XML) should contain all the structure from the 'Template
XML' and the matching data from the 'Data XML'.

An example:

Template XML -------------------------

<root>
<person>
<firstname></firstname>
<lastname></lastname>
<address></address>
</person>
<details>
<age></age>
<length></length>
</details>
</root>

Data XML ------------------------------

<root>
<lastname>Hastings</lastname>
<age>28</age>
</root>


Should parse to (Resulting XML)--------

<root>
<person>
<firstname></firstname>
<lastname>Hastings</lastname>
<address></address>
</person>
<details>
<age>28</age>
<length></length>
</details>
</root>


Any advise would be appreciated!

Cheers!

Chris
 
J

Jesse Hager

Hi Chris,

For your template XML, consider using the official XML template format,
XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<root>
<person>
<firstname><xsl:value-of select="firstname"/></firstname>
<lastname><xsl:value-of select="lastname"/></lastname>
<address><xsl:value-of select="address"/></address>
</person>
<details>
<age><xsl:value-of select="age"/></age>
<length><xsl:value-of select="length"/></length>
</details>
</root>
</xsl:template>
</xsl:stylesheet>
 
G

Gregory Vaughan

You might have a look at ODBC2XML. This is a shareware program (I'm the
developer) that does a very similar thing to what you describe -- but the
data is stored in an ODBC database instead of in XML. You can embed SQL
queries into an XML template, and the result contains the data merged with
the template XML.

http://www.intsysr.com/odbc2xml.htm
 
P

Patrick TJ McPhee

% I want to render an XML file from two XML sources. One is a template
% and the other (if available) contains data I wish to add.

Someone's already suggested using xslt as your `template language'.
I thought I'd point out that, if you're still in the process of defining
the templates, the XSLT `simplified' syntax might be well suited to
this problem. The template would look like this:

<root xmlns:xsl = 'http://www.w3.org/1999/XSL/Transform'
xsl:version='1.0'>
<person>
<firstname><xsl:value-of select='/root/firstname'/></firstname>
<lastname><xsl:value-of select='/root/lastname'/></lastname>
<address><xsl:value-of select='/root/address'/></address>
</person>
<details>
<age><xsl:value-of select='/root/age'/></age>
<length><xsl:value-of select='/root/length'/></length>
</details>
</root>

Having said that, you could combine the two files you present using
the document() function in an ordinary verbose xslt stylesheet, but
it might be a bit tricky if you don't know which elements you'll
want to overwrite.
 
C

Chris Vendel

Thanks for all the input, however I'm fairly stuck to using the two
previously described XML documents.
This leaves me with more or less two options.

1/ Solve it in script somehow
or
2/ Pour both XML files (template & data) into one XML input file and
use XSLT to match the data block to the template block

Eg/ I imagine this would look something like:

---------------------------------

<root>
<tempxml>
<person>
<firstname></firstname>
<lastname></lastname>
<address></address>
</person>
<details>
<age></age>
<length></length>
</details>
</tempxml>
<dataxml>
<lastname>Hastings</lastname>
<age>28</age>
</dataxml>
</root>
 
J

Jesse Hager

Hi Chris,

How about this XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="data_xml"
select="document(/root/dataxml)/root/*"/>
<xsl:variable name="template_xml"
select="document(/root/templatexml)"/>
<xsl:template match="/">
<xsl:apply-templates select="$template_xml/*"/>
</xsl:template>
<xsl:template match="*">
<xsl:variable name="element_to_get" select="name()"/>
<xsl:choose>
<xsl:when test="count(./*) = 0">
<xsl:copy-of select="$data_xml[name()=$element_to_get]"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

It takes an XML file structured like this:

<?xml version="1.0" ?>
<root>
<dataxml>data.xml</dataxml>
<templatexml>template.xml</templatexml>
</root>

Where data.xml is the name of your data file and template.xml is the
name of your template file. Format of the two files is the same as your
original posting. These filenames may also be a full URL if your XSLT
processor supports it. (eg. http://myserver/template.xml)

It locates any empty entities in the template.xml and fills them in with
matching data from the data.xml. Also copies full elements from the
data file so you can have attributes and child entities in the data file.

I hope this helps.
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top