使用 xslt 从 xml 中去除 html 标签 [英] strip html tag from xml using xslt
问题描述
我想从 xml 中获取普通文本,其中一个字段包含 html 数据.我不能在模板上设置条件.请建议我任何解决方案.
i want to fetch normal text from xml which one field containing html data.i cant put condition on template.pls suggest me any solution.
<?xml version="1.0" encoding="UTF-8"?>
<workdetail>
<field name="summaryText1"><UL style="MARGIN-TOP: 0in" type=disc>
<LI style="TEXT-ALIGN: justify;MARGIN-BOTTOM: 0pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'timesnewroman'; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-bidi-font-style: italic"><FONT size=2>Manage the daily activities of the HOD s office.<?xml:namespace prefix = o /><o:p></o:p></FONT></SPAN></LI>
<LI style="MARGIN-BOTTOM: 0pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'timesnewroman'; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-bidi-font-style: italic"><FONT size=2>Handle and manage all communication, correspondence and filing of documents. <o:p></o:p></FONT></SPAN></LI>
<LI style="MARGIN-BOTTOM: 0pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'timesnewroman'; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-bidi-font-style: italic"><FONT size=2>Fix appointments, arrange for meetings, conferences etc.<o:p></o:p></FONT></SPAN></LI>
</workdetail>
mu xsl 文件为
mu xsl file is as
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<workdetail>
<xsl:apply-templates select="*" />
</workdetail>
</xsl:template>
<xsl:template match="*:workdetail">
<xsl:variable name="text" select="*:field[starts-with(@name,'summaryText1')]"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-after($text, '<')"/>
<xsl:variable name="text" select="substring-after($text, '>')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:stylesheet>
这是在 > 之后返回所有内容标签.我可以在此传递更多值吗,它只会返回文本文档.
this is returning everything after > tag. can i pass more value in this which will return only text document.
推荐答案
使用 Saxon 9.5 PE,您应该能够使用 http://www.saxonica.com/documentation/index.html#!functions/saxon/parse-html:
With Saxon 9.5 PE you should be able to use http://www.saxonica.com/documentation/index.html#!functions/saxon/parse-html:
<xsl:template match="workdetail/field[@name = 'summaryText1']">
<xsl:value-of select="saxon:parse-html(.)"/>
</xsl:template>
你在哪里
<xsl:stylesheet xmlns:saxon="http://saxon.sf.net/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">...</xsl:stylesheet>
在样式表的根元素上声明.
declared on the root element of your stylesheet.
如果您无权访问 HTML 解析器,您可以尝试使用 replace
和正则表达式去除标记,但以下是关于如何处理该问题的建议,正则表达没有经过彻底测试:
If you don't have access to a HTML parser you could try to strip markup with a replace
and a regular expression but the following is made as a suggestion on how to approach that, the regular expression is not tested thoroughly:
<xsl:template match="workdetail/field[@name = 'summaryText1']">
<xsl:value-of select="replace(., '</?\w+[^<]*>', '')"/>
</xsl:template>
这篇关于使用 xslt 从 xml 中去除 html 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!