JobV
JobV

Reputation: 33

XML with XSLT Transformation

I'm trying to transform a simple XML document using the XSLT Transform, although when I try to run the XML on the browser [Chrome or Safari], I'm getting an error 'The document is empty', but the XML that I'm using is not empty.

Any hints where my error is and also how to solve it? much appreciated.

XML - people3.xml

<?xml version="1.0" standalone="no"?>

<?xml-stylesheet type="text/xsl" href="people.xsl"?>

<people>
    <husband employee="Yes">
        <name>Mark</name>
        <age>45</age>
            <wife>
                <wname>Janet</wname>
                <age>29</age>
            </wife>
    </husband>
    <husband employee="No">
        <name>Matt</name>
        <age>42</age>
            <wife>
                <wname>Annie</wname>
                <age>41</age>
            </wife>
    </husband>
</people>

XSL - people.xsl

<?xml version="1.0"?>

<xdl:stylesheet version='1.0'

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="name">
    Hello from XSLT
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Views: 239

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

As far as I can tell, you are just running into a WebKit specific limitation of the type of result you can produce with XSLT in the browser, instead of just pushing some text out to the result as a fragment you should try to construct some HTML document so a minimal sample constructing a HTML result document and incorporating your template would be

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

  <xsl:output method="html" indent="yes" version="5" doctype-system="about:legacy-doctype"/>

<xsl:template match="name">
    Hello from XSLT
</xsl:template>

  <xsl:template match="/">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

That way that message about an empty document should go away and some result would be rendered, not really a meaningful one as there are built-in templates that kick in to output any text nodes anyway and as your template matches two name elements in the input sample so the body of the constructed HTML will be like

Hello from XSLT

    45
        
            Janet
            29
        


    
Hello from XSLT

    42
        
            Annie
            41

It is not clear what output you want to achieve but keep in mind that you are better off in the browser to start with creating some more or less well-structured HTML document to be rendered than working with text fragments.

Upvotes: 1

Related Questions