Search code examples
debuggingxsltattributesdeclarationxml-literals

XSLT simplified stylesheet with xsl:attribute-set element


I'm trying to use the <xsl:attribute-set> in my xsl document but I keep getting error messages:

  • compilation error: line 47 element attribute-set
  • element attribute-set only allowed as child of stylesheet

I've also checked the W3Schools website's explanation on XSLT attribute-sets and found out that:

Must be child of <xsl:stylesheet> or <xsl:transform>.

I don't understand what this means, can anyone explain?

If you need more information about my documents, WAMP server set up please comment below.

The first two lines of my XSL document is:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">

I have no idea what they do, just that without it, my XSL will not work.

I am basically transforming my XML into HTML using this XSL file. The whole process is done by PHP:

# START XSLT
$xslt = new XSLTProcessor(); 
$XSL = new DOMDocument(); 
$XSL->load('hello.xsl'); 
$xslt->importStylesheet($XSL); 

# LOAD XML FILE 
$XML = new DOMDocument();
$XML->load('hello.xml');

#PRINT 
print $xslt->transformToXML($XML);

Solution

  • You are using the very rarely-seen "literal result element as stylesheet" facility, also known as a "simplified stylesheet", in which the xsl:stylesheet element and the outermost xsl:template are implicit. Your problem illustrates why this facility is so rarely used - it quickly runs out of steam. Because there is no xsl:stylesheet element, none of the usual children of xsl:stylesheet can be present, and this includes declarations of attribute sets.

    Change your code to wrap it in an explicit xsl:stylesheet and xsl:template match="/". Then add an xsl:attribute-set at the same level as the xsl:template.