Warm tip: This article is reproduced from serverfault.com, please click

Wrong output on XML namespace in element

发布于 2020-12-03 06:41:52

I recently asked a question about an XML output that was wrong, this turned out to be because the namespace was put in an element.

Now I have looked further into this and unfortunately I am still getting stuck on the output, because I don't know exactly what to give the prefixes with now.

Hopefully you can help me.

Below the files as I have them now and the output that I would like to have.

My input 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>

My 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>

My Output so far:

      Aadrapot99900
       
       2
    
    
       Aadrapot99900
       
       7
    
    
       Ae13963
       
       128

The output i'm looking for:

<?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>

I think i have to add the abc prefix somewhere but don't know where exactly, and i'm not sure about the match on the key results.

Thanks in advance!

Questioner
Danny Bierens
Viewed
0
michael.hor257k 2020-12-03 17:52:34

This:

<xsl:template match="/articles">

cannot work because there is no element named articles in your input - and certainly not the root element.

Try changing it to:

<xsl:template match="ns2:return">

after adding a xmlns:ns2="http://main.jws.com.hanel.de" declaration. And you'll probably want to add:

<xsl:strip-space elements="*"/>

at the top of the stylesheet.


This:

xmlns:abc="main.jws.com.hanel.de/xsd"

is wrong. The actual namespace is different. It needs to be:

xmlns:abc="http://main.jws.com.hanel.de/xsd"

Once you have fixed that, change all references to article to abc:article. Likewise for its descendants articleNumber and inventoryAtStorageLocation.

.