У меня есть определенная структура вывода XML, которая не соответствует моим требованиям. По этой причине я рассматриваю возможность использования кода XSLT для преобразования текущей структуры в требуемую.
Я понимаю, что мне нужна переменная XSLT, а также условие if для завершения желаемого результата.
Это мой текущий ввод XML:
<ShipmentInstruction>
<CarrierCode>AFH</CarrierCode>
<CarrierProductCode>AFH</CarrierProductCode>
<ShipmentNoOfBoxes>2</ShipmentNoOfBoxes>
<Boxes>
<Box>
<ShipmentBoxNr>0000000011</ShipmentBoxNr>
<ShipmentBoxReference>0000000011</ShipmentBoxReference>
<ShipmentBoxWeight>1.5</ShipmentBoxWeight>
<Contents>
<Content>
<BarCode>8719829088824</BarCode>
</Content>
</Contents>
</Box>
<Box>
<ShipmentBoxNr>0000000011</ShipmentBoxNr>
<ShipmentBoxReference>0000000011</ShipmentBoxReference>
<ShipmentBoxWeight>1.5</ShipmentBoxWeight>
<Contents>
<Content>
<BarCode>8719829088831</BarCode>
</Content>
</Contents>
</Box>
<Box>
<ShipmentBoxNr>0000000011</ShipmentBoxNr>
<ShipmentBoxReference>0000000011</ShipmentBoxReference>
<ShipmentBoxWeight>1.5</ShipmentBoxWeight>
<Contents>
<Content>
<BarCode>8719829088848</BarCode>
</Content>
</Contents>
</Box>
<Box>
<ShipmentBoxNr>0000000022</ShipmentBoxNr>
<ShipmentBoxReference>0000000022</ShipmentBoxReference>
<ShipmentBoxWeight>7.5</ShipmentBoxWeight>
<Contents>
<Content>
<BarCode>8719829088572</BarCode>
</Content>
</Contents>
</Box>
<Box>
<ShipmentBoxNr>0000000022</ShipmentBoxNr>
<ShipmentBoxReference>0000000022</ShipmentBoxReference>
<ShipmentBoxWeight>7.5</ShipmentBoxWeight>
<Contents>
<Content>
<BarCode>8719829088589</BarCode>
</Content>
</Contents>
</Box>
</Boxes>
</ShipmentInstruction>
И это мой требуемый вывод:
<ShipmentInstruction>
<CarrierCode>AFH</CarrierCode>
<CarrierProductCode>AFH</CarrierProductCode>
<ShipmentNoOfBoxes>2</ShipmentNoOfBoxes>
<Boxes>
<Box>
<ShipmentBoxNr>0000000011</ShipmentBoxNr>
<ShipmentBoxReference>0000000011</ShipmentBoxReference>
<ShipmentBoxWeight>1.5</ShipmentBoxWeight>
<Contents>
<Content>
<BarCode>8719829088824</BarCode>
</Content>
<Content>
<BarCode>8719829088831</BarCode>
</Content>
<Content>
<BarCode>8719829088848</BarCode>
</Content>
</Contents>
</Box>
<Box>
<ShipmentBoxNr>0000000022</ShipmentBoxNr>
<ShipmentBoxReference>0000000022</ShipmentBoxReference>
<ShipmentBoxWeight>7.5</ShipmentBoxWeight>
<Contents>
<Content>
<BarCode>8719829088572</BarCode>
</Content>
<Content>
<BarCode>8719829088589</BarCode>
</Content>
</Contents>
</Box>
</Boxes>
</ShipmentInstruction>
XSLT, который я использовал до тех пор, пока не появится:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:element name="ShipmentInstruction">
<xsl:element name="CarrierCode">
<xsl:value-of select="//ShipmentInstruction/CarrierCode"/>
</xsl:element>
<xsl:element name="CarrierProductCode">
<xsl:value-of select="//ShipmentInstruction/CarrierProductCode"/>
</xsl:element>
<xsl:element name="ShipmentNoOfBoxes">
<xsl:value-of select="//ShipmentInstruction/ShipmentNoOfBoxes"/>
</xsl:element>
<xsl:element name="Boxes">
<xsl:for-each select="//ShipmentInstruction/Boxes/Box">
<xsl:variable name="v1" select="ShipmentBoxNr"/>
<xsl:element name="Box">
<xsl:variable name="v2" select="ShipmentBoxNr"/>
<xsl:element name="ShipmentBoxNr">
<xsl:value-of select="ShipmentBoxNr"/>
</xsl:element>
<xsl:element name="ShipmentBoxReference">
<xsl:value-of select="ShipmentBoxReference"/>
</xsl:element>
<xsl:element name="ShipmentBoxWeight">
<xsl:value-of select="ShipmentBoxWeight"/>
</xsl:element>
<xsl:element name="Contents">
<xsl:for-each select="//Contents/Content">
<xsl:element name="Content">
<xsl:element name="Barcode">
<xsl:value-of select="Barcode"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>