使用 xslt 从 xml 中去除 html 标签 [英] strip html tag from xml using xslt

查看:33
本文介绍了使用 xslt 从 xml 中去除 html 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 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">&lt;UL style="MARGIN-TOP: 0in" type=disc&gt;
&lt;LI style="TEXT-ALIGN: justify;MARGIN-BOTTOM: 0pt" class=MsoNormal&gt;&lt;SPAN style="mso-fareast-font-family: 'timesnewroman'; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-bidi-font-style: italic"&gt;&lt;FONT size=2&gt;Manage the daily activities of the HOD s office.&lt;?xml:namespace prefix = o /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI style="MARGIN-BOTTOM: 0pt" class=MsoNormal&gt;&lt;SPAN style="mso-fareast-font-family: 'timesnewroman'; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-bidi-font-style: italic"&gt;&lt;FONT size=2&gt;Handle and manage all communication, correspondence and filing of documents. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI style="MARGIN-BOTTOM: 0pt" class=MsoNormal&gt;&lt;SPAN style="mso-fareast-font-family: 'timesnewroman'; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-bidi-font-style: italic"&gt;&lt;FONT size=2&gt;Fix appointments, arrange for meetings, conferences etc.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/LI&gt;
 </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, '&lt;')">

        <xsl:value-of select="substring-after($text, '&lt;')"/>



                <xsl:variable name="text" select="substring-after($text, '&gt;')"/>
    </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(., '&lt;/?\w+[^&lt;]*&gt;', '')"/>
</xsl:template>

这篇关于使用 xslt 从 xml 中去除 html 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