xtml: combining attributes from one element into a different element

Discussion in 'XML' started by Mike N., Mar 14, 2008.

  1. Mike N.

    Mike N. Guest

    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.
     
    Mike N., Mar 14, 2008
    #1
    1. Advertising

  2. Mike N.

    Pavel Lepin Guest

    Mike N. <> wrote in
    <>:
    > 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
     
    Pavel Lepin, Mar 14, 2008
    #2
    1. Advertising

  3. Mike N.

    Mike N. Guest

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

    On Mar 14, 1:31 am, Pavel Lepin <> 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.
     
    Mike N., Mar 15, 2008
    #3
  4. 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
     
    Joseph Kesselman, Mar 17, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red
    Replies:
    5
    Views:
    6,296
  2. Replies:
    0
    Views:
    515
  3. Chuck
    Replies:
    0
    Views:
    386
    Chuck
    Aug 28, 2003
  4. Replies:
    4
    Views:
    503
    Joe Kesselman
    Feb 25, 2007
  5. girays
    Replies:
    5
    Views:
    361
    girays
    Apr 24, 2008
Loading...

Share This Page