我满足一个XML文件的以下要求。
到目前为止,我已经成功满足了前两个要求,但是没有满足后两个要求。 有人可以帮我满足以下要求吗? 预先谢谢你。
下面是我的输入xml:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MainNode xmlns:ns0="http://test/system">
<MainResponse>
<Header>
<Value>12AB</Value>
<QTY>10000</QTY>
<DetailList>
<ActualValue>1</ActualValue>
<ActualValue>2</ActualValue>
<ActualValue>3</ActualValue>
</DetailList>
</Header>
</MainResponse>
</ns0:MainNode>
目标XML应为:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<soapenv:Header/>
<soapenv:Body>
<a123:MainResponse xmlns:a123="http://www.ibm.com/test/One/Type">
<b345:Header xmlns:b345="http://www.ibm.com/test/One/DeType">
<b345:Value>12AB</b345:Value>
<b345:QTY>10000</b345:QTY>
<b345:DetailList>
<b345:ActualValue>1</b345:ActualValue>
<b345:ActualValue>2</b345:ActualValue>
<b345:ActualValue>3</b345:ActualValue>
</b345:DetailList>
</b345:Header>
</a123:MainResponse>
</soapenv:Body>
</soapenv:Envelope>
尝试使用XSLT代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" />
<!-- identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- Removing first node -->
<xsl:template match="/">
<xsl:apply-templates select="*/*" />
</xsl:template>
<!-- Removing namespace -->
<xsl:template match="*">
<xsl:element name="{local-name()}" >
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
请尝试以下XSLT。
您需要对模板进行选择性匹配,并相应地添加名称空间和前缀。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://test/system" exclude-result-prefixes="ns0">
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:template match="ns0:MainNode">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<soapenv:Header />
<soapenv:Body>
<xsl:apply-templates />
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
<xsl:template match="MainResponse">
<xsl:element name="a123:{local-name()}" namespace="http://www.ibm.com/test/One/Type">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="Header | Header//*">
<xsl:element name="b345:{local-name()}" namespace="http://www.ibm.com/test/One/DeType">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
输出
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<soapenv:Header />
<soapenv:Body>
<a123:MainResponse xmlns:a123="http://www.ibm.com/test/One/Type">
<b345:Header xmlns:b345="http://www.ibm.com/test/One/DeType">
<b345:Value>12AB</b345:Value>
<b345:QTY>10000</b345:QTY>
<b345:DetailList>
<b345:ActualValue>1</b345:ActualValue>
<b345:ActualValue>2</b345:ActualValue>
<b345:ActualValue>3</b345:ActualValue>
</b345:DetailList>
</b345:Header>
</a123:MainResponse>
</soapenv:Body>
</soapenv:Envelope>