473,770 Members | 4,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"value substitution/population" in template XML using XPath


Hello,
I'm stuck on this problem for quite some time and hope somebody would
be able to guide me.

Basically, I need to populate a large number of "template" XML files
which have all elements/attributes etc. defined but the values in these
elements/attributes might be blank or place holders, as two examples
below:

Example - File type 1
<?xml version="1.0" encoding="UTF-8"?>
<fileType1>
<element1>
<element2 attr2="xxx">
<element3>
<element4 attr4="yyy">
</element4>
</element3>
</element2>
</element1>
</fileType1>
or

Example - File type 2
<?xml version="1.0" encoding="UTF-8"?>
<fileType2>
<element1>
<element2 attr2="xxx">
<element13>
<element14>
<element15>ZZ Z</element15>
</element14>
</element13>
</element2>
<element3>
</element3>
</element1>
</fileType2>
and the new values to be populated (or atleast their XPath) are
indicated in a seperate XML data file as, and easily obtained as
xpath/value pair using Java or XSLT etc.:

<?xml version="1.0" encoding="UTF-8"?>
<newValues>
<file type="fileType2 ">
<element>
<x-path>/fileType2/element1/element3</x-path>
<value>newValue 3</value>
</element>
<element>
<x-path>/fileType2/element1/element2/@attr2</x-path>
<value>ABC</value>
</element>
<element>
<x-path>/fileType2/element1/element2/element13/element14/element15</x-path>
<value>newValue 15</value>
</element>
</file>
<file type="fileType1 ">
<element>
<x-path>/fileType1/element1/element2/@attr2</x-path>
<value>123</value>
</element>
<element>
<x-path>/fileType1/element1/element2/element3/element4/@attr4</x-path>
<value>999</value>
</element>
</file>
</newValues>

So what is the best approach to read the 'template' example file(s) and
output it with new values for xpath indicated in the data file?

I have used DOM/JDOM to perform this task at a limited scale, but the
complicating factors are:

- the number of unique "template" files is rather large (all
generated from relevent XSD)
- the structure of each template is quite deep/complex
- the number of xpath/value would be arbitrary (say min of 5 "fields"
to be changed, max all of them!)

Essentially this task is a 'value substitution' or 'data population'
problem for an XML file, where the xpath/value pair are indicated in
another XML file. IMHO (re)constructin g the output XML from scratch
appears would be overkill, and not feasbile to do it programatically in
Java code etc. Hence my search for a more efficient way to do it, say
with XSLT or some other technique, which 'processes' the original XML
input file WITHOUT altering the structure but substituting the desired
values as required.

Would appreciate any and all help in this regards!

Regards,

Bilal B.
--
Bilal
------------------------------------------------------------------------
Bilal's Profile: http://techiegroups.com/member.php?userid=336
View this thread: http://www.techiegroups.com/showthread.php?t=116624

Sep 15 '06 #1
2 2577
Y'know, I'd consider (ab)using XSLT for this.

Start with the standard identity transform, to handle most of the document.

Then as the equivalent of your
<element>
<x-path>/fileType2/element1/element3</x-path>
<value>newValue 3</value>
</element>
write a template something like:

<xsl:template match="/fileType2/element1/element3">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:choose>
<xsl:when test="node()" <!-- If not empty -->
<xsl:apply-templates select="node()"/>
<xsl:when>
<xsl:otherwise> newValue3</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>

Obviously this could handle more complicated conditions/operations as well.

Note that if you don't want to write this code by hand it would be
straightforward to write a stylesheet which generates this stylesheet
from the <newValuessketc h you showed us.

XPath-match-and-transform is exactly what XSLT is designed for. This
isn't a typical application for it, but it's can do the job, and
probably beats hand-coding a new application for the purpose.

Having said all that: You said you're using schemas. I believe schemas
can specify default values. Is there a reason you aren't using that
mechanism rather than inventing a new one?

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Sep 16 '06 #2
Hi Joe,
Thanks for your reply; I'll explore your suggestions further to fully
understand them as my XSLT experience is, to be honest, between advanced
beginner & intermediate :)
>Note that if you don't want to write this code by hand it
would be straightforward to write a stylesheet which
generates this stylesheet from the <newValuessketc h you
showed us.
As the <newValuessketc h/file isn't static, I think that would be the
only viable way to do. Hmm... need bit more reading up on that!
>Having said all that: You said you're using schemas. I
believe schemas can specify default values. Is there a
reason you aren't using that mechanism rather than
inventing a new one?
Yes, I do have the schema so if the task can be done by using the
schema, I would try that approach as well. Indeed default values for
elements/attributes can be specified in the schema as below:

