在处理xml文件时是否可以跳过节点?例如:说我有以下xml代码:
<mycase desc="">
<caseid> id_1234 </caseid>
<serid ref=""/>
......
......
......
</mycase>
Run Code Online (Sandbox Code Playgroud)
我想让它看起来像这样:
<mycase desc="" caseid="id_1234">
.....
.....
</mycase>
Run Code Online (Sandbox Code Playgroud)
目前我这样做:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:err="http://www.w3.org/2005/xqt-errors">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="mycase">
<xsl:element name="mycase">
<xsl:attribute name="desc"/>
<xsl:attribute name="caseid">
<xsl:value-of select="caseid"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
......
......
Run Code Online (Sandbox Code Playgroud)
这确实创造了我想要的东西,但因为<xsl:apply-templates/>它处理所有节点.虽然我希望它一起跳过处理caseid和serid.这也适用于其他节点,这些节点在新的XML结构中不可用.那么如何跳过我不想使用xslt处理的节点.
您可以使用空模板来抑制输入文档中某些节点的输出:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="mycase">
<mycase caseid="{caseid}">
<xsl:apply-templates select="@*|node()"/>
</mycase>
</xsl:template>
<xsl:template match="caseid|serid"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)