user1535147
user1535147

Reputation: 1475

xsl:strip-space is not removing the carriage return

This is the xml file. The spaces and carriage return in the "CategoryName" element is intended.

<?xml version="1.0" encoding="utf-8"?>
<group>
    <item>
        <id>item 1</id>
        <CategoryName>


        </CategoryName>
    </item>
    <item>
        <id>item 2</id>
        <CategoryName></CategoryName>
    </item>
    <item>
        <id>item 3</id>
        <CategoryName>      </CategoryName>
    </item>

</group>

The following is the XSLT file for the XML file above. What it is suppose to do is that it will clear all the empty spaces in the "CategoryName" element. Then, it will test the whether "CategoryName" is empty or not.

<?xml version="1.0" encoding="utf-8"?>
<!-- DWXMLSource="testempty.xml" -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

  <xsl:strip-space elements="*" /> <!--HERE IS STRIP SPACE-->


  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Untitled Document</title>
    </head>

    <body>
    <xsl:for-each select="/group/item">
      <xsl:if test="CategoryName = ''"> <!--HERE IS THE TEST-->
        <p>Empty</p>          <!--IT WILL OUTPUT 'EMPTY' IF THE ELEMENT IS EMPTY-->
      </xsl:if>
    </xsl:for-each>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

The problem is, xsl:strip-space is not doing its job. Only Item 2's "CategoryName" pass the "empty" test.

What is wrong?

Upvotes: 5

Views: 1479

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

Clearly, DeamWeaver's XSLT processor has a bug.

Here is another way to get the wanted result:

Replace:

   <xsl:if test="CategoryName = ''">

with:

   <xsl:if test="not(normalize-space(CategoryName))">

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163458

I don't know what XSLT engine Dreamweaver is using, but this looks wrong.

I think there may be some XSLT processors that only apply xsl:strip-space if you present them with unparsed input (lexical XML), and not if you present them with a DOM. There's nothing in the spec to justify such behaviour, but it makes the implementor's life a lot easier.

It's worth pointing out however that this is NOT the way xsl:strip-space is intended to be used. It's intended to be used to strip "ignorable" whitespace, that is the whitespace used for indentation in element-only content. If you are using an XSLT 2.0 schema-aware transformation this becomes formalized as a rule: xsl:strip-space will not affect the content of elements having simple content. This is because stripping spaces for such an element could make the element invalid against the schema.

Upvotes: 2

Related Questions