Developer forum

Forum » PIM » PIM feed with XSLT - invalid attribute character

PIM feed with XSLT - invalid attribute character

Michael Knudsen
Reply

Hello forum,

- I have built a PIM XML feed and are trying to remove all HTML formatting from the Description field, but when saving the XSLT I'm getting an error:

The error is related to the line: <xsl:when test="contains($string, '&lt;')"> 

If I use a XSLT validation and transformation tool everything works as expected. How to have this working in DW?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="LongDescription">
    <xsl:copy>
        <xsl:call-template name="remove-markup">
            <xsl:with-param name="string" select="."/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

<xsl:template name="remove-markup">
    <xsl:param name="string"/> 
    <xsl:choose>
        <xsl:when test="contains($string, '&lt;')">
            <xsl:value-of select="substring-before($string, '&lt;')" />
            <!-- recursive call -->
            <xsl:call-template name="remove-markup">
                <xsl:with-param name="string" select="substring-after($string, '&gt;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

Br. Michael Knudsen


Replies

 
Michael Knudsen
Reply

Does anyone have a different approach to solving this challenge?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Where does the error comes when you save the xslt - is in the file edit in Files?

 
Michael Knudsen
Reply

The error is shown in the Editor when I save, no matter if the XSLT is open from FILES > Templates > Feeds or from the 'pen' icon on the PIM feed.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Does it work whe you also escape the ampersand? I.e '&amp;lt;' in the place where you have '&lt;' now? Or does that break your test?

Imar

 
Michael Knudsen
Reply

Interresting, because now it doesn't gives an error on line 24:
<xsl:when test="contains($string, '&amp;lt;')">

But now it gives another error on line 25 when I also escape the ampersand:
<xsl:value-of select="substring-before($string, '&amp;lt;')" />

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Hi Michael,

As far as I understand, the given error message is due to not all XSLT processors supporting 'xsl:strip-space' on a loaded XML document. I know it does not remove the whitespaces then, but if you remove that line, does it then at least process without error? If that is the case, maybe we can find another approach.

BR
Snedker

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Hi Michael,

If this is my input:

<?xml version="1.0" encoding="UTF-8"?>
<root>  
   <LongDescription> This is <b>bold</b> text with some spaces. </LongDescription>
   <LongDescription>  <i>Italic</i> and <u>underlined</u> text.</LongDescription> 
</root>

 

Image, just to show the white-spaces:

 

And then containing the whitespace-strip within remove-markup template with this xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="LongDescription">
    <xsl:copy>
        <xsl:call-template name="remove-markup">
            <xsl:with-param name="string" select="."/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

<xsl:template name="remove-markup">
    <xsl:param name="string"/> 
    <xsl:variable name="no-whitespace-string" select="normalize-space($string)" />
    <xsl:choose>
        <xsl:when test="contains($no-whitespace-string, '&amp;lt;')">
            <xsl:value-of select="substring-before($no-whitespace-string, '&lt;')" />
            <!-- recursive call -->
            <xsl:call-template name="remove-markup">
                <xsl:with-param name="string" select="substring-after($no-whitespace-string, '&gt;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$no-whitespace-string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

 

Then the output is

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <LongDescription>This is bold text with some spaces.</LongDescription>
 <LongDescription>Italic and underlined text.</LongDescription>
</root>
 
It seems to do the job, but let me know if it works for you too.
 
BR
Snedker
 
Michael Knudsen
Reply

Hello Morten,

- I have copied your XSLT example with the whitespace-strip into a new xslt file on a DW9.15.13 solution - and saving gives the same result :-| 

Br. Michael Knudsen

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Do you know which exact part of the XML input that fails? Can you perhaps share the input XML (or give me info about site/file, then I can grab it myself)..?

BR
Snedker

 
Michael Knudsen
Reply

It's the XSLT file that shows the error. Assume you can take your own XSLT and save as a file on any DW solution and then reopen it and try save it.

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

I have attached the my xslt to this message. When exactly do you get the error message? Does your xslt fail to compile, or does the error show when you try to process xml through it?

/Snedker

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Hi Michael,

I believe we can conclude that the recent struggles with invalid attribute character relate to flaws in Dynamicweb Admin when saving xml file with certain content. We will try and improve that. For work-around try using a dedicated xml editor of your preferred choice, and upload the file(s) instead. I believe the initial problem described in this thread has been tackled. Otherwise let me know.

BR
Snedker

 

You must be logged in to post in the forum