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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with creating a simple xsl

Status
Not open for further replies.

JazzMaan

Programmer
Jun 15, 2004
57
US
Following is my xml:
****************
<root>
<sequence name="RState">
<entity name="Indicator">
<value name="CurrentState" type="int">2</value>
<value name="Description" type="string">test</value>
</entity>

<entity name="Indicator">
<value name="CurrentState" type="int">3</value>
<value name="Description" type="string">test2</value>
</entity>
</sequence>
</root>

*******************

I would like my output to be:
*******************

<Rstate>
<indicator>
<CurrentState>2</CurrentState>
<Description>test</Description>
<indicator>

<indicator>
<CurrentState>3</CurrentState>
<Description>test2</Description>
<indicator>
<Rstate>

 
<xsl:stylesheet xmlns:xsl=" version="1.0">

<xsl:template match="/">
<xsl:apply-templates select="root/sequence"/>
</xsl:template>

<xsl:template match="sequence">
<xsl:element name="{@name}">
<xsl:apply-templates select="entity"/>
</xsl:element>
</xsl:template>

<xsl:template match="entity">
<xsl:element name="{@name}">
<xsl:apply-templates select="value"/>
</xsl:element>
</xsl:template>

<xsl:template match="value">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
 
Perhaps a more generic solution would be:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="node()[@name != '']">
    <xsl:element name="{@name}">
      <xsl:apply-templates select="node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="root">
    <xsl:apply-templates select="node()"/>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top