tags:

views:

75

answers:

3

this question relates to

http://stackoverflow.com/questions/2086179/xslt-to-generate-html-tags-specified-in-xml

where I had an xml docment and used an xsl to generate html tags using

<xsl:element name="{Type}" >

the problem I have is that I want to specify some html attributes in my xml for example

<Page>
  <ID>Site</ID>
  <Object>
    <ID>PostCode</ID>
    <Type>div</Type>
    <Attributes>
       <Attribute name="class">TestStyle</Attribute>
       <Attribute name="id">TestDiv</Attribute>
    </Attributes>
    <Class>display-label</Class>
    <Value>PostCode</Value>
  </Object>
</Page>

So does any one know how I could populate the xsl:element with the two attributes using xsl?

Thanks

+1  A: 
<xsl:element name="Attribute">
  <xsl:attribute name="class">TestStyle</xsl:attribute>
  <xsl:attribute name="id">TestDiv</xsl:attribute>
</xsl:element>
Anon
I don't want to know how to represent it as xslt but rather how to loop throught and populate it
mjmcloug
I think Anon saw your sample XML as the desired output. It would be helpful to also post a sample of the desired output with your sample input.
Mads Hansen
+1  A: 

You need to fix the Attributes element in your source sample, it's not closed.

You can use xsl:for-each or xsl:apply-templates with select="Attributes/Attribute", to invoke an xsl:attribute element that looks a bit like this:

<xsl:attribute name="{@name}"><xsl:value-of select="text()"/></xsl:attribute>

The thing you need to watch out for is that xsl:attribute must come before anything that adds children to the {Type} element.

Paul Butcher
+2  A: 

Building from the stylesheet that I posted in the previous question, within the element declaration you can iterate through each of the Attributes/Attribute elements and construct the attributes for the element that you are constructing.

You are "standing" on the Object element node inside that for-loop, so you can then iterate over it's Attributes/Attribute elements like this:

<xsl:for-each select="Attributes/Attribute">
  <xsl:attribute name="{@name}"><xsl:value-of select="current()"/></xsl:attribute>
</xsl:for-each>

Applied to your stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
      </head>
      <body>
        <xsl:for-each select="Page/Object">
            <xsl:element name="{Type}" >
                <xsl:for-each select="Attributes/Attribute">
                    <xsl:attribute name="{@name}"><xsl:value-of select="current()"/></xsl:attribute>
                </xsl:for-each>
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

This is an alternative way to achieve the same output, but in a little more delcarative fashion, using apply-templates instead of for-each.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
      </head>
      <body>
        <xsl:apply-templates select="Page/Object" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Object">
    <xsl:element name="{Type}" >
       <xsl:apply-templates select="Attributes/Attribute" />
       <xsl:apply-templates select="Value" />
    </xsl:element>
  </xsl:template>

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

</xsl:stylesheet>
Mads Hansen