xtml: combining attributes from one element into a different element

M

Mike N.

totally new to xml and xtml, but I have been able to do some basic
transforms such as move through he source xml doc and copy selected
elements and attributes. I am hung up though on the following:

I have a poorly formed xml document with an element called misc, this
element has a couple hundred attributes. I am trying to iterate
through this list of attributes and copy certain of these which match
a text pattern (I need a wildcard to finish the pattern, i.e.
"motorcycle_*") to another element which already has it's own nodes.

Source.xml
<sample>
....
<vehicles>
<motorcycle speed="140" wheels="2" handling="45.0" />
<car speed="100" wheels="4" handling="45.0" />
<truck speed="80" wheels="12" handling="45.0" />
<vehicles>
....
<misc truck_engine = "diesel" car_acceleration = "20"
motorcycle_acceleration = "20" motorcycle_engine = "electric"
car_engine = "gasoline" truck_acceleration = "10"/>
....
<sample>

what I want to do is end up with:

<motorcycle speed="140" wheels="2" handling="45.0"
motorcycle_acceleration = "20" motorcycle_engine = "electric" />

The source doc above is a very much simplified form, as said, misc has
hundreds of attributes, but I can match to them if I can do a string
match like "motorcycle_*"

I can get the motorcycle element and it's attributes using copy-of,
but that seems to keep me locked from being able to then add
additional attributes to it.

Thanks in advance to any who can help.
 
P

Pavel Lepin

Mike N. said:
I have a poorly formed xml document with an element called
misc, this
element has a couple hundred attributes. I am trying to
iterate through this list of attributes and copy certain
of these which match a text pattern (I need a wildcard to
finish the pattern, i.e. "motorcycle_*") to another
element which already has it's own nodes.
/sample/misc/@*[starts-with(local-name(),'motorcycle_')]

I can get the motorcycle element and it's attributes using
copy-of, but that seems to keep me locked from being able
to then add additional attributes to it.

So create all the attributes *before* you start creating any
child nodes. Smart, eh?
 
M

Mike N.

/sample/misc/@*[starts-with(local-name(),'motorcycle_')]
I can get the motorcycle element and it's attributes using
copy-of, but that seems to keep me locked from being able
to then add additional attributes to it.

So create all the attributes *before* you start creating any
child nodes. Smart, eh?

Thank you for the example on how to pull out the attributes :)

I am not sure what you mean in the second part, I was trying to add
attributes immediatly after the copy-of. I tried adding attributes
like this:

<xsl:copy-of select=".">
<xsl:attribute name="motorcycle_engine"><xsl:value-of select="/
misc/@motorcycle_engine" />
</xsl:attribute>
</xsl:copy-of>

Thinking I could add the end tag like you do on a regular copy tag but
got an error.

But I am learning and see I probably need to use regular copy and then
@* to grab all of the original attributes before trying to add the
attributes in from the misc element.

Again, thanks.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top