我是 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">首先呼叫,它會洗掉除“ &nbsp”之外的所有 HTML 標簽
如果<xsl:template name="replace-string">首先呼叫,它會替換“ &nbsp”,''但不會洗掉其他 HTML 標記。
我在when子句中呼叫這些模板。如何解決這個問題?我需要洗掉所有 HTML 標簽。有沒有辦法一次性完成,還是我遺漏了什么?
uj5u.com熱心網友回復:
確保您正在清除標簽之間的值,而不僅僅是選擇它。而不是xsl:value-of,通過replace-string模板運行它。
此外,您可能希望&nbsp;用空格' '代替空字串替換。否則,你會得到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>
uj5u.com熱心網友回復:
如果您想使用一個模板的輸出作為另一個模板的輸入,則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>
簡化演示:https : //xsltfiddle.liberty-development.net/eiorv1s
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/391145.html
上一篇:使用XPath查找最靠后的元素
