By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

How to convert attributes to element

P: n/a
Hello all,

Can't figure it out. :( Assume that we have 2 XML document:

Document1 (source):

<elements>
<elementA attribute1="value1" attribute2="value2" ... />
<elementA attribute1="value1" attribute2="value2" ... />
...
</elements>

Document2 (element description):

<elements>
<element name="elementA">
<attributes>
<attribute name="attribute1" description="Attribute1 desc" />
<attribute name="attribute2" description="Attribute2 desc" />
</attributes>
</element>
</elements>

Result document must have the following structure:

<elements>
<element name="elementA">
<attributes>
<attribute name="attribute1" description="Attribute1 desc"
value="value1" />
<attribute name="attribute2" description="Attribute2 desc"
value="value2" />
</attributes>
</element>
<element name="elementA">
<attributes>
<attribute name="attribute1" description="Attribute1 desc"
value="value1" />
<attribute name="attribute2" description="Attribute2 desc"
value="value2" />
</attributes>
</element>
</elements>

Please somebody help me,
Tod

Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You want to use XSLT
(http://www.zvon.org/xxl/XSLTutorial/.../contents.html) which, if
you don't already know it, will take you a while to learn.

Then, to combine two XML documents into one, you have to choose one as
the primary document (probably your Document1), and use the other
document as a kind of reference lookup. You can do this using the XSLT
document() function, something like this:

<xsl:variable name="elementName" select="name()" />
<xsl:for-each select="@*">
<xsl:variable name="attributeName" select="name()" />
<xsl:variable name="attributeDescription"
select="document(Document2.xml)/elements/element[@name=$elementName]/attribute[@name=$attributeName]/@description"
/>
.... write output XML here...
</xsl:for-each>

A *HUGE* caveat: this is _not_ working XSLT code. I have not tested it.
It is just an idea of where to start. You cannot just plug it into an
XSLT document without understanding XSLT, and trust that the transform
will work. It won't. In particular, I've never tried XPath expressions
with double [] selection in them, so I don't even know if that works or
if I have the syntax right. The point is that there's a way, if you
understand XSLT, to monkey around with this and get it to work.

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.