Home > Blockchain >  Replace HTML tags in XSL
Replace HTML tags in XSL

Time:12-23

I'm new to XSL programming and facing an issue while trying to remove the HTML tags from the XML file.

My Input XML file is: Just adding the tag that I'm interested in

<Ingredients>
        &lt;p&gt;&lt;b&gt;INGREDIENTS:&lt;/b&gt; Reduced&amp;nbsp;Fat Cheese (84%) [Reduced Fat&lt;strong&gt;Milk&lt;/strong&gt;, Salt, Starter Cultures, Enzyme (Animal Rennet, Lipase)],&lt;strong&gt;Milk&lt;/strong&gt; Solids, Water, Emulsifying Salt (331), Salt, Acidity Regulator (330), Preservative (200), Natural Colours (100, 160b).&lt;/p&gt;
 </Ingredients>

My XSL file is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple" exclude-result-prefixes="ns1 ns1">
<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
    <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$with"/>
        <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="substring-after($text,$replace)"/>
            <xsl:with-param name="replace" select="$replace"/>
            <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$text"/>
    </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template name="strip-html-tags">
<xsl:param name="text"/>
<xsl:choose>
    <xsl:when test="contains($text, '&lt;')">
        <xsl:value-of select="substring-before($text, '&lt;')"/>
        <xsl:call-template name="strip-html-tags">
                <xsl:with-param name="text" select="substring-after($text, '&gt;')"/>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="$text"/>
            <xsl:with-param name="replace" select="'&amp;nbsp;'" />
            <xsl:with-param name="with" select="''"/>
        </xsl:call-template>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
    <ItemDetails>
        <SourceSystem>
            <xsl:text>Fusion</xsl:text>
        </SourceSystem>
        <ActionType>
            <xsl:text>Create</xsl:text>
        </ActionType>
        <CreateDateTime>
            <xsl:text>2021-11-10T08:00:00</xsl:text>
        </CreateDateTime>
        <Ingredients>
                    <xsl:call-template name="strip-html-tags">
                        <xsl:with-param name="text" select="ns1:productSpecificationFullDTO/ns1:specificationSectionDetail/ns1:specificationSectionFoodRecipeAndRawMaterialsSection/ns1:onPackIngredientsList"/>
                        <!--<xsl:with-param name="replace" select="'&amp;nbsp;'"/>
                        <xsl:with-param name="with" select="''"/>-->
                    </xsl:call-template>
        </Ingredients>
    </ItemDetails>
</xsl:template>
</xsl:stylesheet>

Using the above XSL file I'm trying to remove the HTML tags and also trying to replace the special character like &amp;nbsp; with empty. Hence I'm calling 2 templates.

<xsl:template name="strip-html-tags"> strips the other tags but not "&amp;nbsp"

So created a <xsl:template name="replace-string"> to replace "&amp;nbsp" with ''.

However this is not working. either of the templates works when invoked first.

If <xsl:template name="strip-html-tags"> is invoked first, it removes all the HTML tags except "&amp;nbsp"

If <xsl:template name="replace-string"> is invoked first, it replace the "&amp;nbsp" with '' but the other HTML tags are not removed.

I am calling these templates in the when clause. How can this issue be solved? I need all the HTML tags to be removed. Is there a way to do it at single go or is it something that I'm missing?

CodePudding user response:

Ensure that you are scrubbing the value that is between the tags, not just selecting it. Instead of xsl:value-of, run it through the replace-string template.

Also, you may want to replace &amp;nbsp; with a space ' ' instead of empty string. Otherwise, you will get ReducedFat instead of Reduced Fat

<xsl:when test="contains($text, '&lt;')">
  <!--<xsl:value-of select="substring-before($text, '&lt;')"/>-->
  <xsl:call-template name="replace-string">
    <xsl:with-param name="text" select="substring-before($text, '&lt;')"/>
    <xsl:with-param name="replace" select="'&amp;nbsp;'" />
    <xsl:with-param name="with" select="' '"/>
  </xsl:call-template>
  <xsl:call-template name="strip-html-tags">
    <xsl:with-param name="text" select="substring-after($text, '&gt;')"/>
  </xsl:call-template>
</xsl:when>

CodePudding user response:

If you want to use the output of one template as the input to the other template, then call the first template within the xsl:with-param instruction of the second template - for example:

    <xsl:call-template name="replace-string">
        <xsl:with-param name="text">
            <xsl:call-template name="strip-html-tags">
                <xsl:with-param name="text" select="Ingredients"/>
            </xsl:call-template>
        </xsl:with-param>
        <xsl:with-param name="replace" select="'&amp;nbsp;'" />
        <xsl:with-param name="with" select="''"/>
    </xsl:call-template>

Simplified demo: https://xsltfiddle.liberty-development.net/eiorv1s

  • Related