且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 xslt 删除嵌入的 html 标签

更新时间:2023-12-05 19:56:52

如果你事先知道使用了哪些 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>