替换 XSL 中的 HTML 标签
Replace HTML tags in XSL
我是 XSL 编程的新手,在尝试从 XML 文件中删除 HTML 标签时遇到问题。
我的输入 XML 文件是:只需添加我感兴趣的标签
<Ingredients>
<p><b>INGREDIENTS:</b> Reduced&nbsp;Fat Cheese (84%) [Reduced Fat<strong>Milk</strong>, Salt, Starter Cultures, Enzyme (Animal Rennet, Lipase)],<strong>Milk</strong> Solids, Water, Emulsifying Salt (331), Salt, Acidity Regulator (330), Preservative (200), Natural Colours (100, 160b).</p>
</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, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="strip-html-tags">
<xsl:with-param name="text" select="substring-after($text, '>')"/>
</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="'&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="'&nbsp;'"/>
<xsl:with-param name="with" select="''"/>-->
</xsl:call-template>
</Ingredients>
</ItemDetails>
</xsl:template>
</xsl:stylesheet>
使用上面的 XSL 文件,我试图删除 HTML 标记,并尝试将 &nbsp;
等特殊字符替换为空。因此我调用了 2 个模板。
<xsl:template name="strip-html-tags">
去除其他标签但不去除“&nbsp
”
所以创建了一个 <xsl:template name="replace-string">
来用 ''
替换“&nbsp
”。
但是这不起作用。任何一个模板在第一次调用时都有效。
如果首先调用 <xsl:template name="strip-html-tags">
,它会删除所有 HTML 标签,除了“&nbsp
”
如果先调用 <xsl:template name="replace-string">
,它会将“&nbsp
”替换为 ''
,但不会删除其他 HTML 标签。
我在 when
子句中调用这些模板。
如何解决这个问题?我需要删除所有 HTML 标签。有没有一种方法可以一次完成,或者我缺少什么?
确保您擦除标签之间的值,而不仅仅是选择它。而不是 xsl:value-of
,运行 它通过 replace-string
模板。
此外,您可能希望将 &nbsp;
替换为 space ' '
而不是空字符串。否则,您将得到 ReducedFat
而不是 Reduced Fat
<xsl:when test="contains($text, '<')">
<!--<xsl:value-of select="substring-before($text, '<')"/>-->
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-before($text, '<')"/>
<xsl:with-param name="replace" select="'&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, '>')"/>
</xsl:call-template>
</xsl:when>
如果您想将一个模板的输出用作另一个模板的输入,则在第二个模板的 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="'&nbsp;'" />
<xsl:with-param name="with" select="''"/>
</xsl:call-template>
我是 XSL 编程的新手,在尝试从 XML 文件中删除 HTML 标签时遇到问题。
我的输入 XML 文件是:只需添加我感兴趣的标签
<Ingredients>
<p><b>INGREDIENTS:</b> Reduced&nbsp;Fat Cheese (84%) [Reduced Fat<strong>Milk</strong>, Salt, Starter Cultures, Enzyme (Animal Rennet, Lipase)],<strong>Milk</strong> Solids, Water, Emulsifying Salt (331), Salt, Acidity Regulator (330), Preservative (200), Natural Colours (100, 160b).</p>
</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, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="strip-html-tags">
<xsl:with-param name="text" select="substring-after($text, '>')"/>
</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="'&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="'&nbsp;'"/>
<xsl:with-param name="with" select="''"/>-->
</xsl:call-template>
</Ingredients>
</ItemDetails>
</xsl:template>
</xsl:stylesheet>
使用上面的 XSL 文件,我试图删除 HTML 标记,并尝试将 &nbsp;
等特殊字符替换为空。因此我调用了 2 个模板。
<xsl:template name="strip-html-tags">
去除其他标签但不去除“&nbsp
”
所以创建了一个 <xsl:template name="replace-string">
来用 ''
替换“&nbsp
”。
但是这不起作用。任何一个模板在第一次调用时都有效。
如果首先调用 <xsl:template name="strip-html-tags">
,它会删除所有 HTML 标签,除了“&nbsp
”
如果先调用 <xsl:template name="replace-string">
,它会将“&nbsp
”替换为 ''
,但不会删除其他 HTML 标签。
我在 when
子句中调用这些模板。
如何解决这个问题?我需要删除所有 HTML 标签。有没有一种方法可以一次完成,或者我缺少什么?
确保您擦除标签之间的值,而不仅仅是选择它。而不是 xsl:value-of
,运行 它通过 replace-string
模板。
此外,您可能希望将 &nbsp;
替换为 space ' '
而不是空字符串。否则,您将得到 ReducedFat
而不是 Reduced Fat
<xsl:when test="contains($text, '<')">
<!--<xsl:value-of select="substring-before($text, '<')"/>-->
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-before($text, '<')"/>
<xsl:with-param name="replace" select="'&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, '>')"/>
</xsl:call-template>
</xsl:when>
如果您想将一个模板的输出用作另一个模板的输入,则在第二个模板的 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="'&nbsp;'" />
<xsl:with-param name="with" select="''"/>
</xsl:call-template>