473,378 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I insert conditional elements in target document?

dmc
Can anyone suggest an elegant solution for this problem? I have a
source document as follows:

<Object Type="Type1">
<Attribute_A>ValueA</Attribute_A>
<Attribute_B>ValueB</Attribute_B>
</Object>

<Object Type="Type2">
<Attribute_A>ValueA</Attribute_A>
<Attribute_B>ValueB</Attribute_B>
</Object>

The schema for my destination document requires the end result to be:

<Object_Type_1>
<Type1AttributeA>ValueA</Type1AttributeA>
<Type1AttributeB>ValueB<Type1AttributeB>
</Object_Type_1>

<Object_Type_2>
<Type2AttributeA>ValueA</Type2AttributeA>
<Type2AttributeB>ValueB<Type2AttributeB>
</Object_Type_2>

The problem is that the element names, and their structure and
children elements
are different depending on the Type of the source elements.

I could use a lookup for the <Object_Type...elements. This works
very well if only the name
of teh target element changes, but this gets very cumbersome
for managing the child elements and sub-trees. Any help greatly
appreciated.

Nov 20 '06 #1
3 1453
What language are you working in? The best solution depends on the tools
you have available.
Nov 21 '06 #2

dmc wrote:
Can anyone suggest an elegant solution for this problem?
I have a source document as follows:

<Object Type="Type1">
<Attribute_A>ValueA</Attribute_A>
<Attribute_B>ValueB</Attribute_B>
</Object>

<Object Type="Type2">
<Attribute_A>ValueA</Attribute_A>
<Attribute_B>ValueB</Attribute_B>
</Object>

The schema for my destination document requires the end
result to be:

<Object_Type_1>
<Type1AttributeA>ValueA</Type1AttributeA>
<Type1AttributeB>ValueB<Type1AttributeB>
</Object_Type_1>

<Object_Type_2>
<Type2AttributeA>ValueA</Type2AttributeA>
<Type2AttributeB>ValueB<Type2AttributeB>
</Object_Type_2>

The problem is that the element names, and their
structure and children elements are different depending
on the Type of the source elements.
You probably have a reason you're doing something like
this, but, frankly, judging from the example given, the
design looks quite awful.
I could use a lookup for the <Object_Type...elements.
This works very well if only the name of teh target
element changes, but this gets very cumbersome for
managing the child elements and sub-trees. Any help
greatly appreciated.
You failed to mention it, but I presume you're using some
sort of DOM or SAX API. Now, it's certainly possible to do
it this way, BUT. If you have an API to some sort of XSLT
processor, -- well, XSLT is quite well-suited to this kind
of processing.

This transformation works for your sample document:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- identity -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- exclusions -->
<xsl:template match="Object">
<xsl:variable name="elt-name">
<xsl:call-template name="make-obj-elt-name"/>
</xsl:variable>
<xsl:element name="{$elt-name}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template name="make-obj-elt-name">
<xsl:value-of select="local-name()"/>
<xsl:text>_Type_</xsl:text>
<xsl:value-of select="substring-after(@Type,'Type')"/>
</xsl:template>
<xsl:template match="@Type[parent::Object]"/>
<xsl:template
match="*[substring-before(local-name(),'_')]">
<xsl:variable name="elt-name">
<xsl:call-template name="make-attr-elt-name"/>
</xsl:variable>
<xsl:element name="{$elt-name}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template name="make-attr-elt-name">
<xsl:value-of select="../@Type"/>
<xsl:value-of
select="substring-before(local-name(),'_')"/>
<xsl:value-of
select="substring-after(local-name(),'_')"/>
</xsl:template>
</xsl:stylesheet>

Note that it's unaware of namespaces.

--
Pavel Lepin

Nov 21 '06 #3
dmc
Thanks for the replies.

Sorry - I forgot to mention the important things: I am trying to write
an XSLT stylesheet to do this.

The example was really trying to extract the essence of the problem but
I'm afraid I over simplified - the actual designs are somewhat more
complex and the element names are quite different. In any case your
solution is very clever and has given me an idea that I'd like to play
with, thanks.

I left out namespace stuff for clarity.

Nov 21 '06 #4

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

Similar topics

1
by: Orest Kinasevych | last post by:
Okay, I made sense of the earlier suggestions and realized I was on the right track -- I appreciate the feedback which got me to this point. The suggestions posted here indeed worked and...
4
by: TheKeith | last post by:
I just wrote the following script for something I'm working on: ---------------------------------------------------------------------------- ------------------- <html> <head> <script...
11
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be...
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Example: <a> <b createAnotherWhenCondition="x"> <c>text for c</c> <d>text for d</d> </b>
5
by: Ed Jay | last post by:
I have a switch statement that controls which of several containers is displayed or not. It currently looks like: function showHelp(n) { show('vhelp'); //makes parent container visible switch...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
1
by: kidelectric | last post by:
The issue I am having is that I would like to be able to drag-and-drop div elements that have rounded corners.* Since these elements will be dynamically created (including background color), I could...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.