INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

convert xml to xml

convert xml to xml

(OP)
Most of my experience is in converting xml to html. I'm in new waters now in an attemt to transform xml to xml. It's fine until i try to convert to an element with attributes. Then i get stuck, and i've gone through several XML books trying to figure out a way past this. Here's what i have

old xml sample

CODE --> xml

<rootElement>
   <Variables>
      <Variable name="varname" value="varvalue" />
   </Variables>
</rootElement> 

I want it to convert to

CODE --> xml

<rootElement>
   <Data><Var name="varname">varvalue</Var></Data>
</rootElement> 

i wrote the following: but it, of course, does not validate since < is not a valid value to have inside a name.

CODE --> xslt

<xsl:template match="Variables">
   <Data>
   <xsl:for-each select="Variable">
      <Var name="<xsl:value-of select="@name" />"><xsl:value-of select="@value" /></Var>
   </xsl:for-each>
   </Data>
</xsl:template> 

I found xsl:element and xsl:attribute but i can't find anything that shows me how to combine the two. Suggestions?

RE: convert xml to xml

(OP)
I had to step out of the box to solve it, but i managed.

in the stylesheet section, i added the attribute-set

CODE -->

<xsl:attribute-set name="as_variable">
	<xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute>
</xsl:attribute-set> 

and then in the template section i did this:

CODE -->

<xsl:template match="Variables">
	<Data>
	<xsl:for-each select="Variable">
		<xsl:element name="Var" use-attribute-sets="as_variable"><xsl:value-of select="@value" /></xsl:element>
	</xsl:for-each>
	</Data>
</xsl:template> 

As a procedural programmer i cannot EXPLAIN why it works, however it worked, my converted data looked like I wanted it to

CODE -->

<Data>
 <Var name="var1">varval1</Var>
 <Var name="var2">varval2</Var>
</Data> 

RE: convert xml to xml

Wow, you found attribute-set! And used it!! I am impressed.

However, I can offer a much simpler approach on this one. It involves he use of an Attribute Value Template. Here is your original attempt modified to use an AVT:

CODE --> XSLT

<xsl:template match="Variables">
   <Data>
   <xsl:for-each select="Variable">
      <Var name="{@name}"><xsl:value-of select="@value" /></Var>
   </xsl:for-each>
   </Data>
</xsl:template> 

Another alternative would be simply to use xsl:attribute:

CODE --> XSLT

<xsl:template match="Variables">
   <Data>
   <xsl:for-each select="Variable">
      <Var>
        <xsl:attribute name="name"><xsl:value-of select="@name" />"></xsl:attribute>
        <xsl:value-of select="@value" />
      </Var>
   </xsl:for-each>
   </Data>
</xsl:template> 

Isn't XSLT fun? shadeshappy

Tom Morrison
Hill Country Software

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close