473,469 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Additive parsing? (for want of a better subject)

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
Jul 20 '05 #1
5 1770
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>
--
Jesse Hager
Jul 20 '05 #2
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
"Chris Vendel" <c.********@chello.nl> wrote in message
news:3a**************************@posting.google.c om...
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

Jul 20 '05 #3
In article <3a**************************@posting.google.com >,
Chris Vendel <c.********@chello.nl> wrote:

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

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #4
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>

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

Now all I need is an XSLT that loops through all the nodes in block
'templatexml' and copies them to the output XML with the 'dataxml'
elements (values) inserted where their node names match.
Jul 20 '05 #5
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:otherwise>
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:otherwise>
</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.

--
Jesse Hager
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Vendel | last post by:
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....
7
by: Steve | last post by:
Is there a way to make style classes that are additive without the use of additional elements? ..Header { font-weight:bold; } ..SubEntry { padding-left:10px; )
1
by: yonido | last post by:
hello, my goal is to get patterns out of email files - say "message forwarding" patterns (message forwarded from: xx to: yy subject: zz) now lets say there are tons of these patterns (by gmail,...
3
by: GazK | last post by:
I have been using an xml parsing script to parse a number of rss feeds and return relevant results to a database. The script has worked well for a couple of years, despite having very crude...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.