Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   xtml: combining attributes from one element into a different element (http://www.velocityreviews.com/forums/t598633-xtml-combining-attributes-from-one-element-into-a-different-element.html)

Mike N. 03-14-2008 05:39 AM

xtml: combining attributes from one element into a different element
 
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.

Pavel Lepin 03-14-2008 08:31 AM

Re: xtml: combining attributes from one element into a different element
 

Mike N. <michael.niblett@gmail.com> wrote in
<6f5f5286-0081-4f15-ba02-ca2e6cb5d280@e23g2000prf.googlegroups.com>:
> 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?

--
"...a Netscape engineer who shan't be named once passed a
pointer to JavaScript, stored it as a string and later
passed it back to C, killing 30..." --Blake Ross

Mike N. 03-15-2008 03:00 AM

Re: xtml: combining attributes from one element into a differentelement
 
On Mar 14, 1:31 am, Pavel Lepin <p.le...@ctncorp.com> wrote:


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


Joseph Kesselman 03-17-2008 01:30 PM

Re: xtml: combining attributes from one element into a differentelement
 
> <xsl:attribute name="motorcycle_engine"><xsl:value-of select="/
> misc/@motorcycle_engine" />
> </xsl:attribute>


Quick observation: Use either / before the >, ***OR** a separate
end-element tag. Not both.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden


All times are GMT. The time now is 10:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.