请参阅XSLT,输入XML,所需的输出XML和实际输出,我将在下面。
<abc></abc>
<soapenv:Body>
转换为<Body>
(我想保留原样) XSLT
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="grandParent">
<xsl:copy>
<xsl:apply-templates select="@*" />
<childValues>
<xsl:value-of select="normalize-space(.)" />
</childValues>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输入XML -
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<netconf:rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
<netconf:edit-config>
<netconf:target>
<netconf:url/>
</netconf:target>
<netconf:config>
<requests module="E5100">
<request action="create" userName="sigma" sessionId="_sessionId">
<SSA>
<NetworkName>NTWK-ntwk_nm</NetworkName>
<PortNumber>2</PortNumber>
<PortType>vdsl</PortType>
<SSAProvision>
<UserDescr></UserDescr>
<SubscriberID></SubscriberID>
</SSAProvision>
</SSA>
</request>
</requests>
</netconf:config>
</netconf:edit-config>
</netconf:rpc>
</soapenv:Body>
</soapenv:Envelope>
所需的输出XML:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<netconf:rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
<netconf:edit-config>
<netconf:target>
<netconf:url/>
</netconf:target>
<netconf:config>
<requests module="E5100">
<request action="create" sessionId="_sessionId" userName="sigma">
<SSA>
<NetworkName>NTWK-ntwk_nm</NetworkName>
<!-- Port Number removed-->
<PortType>vdsl</PortType>
<SSAProvision>
<UserDescr/>
<SubscriberID/>
</SSAProvision>
</SSA>
</request>
</requests>
</netconf:config>
</netconf:edit-config>
</netconf:rpc>
</soapenv:Body>
</soapenv:Envelope>
实际输出:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
<edit-config>
<target>
<url/>
</target>
<config>
<requests module="E5100">
<request action="create" sessionId="_sessionId" userName="sigma">
<SSA>
<NetworkName>NTWK-ntwk_nm</NetworkName>
<!-- want to remove this PortNumber completely-->
<PortNumber/>
<PortType>vdsl</PortType>
<SSAProvision>
<UserDescr/>
<SubscriberID/>
</SSAProvision>
</SSA>
</request>
</requests>
</config>
</edit-config>
</rpc>
</Body>
</Envelope>
答案 0 :(得分:2)
要删除没有内容的元素,您可以添加模板规则
<xsl:template match="*[not(child::node())]"/>
正如其他人所说,命名空间信息的丢失是所使用的工具链中的一个错误。我建议(a)确切地确定正在使用什么工具链,(b)检查是否存在可能修复错误的相同工具的更高版本,(c)失败,转移到不同的XSLT处理器和/或XML解析器。
有时,发现使用XSLT处理器的最简单方法是添加类似:
<xsl:template match="/">
<xsl:comment>Generated using <xsl:value-of select="system-property('xsl:vendor')"/></xsl:comment>
<xsl:apply-templates/>
</xsl:template>