从 XML 文件中删除 HTML 标签
Strip HTML Tags in from XML file
我在尝试使用 XSLT 去除 XML 文件中的 HTML 标签时遇到问题。
输入XML
<ns1:productSpecificationFullDTO xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple">
<ns1:specification>
<ns1:CopySection>
<ns1:otherRangeName>
<![CDATA[CNF - BOP 1904
<b>BOP 1904</b>]]>
</ns1:otherRangeName>
<ns1:additionalPrintOfPackCopy>
<![CDATA[CNF FOP 1904 <b>FOP 1904</b>]]>
</ns1:additionalPrintOfPackCopy>
</ns1:CopySection>
</ns1:ns1:specification></ns1:productSpecificationFullDTO>
XSLT 定义
虽然剥离 HTML 标签的功能适用于少数属性,但它不适用于某些
<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:choose>
<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:template>
<xsl:template match="/">
<ItemDetails>
<Items>
<!-- Food section start here -->
<LongDescription>
<xsl:variable name="productCheck" select="ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:otherRangeName"/>
<xsl:choose>
<xsl:when test="$productCheck != ''">
<xsl:call-template name="strip-html-tags">
<xsl:with-param name="text" select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:otherRangeName)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:additionalPrintOfPackCopy)"/>
</xsl:otherwise>
</xsl:choose>
</LongDescription>
</Items>
</ItemDetails>
</xsl:template></xsl:stylesheet>
期望输出
输出应如下所示,其中删除了所有 HTML 标签
<ItemDetails xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple">
<Items>
<Item>
<LongDescription>CNF - BOP 1904 BOP 1904<LongDescription/>
</Item>
</Items>
</ItemDetails>
实际输出
我得到的实际输出包括额外的 HTML 不需要的标签,应该删除
<ItemDetails xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple">
<Items>
<Item>
<LongDescription>CNF - BOP 1904 BOP 1904BOP 1904</b>CNF - BOP 1904 <b>BOP 1904</b><LongDescription/>
</Item>
</Items>
</ItemDetails>
请协助。提前致谢
试试这个:
<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 match="/">
<ItemDetails>
<Items>
<!-- Food section start here -->
<LongDescription>
<!-- First determine which content to be stripped -->
<xsl:variable name="stripFromHtml">
<xsl:variable name="otherRangeName" select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:otherRangeName)"/>
<xsl:choose>
<xsl:when test="$otherRangeName">
<xsl:value-of select="$otherRangeName"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:additionalPrintOfPackCopy)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Second strip tags -->
<xsl:variable name="tags-stripped">
<xsl:call-template name="strip-html-tags">
<xsl:with-param name="text" select="$stripFromHtml"/>
</xsl:call-template>
</xsl:variable>
<!-- Third translate nbsp's -->
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$tags-stripped"/>
<xsl:with-param name="replace" select="'&nbsp;'"/>
<xsl:with-param name="with" select="' '"/>
</xsl:call-template>
</LongDescription>
</Items>
</ItemDetails>
</xsl:template>
<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:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我在尝试使用 XSLT 去除 XML 文件中的 HTML 标签时遇到问题。
输入XML
<ns1:productSpecificationFullDTO xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple">
<ns1:specification>
<ns1:CopySection>
<ns1:otherRangeName>
<![CDATA[CNF - BOP 1904
<b>BOP 1904</b>]]>
</ns1:otherRangeName>
<ns1:additionalPrintOfPackCopy>
<![CDATA[CNF FOP 1904 <b>FOP 1904</b>]]>
</ns1:additionalPrintOfPackCopy>
</ns1:CopySection>
</ns1:ns1:specification></ns1:productSpecificationFullDTO>
XSLT 定义 虽然剥离 HTML 标签的功能适用于少数属性,但它不适用于某些
<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:choose>
<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:template>
<xsl:template match="/">
<ItemDetails>
<Items>
<!-- Food section start here -->
<LongDescription>
<xsl:variable name="productCheck" select="ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:otherRangeName"/>
<xsl:choose>
<xsl:when test="$productCheck != ''">
<xsl:call-template name="strip-html-tags">
<xsl:with-param name="text" select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:otherRangeName)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:additionalPrintOfPackCopy)"/>
</xsl:otherwise>
</xsl:choose>
</LongDescription>
</Items>
</ItemDetails>
</xsl:template></xsl:stylesheet>
期望输出 输出应如下所示,其中删除了所有 HTML 标签
<ItemDetails xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple">
<Items>
<Item>
<LongDescription>CNF - BOP 1904 BOP 1904<LongDescription/>
</Item>
</Items>
</ItemDetails>
实际输出 我得到的实际输出包括额外的 HTML 不需要的标签,应该删除
<ItemDetails xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple">
<Items>
<Item>
<LongDescription>CNF - BOP 1904 BOP 1904BOP 1904</b>CNF - BOP 1904 <b>BOP 1904</b><LongDescription/>
</Item>
</Items>
</ItemDetails>
请协助。提前致谢
试试这个:
<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 match="/">
<ItemDetails>
<Items>
<!-- Food section start here -->
<LongDescription>
<!-- First determine which content to be stripped -->
<xsl:variable name="stripFromHtml">
<xsl:variable name="otherRangeName" select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:otherRangeName)"/>
<xsl:choose>
<xsl:when test="$otherRangeName">
<xsl:value-of select="$otherRangeName"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:CopySection/ns1:additionalPrintOfPackCopy)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Second strip tags -->
<xsl:variable name="tags-stripped">
<xsl:call-template name="strip-html-tags">
<xsl:with-param name="text" select="$stripFromHtml"/>
</xsl:call-template>
</xsl:variable>
<!-- Third translate nbsp's -->
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$tags-stripped"/>
<xsl:with-param name="replace" select="'&nbsp;'"/>
<xsl:with-param name="with" select="' '"/>
</xsl:call-template>
</LongDescription>
</Items>
</ItemDetails>
</xsl:template>
<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:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>