Additive parsing? (for want of a better subject)

Chris Vendel

Usenet Archive
Joined
Nov 13, 2001
Messages
4
Reaction score
0
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
 

Jesse Hager

Usenet Archive
Joined
Jun 3, 2004
Messages
11
Reaction score
0
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>
 

Gregory Vaughan

Usenet Archive
Joined
May 31, 2000
Messages
52
Reaction score
0
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
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
% 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.
 

Chris Vendel

Usenet Archive
Joined
Nov 13, 2001
Messages
4
Reaction score
0
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>
 

Jesse Hager

Usenet Archive
Joined
Jun 3, 2004
Messages
11
Reaction score
0
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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,798
Members
249,594
Latest member
henrywalsh

Latest Threads

Top