我最近问了一个关于XML输出错误的问题,这是因为名称空间放在了一个元素中。
现在,我已经对此进行了进一步的研究,但是不幸的是,我仍然陷在输出中,因为我现在不知道该给前缀加上什么。
希望你能帮助我。
在我现在拥有的文件下面以及我想要的输出下面。
我的输入XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns2:readAllAMDResV04 xmlns:ns2="http://main.jws.com.hanel.de">
<ns2:return>
<article xmlns="http://main.jws.com.hanel.de/xsd">
<articleNumber>Aadrapot99900</articleNumber>
<articleName/>
<inventoryAtStorageLocation>2</inventoryAtStorageLocation>
</article>
<article xmlns="http://main.jws.com.hanel.de/xsd">
<articleNumber>Aadrapot99900</articleNumber>
<articleName/>
<inventoryAtStorageLocation>7</inventoryAtStorageLocation>
</article>
<article xmlns="http://main.jws.com.hanel.de/xsd">
<articleNumber>Ae13963</articleNumber>
<articleName/>
<inventoryAtStorageLocation>128</inventoryAtStorageLocation>
</article>
<article xmlns="http://main.jws.com.hanel.de/xsd">
<articleNumber>PCM11512050E</articleNumber>
<articleName/>
<inventoryAtStorageLocation>68</inventoryAtStorageLocation>
</article>
<ns1:returnValue xmlns:ns1="http://main.jws.com.hanel.de/xsd">0</ns1:returnValue>
</ns2:return>
</ns2:readAllAMDResV04>
</soapenv:Body>
</soapenv:Envelope>
我的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:abc="main.jws.com.hanel.de/xsd"
exclude-result-prefixes="abc"
version="1.0" >
<xsl:key name="key" match="article" use="articleNumber"/>
<xsl:template match="/articles">
<result>
<xsl:apply-templates select="article[generate-id() = generate-id(key('key', articleNumber)[1])]"/>
</result>
</xsl:template>
<xsl:template match="article">
<count>
<articleNumber><xsl:value-of select="articleNumber"/></articleNumber>
<totalQuantity><xsl:value-of select="sum(key('key', articleNumber)/inventoryAtStorageLocation)"/></totalQuantity>
</count>
</xsl:template>
</xsl:stylesheet>
到目前为止,我的输出:
Aadrapot99900
2
Aadrapot99900
7
Ae13963
128
我正在寻找的输出:
<?xml version="1.0" encoding="utf-8"?>
<DataSet>
<article>
<articleNumber>Aadrapot99900</articleNumber>
<totalQuantity>9</totalQuantity>
</article>
<article>
<articleNumber>Ae13963</articleNumber>
<totalQuantity>128</totalQuantity>
</article>
</DataSet>
我认为我必须在某处添加abc前缀,但不知道确切在哪里,而且我不确定关键结果是否匹配。
提前致谢!
这:
<xsl:template match="/articles">
无法工作,因为articles
你的输入中没有命名元素-当然不是根元素。
尝试将其更改为:
<xsl:template match="ns2:return">
添加xmlns:ns2="http://main.jws.com.hanel.de"
声明后。你可能需要添加:
<xsl:strip-space elements="*"/>
在样式表的顶部。
这:
xmlns:abc="main.jws.com.hanel.de/xsd"
是错的。实际的名称空间是不同的。它必须是:
xmlns:abc="http://main.jws.com.hanel.de/xsd"
解决此问题后,请将所有引用更改article
为abc:article
。同样对于它的后代articleNumber
和上帝inventoryAtStorageLocation
。
。