tags:

views:

210

answers:

2

Hey,

So this may be a strange request but I'll give it a go. I have an xml document

<?xml version="1.0" encoding="utf-8" ?>
<Page>
  <ID>Site</ID>
  <Object>
    <ID>PostCode</ID>
    <Type>div</Type>
    <Class>display-label</Class>
    <Value>PostCode</Value>
  </Object>
  <Object>
    <ID>PostCodeValue</ID>
    <Type>div</Type>
    <Class>display-field</Class>
    <Value>$PostCode</Value>
  </Object>
</Page>

and I'm using this XSL to transform it into a html page

<?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">
          &lt;<xsl:value-of select="Type"/>&gt;
          <xsl:value-of select="Value"/>
          &lt;/<xsl:value-of select="Type"/>&gt;
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

As you can see I'm trying to generate the correct html tag dependent on the type node in the xml. The problem is "<" isn't accepted in the xsl and encoding it stops it from being recognised as a tag.

Any suggestions how I'd go about this?

Thanks in advance

+5  A: 
Mads Hansen
+1 Nice. It works when tested at http://www.w3schools.com/xsl/
Johan
How would I populate this element with the class value
mjmcloug
Do you mean to add a class attribute to your `div`? You can add attributes to the element in a similar fashinon with `<xsl:attribute>` (e.g. `<xsl:element name="{Type}"><xsl:attribute name="class">foo</xsl:attribute><xsl:value-of select="Value" /></xsl:attribute></xsl:element>`)
Mads Hansen
see this question here http://bit.ly/6wAc5L
mjmcloug
I added an answer to the question and also provided an alternative style that achieves the same result using apply-templates(more declarative) rather than nesting for-each clauses (more procedural).
Mads Hansen
A: 

you need to wrap your "<" in a text node with disable-output-escaping like this:

<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="temp.xml" -->
<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:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="Type"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
          <xsl:value-of select="Value"/>
          <xsl:text disable-output-escaping="yes">&lt;/</xsl:text><xsl:value-of select="Type"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Josh
XSLT 1.0 as originally published says: "It is an error for output escaping to be disabled for a text node that is used for something other than a text node in the result tree."
Mads Hansen
xsl:element would be the best way to do this but to answer the actual question you need to use the xsl:text as above.
Josh
It doesn't work when I tested it.
Johan