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

XLST: rs data dictates Node name

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
GB
I need some help on a specific part of an XSLT I have produced.

I have a DAO recordset with the following fields ("and data")

Field_1 ("Shop")
Field_2 ("London")
Field_3 ("Angels")

The resulting XML must look like:

<?xml version="1.0"?>
<root>
<Shop>
<LocationTown>London</LocationTown>
<EntryName>Angels</EntryName>
</Shop>
</root>

So my xslt will be....

<xsl:stylesheet xmlns:xsl=" <xsl:template match="/">
<Shop>
<xsl:for-each select="//rs:data">
<xsl:apply-templates/>
</xsl:for-each>
</Shop>
</xsl:template>
<xsl:template match="//z:row">
<Name>
<LocationTown>
<xsl:value-of select="@Field_2"/>
</LocationTown>
<EntryName>
<xsl:value-of select="@Field_3"/>
</EntryName>
</xsl:template>
</xsl:stylesheet>


The data in field_1 will only ever be one of two values (Shop or Factory), but I cannot get that value to dictate the name of the node.

I would basically like to say:

<xsl:template match="/">
<xsl:if test="@Field_01='Shop'"
<Shop>
<xsl:for-each select="//rs:data">
<xsl:apply-templates/>
</xsl:for-each>
</Shop>
<xsl:if>
<xsl:if test="@Field_01='Factory'"
<Factory>
<xsl:for-each select="//rs:data">
<xsl:apply-templates/>
</xsl:for-each>
</Factory>
<xsl:if>
</xsl:template>

If the data is 'Shop', have an element of <Shop>.. if the data is 'Factory'.. have entry of <Factory>

Is this possible?

------------------------
Hit any User to continue
 
I have a work around for this.. two xslt files, and a bit of code to check the value of each field prior to applying the transformation.

It's a bit of a clunky hack, but it works I guess.

------------------------
Hit any User to continue
 
You're using the old namespace. Use xmlns:xsl=" instead.

Avoid using "//". Its massively inefficient. Try to specify the path.

To create an element with a dynamic name use <xsl:element>
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <xsl:apply-templates select="//z:row"/>
  </xsl:template>
  <xsl:template match="//z:row">
    <xsl:element name="{@Field_1}">
      <LocationTown>
        <xsl:value-of select="@Field_2"/>
      </LocationTown>
      <EntryName>
        <xsl:value-of select="@Field_3"/>
      </EntryName>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Post your XML for a better answer.

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