我有下面的虚拟xml文件,我需要去掉TXT中的标记。我已经创建了一个样式表,它成功地剥离了所有的文件中的标记,但我只希望它只去除TXT块中的标记。为了实现这一点,我需要对XSLT进行哪些更改?
XML
<DOC>
<ID>1234</ID>
<TXT>
<A><DESC type="PERSON">George Washington</DESC> lived in a house called <DESC type="PLACE">Mount Vernon.</DESC></A>
<A><DESC type="PERSON">Thomas Jefferson</DESC> lived in a house called <DESC type="PLACE">Monticello.</DESC></A>
</TXT>
</DOC>XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="strip-tags">
<xsl:param name="TXT"/>
<xsl:choose>
<xsl:when test="contains($TXT, 'A')">
<xsl:value-of select="$TXT"/>
<xsl:call-template name="strip-tags">
<xsl:with-param name="TXT" select="substring-after($TXT, 'A')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$TXT"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>电流输出
<?xml version="1.0" encoding="UTF-8"?>
1234
George Washington lived in a house called Mount Vernon.
Thomas Jefferson lived in a house called Monticello.期望输出
<?xml version="1.0" encoding="UTF-8"?>
<DOC><ID>1234</ID>
<TXT>George Washington lived in a house called Mount Vernon.
Thomas Jefferson lived in a house called Monticello.</TXT>
</DOC>发布于 2019-03-11 15:56:16
重新制定您的请求:
除
TXT元素的子代外,每个节点都被转换为自身。
使用同一性变换
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="TXT//*">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>结果:
<DOC>
<ID>1234</ID>
<TXT>
George Washington lived in a house called Mount Vernon.
Thomas Jefferson lived in a house called Monticello.
</TXT>
</DOC>https://stackoverflow.com/questions/55104974
复制相似问题