Ich bin Anfänger in XSLT und stecke an einem Punkt fest, brauche wenig Hilfe. Ich muss die gesamte Menge in jeder Pflanze summieren. finden Sie unten Input xml.
<?xml version='1.0' encoding='UTF-8'?>
<stock>
<records>
<Plant>1001</Plant>
<quantity>1381</quantity>
<StorageLocation>1001</StorageLocation>
</records>
<records>
<Plant>1001</Plant>
<quantity>20</quantity>
<StorageLocation>4001</StorageLocation>
</records>
<records>
<Plant>1002</Plant>
<quantity>0</quantity>
<StorageLocation>5001</StorageLocation>
</records>
<records>
<Plant>1002</Plant>
<quantity>28</quantity>
<StorageLocation>1901</StorageLocation>
</records>
<records>
<Plant>1003</Plant>
<quantity>1</quantity>
<StorageLocation>1006</StorageLocation>
</records>
<records>
<Plant>1003</Plant>
<quantity>0</quantity>
<StorageLocation>1001</StorageLocation>
</records>
</stock>
Ich habe viel im Internet recherchiert und einen Code gefunden. Es funktioniert nicht und ich kann es nicht beheben.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="records">
<WebshopResponseArea>
<!-- Group each records element by its plant -->
<xsl:for-each-group select="records" group-by="Plant">
<!-- Sum all the elements from the current group -->
<xsl:value-of select="sum(current-group()/quantity)" />
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
Die erwartete XML-Ausgabe ist unten.
<?xml version='1.0' encoding='UTF-8'?>
<WebshopResponseArea>
<Records>
<AvailableStock>1401</AvailableStock>
<Plant>1001</Plant>
</Records>
<Records>
<AvailableStock>28</AvailableStock>
<Plant>1002</Plant>
</Records>
<Records>
<AvailableStock>1</AvailableStock>
<Plant>1003</Plant>
</Records>
</WebshopResponseArea>
Ihre Hilfe wäre sehr willkommen. vielen Dank im Voraus.
Lösung des Problems
Ändern Sie einfach Ihr Spiel von
<xsl:template match="records">
zu
<xsl:template match="stock">
Und etwas mehr Kontext und es wird so aussehen:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="stock">
<WebshopResponseArea>
<!-- Group each records element by its plant -->
<xsl:for-each-group select="records" group-by="Plant">
<Records>
<AvailableStock>
<xsl:value-of select="sum(current-group()/quantity)" />
</AvailableStock>
<xsl:copy-of select="current-group()[1]/Plant"/>
</Records>
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
Keine Kommentare:
Kommentar veröffentlichen