使用 xslt 删除嵌入的 html 标签 [英] Remove embedded html tags with xslt

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

问题描述

我的输入 xml 在 Emp_Name 和 Country Elements 中嵌入了 html 标签,我只需要去除 html 标签即可获得以下所需的输出.

能否请您帮忙如何在 XSLT 中实现这一点.

输入 XML:

 <记录><Emp_ID>288237</Emp_ID><Emp_Name><br>约翰</br></Emp_Name><国家><p>美国</p></国家><经理>遗嘱</经理><Join_Date>5/12/2014</Join_Date><经验>9年</经验><项目>abc</项目><技能>java</技能></记录>

期望的输出:

 <记录><Emp_ID>288237</Emp_ID><Emp_Name>John</Emp_Name><国家>美国</国家><经理>遗嘱</经理><Join_Date>5/12/2014</Join_Date><经验>9年</经验><项目>abc</项目><技能>java</技能></记录>

解决方案

如果你事先知道使用了哪些 HTML 标签,你可以这样做:

XSLT 1.0

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/><xsl:strip-space elements="*"/><!-- 身份变换--><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><xsl:template match="br|p"><xsl:apply-templates/></xsl:模板></xsl:stylesheet>

<小时>

<块引用>

是否可以像我一样明确地为这两个字段编写 xslt接收该元素的任何 html 标签(不是

).

那怎么样:

XSLT 1.0

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/><xsl:strip-space elements="*"/><!-- 身份变换--><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><xsl:template match="Emp_Name|Country"><xsl:copy><xsl:value-of select="."/></xsl:copy></xsl:模板></xsl:stylesheet>

My input xml has embedded html tags in Emp_Name and Country Elements and I need to strip off only html tags to get the below desired output.

Could you please assist how this can be achieved in XSLT.

Input XML:

 <root>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name> <br>John</br></Emp_Name>
<Country><p>US</p></Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>abc</Project>
<Skill>java</Skill>
</Record>

Desired Output:

 <root>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>abc</Project>
<Skill>java</Skill>
</Record>

解决方案

If you know in advance which HTML tags are used, you can do:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="br|p">
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>


EDIT:

is it possible to write xslt for those two fields explicitly as I may receive any html tags(not <br> and <p> alone) for that elements.

Then how about:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Emp_Name|Country">
    <xsl:copy>
        <xsl:value-of select="."/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

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

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