XSLT删除少数节点并使用其子节点获取给定节点

时间:2016-09-13 10:02:28

标签: xslt stylesheet

输入

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body><jsonObject>
<User>
<No>123</No>
<Id>1</Id>
<MailCode>43</MailCode>
<Number>998</Number>
</User>
</jsonObject></soapenv:Body>
</soapenv:Envelope>

预期产出

<User xmlns="http://sample.org">
<No>123</No>
<Id>1</Id>
<MailCode>43</MailCode>
<Number>998</Number>
</User>

当前的XSLT

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns="http://sample.org">


<xsl:template match="jsonObject">
</xsl:template>

<xsl:template match="User">
        <!--Define the namespace -->
        <xsl:element name="{local-name()}" namespace="http://sample.org">
            <!--apply to above selected node-->
             <xsl:apply-templates select="node()|@*">

        </xsl:apply-templates></xsl:element>
    </xsl:template>


</xsl:stylesheet>

但目前的输出是,

<User xmlns="http://sample.org">
123
1
43
998
</User>

我在这里做错了什么?也有办法直接提取<User>节点的内容,而不是编写单独的模板来删除像<jsonObject>这样的节点吗?

1 个答案:

答案 0 :(得分:1)

可以通过应用以下样式表来实现预期输出:

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="*"/>

<xsl:template match="User | User/*" >
    <xsl:element name="{local-name()}" namespace="http://sample.org">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>
相关问题