<xs:element name="color" type="xs:string " default="red"/>

The values for elements/attributes as exampled in the <newValues>
sketch will differ from case to case, so unsure how that would be
useful? Wouldn't that necessitate changing the schema file to generate a
new instance xml? Also, how would one use the xpath to navigate the
schama inself? Pardon my ignorance if that is a something obvious but my
XSLT experience has been limited to plain XML files rather than XSD
files, though I recognize that the principle remains the same for the
two. So could you elaborate please?

Thanks again!

Regards,

Bilal B.

*** Sent via Developersdex http://www.developersdex.com ***
Sep 18 '06 #3

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

Similar topics

9
6510
by: Russ Perry Jr | last post by:
I'm using "ID" and "Value" in the generic sense here... Let's say one page I had a <html:select> with a collection like this: <html:options collection="items" property="key" labelProperty="value"/> In this case "key" is what I mean by "ID", and "value" is what I mean by "Value" -- the "Value" is shown to the user, but the "ID" is what I plan to keep, to be saved to a database (as a reference in a table to a common table as a part of...
7
3678
by: ma740988 | last post by:
The string object value_f doesn't produce the right output. At issue, - I suspect - is the conversion from string to int with istringstream. An alternate approach? Thanks in advance #include <iostream> #include <sstream> #include <string> using namespace std;
7
2943
by: Don | last post by:
Via JavaScript within the client page containing the checkbox, I'm trying to capture the value of a checkbox to make a cookie out of it. But, all I get is the defined "ON" value, even when it is unchecked. I'm using the following construct: document.cookie = "cpceRememberLoginCookie="+document.getPage1.rememberLogin.value+"; expires=Thr, 01-Jan-2015 00:00:00 GMT"; The actual checkbox value passed in the <form> POST reflects the actual...
4
37466
by: charliewest | last post by:
I need to set the selected drop down list value at run time. I am aware of the method "SelectIndex" however this works only if you know the precise location of the value within the ListItem collection. Otherwise, what is the recommended approach? I have managed to set the appropriate value using the following loop: for (int i = 1; i < ddlUsers.Items.Count; i++) { if (ddlUsers.Items.ToString() == sUserId)
2
6851
by: andrew007 | last post by:
I do xml / xslt transformation using asp.net but I found any value (w/xml format) in xml node html-encoded to &lt and &gt format if it's > or < tag. Since I have sub xml data in a parent xml node as a value. Check out the following problem. I want to convert the value in <WpDatesXml> node to have a valid "<" and ">" instead of &lt or &gt format so that I can use this xml for another use. Please help! <NewDataSet> <Table1>
12
13916
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL does not support it. Could someone helps me please? The example table: T1 (col1 varchar(7) not null,
2
13701
by: riceyeh | last post by:
Hi, What does <xsl:if test="not($values)"mean? What I do not understand is $values? Here, means array? And . = $value means current node is equal to the variable value? So the total meaning is "if not exists of value in array of values" ? But the result of '. = $value' is a boolean value, so '$values' should not mean "the value in values". Rice
4
1506
by: vinnie | last post by:
When i try to populate a dropdownlist menu, using a for cicle works perfect: i get a list of int from ie 1 to 10. How an populate my ddlist if working on the code i wish to get not longer a series of integers, but a series of decimals? Like this: old fashion: (for i=1; i<=100; i++) i get 100 integers; New fashion) i want to populate the same list, but using the decimals: 1; 1.01; 1.02; 1.03.... 99.99, 100.00
4
1612
by: Aries Sun | last post by:
The following code can be compiled without error: typedef char True; // sizeof(True) == 1 typedef struct { char a; } False; // sizeof(False) 1 //... template <typename TTrue isPtr( T * ); template <typename TFalse isPtr(T); #define is_ptr( e ) (sizeof(isPtr(e))==sizeof(True))
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7431
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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 we have to send another system
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.