Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert xml to xml

Status
Not open for further replies.

wyrdo

Technical User
Sep 19, 2013
4
US
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
XML:
<rootElement>
   <Variables>
      <Variable name="varname" value="varvalue" />
   </Variables>
</rootElement>

I want it to convert to
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:
<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?
 
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>
 
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:
<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:
<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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top