【发布时间】:2012-10-11 02:45:01
【问题描述】:
我正在尝试使用 XSLT 1.0 从 xml 字符串中排除某些 html 标记
这里,目前我不包括<a> 和<img> 标签。对于<a> 标签,我只想显示文本。
尝试过 XSLT 模板:
<xsl:template match="*" mode="ExcludeHTMLTags">
<xsl:choose>
<xsl:when test="local-name() = 'a' or local-name() = 'img'">
<xsl:value-of select="text()"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()|@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
以下列方式调用上述模板:
<xsl:variable name="guideContent">
<root>
<xsl:apply-templates
select="document(@guideID)/tcm:Component/tcm:Data/tcm:Content/em:GeneralContent/em:Body/node()"
mode="expandXHTML"/>
</root>
</xsl:variable>
<xsl:apply-templates select="msxsl:node-set($guideContent)/node()" mode="ExcludeHTMLTags"/>
输入 XML 字符串:
<root>
This is a test message.
<p>Message within p tag</p> click <a href="www.test.com">here</a>.
<img src="/test.jpg" /> Message after image.
<strong>Message within strong</strong>
<link:component id="XXX" ... >My Link</link:component>
<p>Message after link component</p>
</root>
输出:
<root>
This is a test message.
<p>Message within p tag</p> click here.
Message after image.
<strong>Message within strong</strong>
<link:component id="XXX" ... >My Link</link:component>
<p>Message after link component</p>
</root>
请提出我做错了什么并告诉最好的方法。
【问题讨论】:
-
请提供源 XML 文档和所需的确切结果。
-
@DimitreNovaatchev:我已经更新了示例输入和预期输出。
-
Siva Charan -- 这又是非常畸形的。无论如何,我已经使用格式良好的 (x)Html 回答了您的问题。
-
@DimitreNovaatchev:为了形成良好的格式,我可以包装一个根节点。所以我猜不会有问题。
-
Siva Charan,它的格式仍然不正确——你没有合适的 XML 编辑器/IDE 可以立即看到吗?