Remove embedded html tags with xslt

2019-06-13 16:00发布

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>

1条回答
Bombasti
2楼-- · 2019-06-13 16:25

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>
查看更多
登录 后发表回答