替换XSL中的HTML标记



我是XSL编程的新手,在尝试从XML文件中删除HTML标记时遇到了一个问题。

我的输入XML文件是:只添加我对感兴趣的标签

<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>

我的XSL文件是:

<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>

使用上面的XSL文件,我试图删除HTML标记,并尝试用空替换&amp;nbsp;这样的特殊字符。因此,我调用2个模板。

CCD_ 2剥离其它标签;CCD_ 3";

因此创建了CCD_;CCD_ 5";用CCD_ 6。

然而,这并不奏效。第一次调用时,两个模板中的任何一个都可以工作。

如果首先调用<xsl:template name="strip-html-tags">,它将删除除"0"之外的所有HTML标记;CCD_ 8";

如果首先调用<xsl:template name="replace-string">;CCD_ 10";具有CCD_ 11,但不移除其它HTML标签。

我在when子句中调用这些模板。如何解决这个问题?我需要删除所有的HTML标记。有没有一种方法可以一次性完成,还是我错过了什么?

如果您想使用一个模板的输出作为另一个模板,那么在第二个模板的xsl:with-param指令中调用第一个模板-例如:

<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>

简化演示:https://xsltfiddle.liberty-development.net/eiorv1s

确保您正在清理标记之间的值,而不仅仅是选择它。通过replace-string模板运行它,而不是xsl:value-of

此外,您可能希望将&amp;nbsp;替换为空格' ',而不是空字符串。否则,您将获得ReducedFat而不是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>

最新更新