XSLT 删除几个节点并获取给定节点及其子节点
XSLT to remove few nodes and get a given node with its child nodes
输入
<?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>
?
这样的节点
可以通过应用以下样式表来实现预期的输出:
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>
输入
<?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>
?
可以通过应用以下样式表来实现预期的输出:
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>