<?xml version="1.0" encoding="UTF-8"?>
<Orders>
<Order>
<OrderId>870</OrderId>
<Positions>
<Position>
<No>1</No>
<Product>sul acid</Product>
<Quantity>500</Quantity>
</Position>
<Position>
<No>2</No>
<Product>pyridine cde</Product>
</Position>
</Positions>
</Order>
<Order>
<OrderId>800</OrderId>
<Positions>
<Position>
<No>1</No>
<Product>Thingamajig</Product>
<Quantity>60</Quantity>
</Position>
</Positions>
</Order>
</Orders>
--------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<!-- Match the root element of the input document -->
<xsl:template match="Orders">
<ORDERLIST>
<!-- Apply templates to each Order element -->
<xsl:apply-templates select="Order"/>
</ORDERLIST>
</xsl:template>
<!-- Match each Order element -->
<xsl:template match="Order">
<!-- Get the OrderId value -->
<xsl:variable name="orderId"
select="OrderId"/>
<!-- Apply templates to each Position element -->
<xsl:apply-templates select="Positions/Position"/>
</xsl:template>
<!-- Match each Position element -->
<xsl:template match="Position">
<!-- Get the No and Product values -->
<xsl:variable name="no"
select="No"/>
<xsl:variable name="product"
select="Product"/>
<!-- Check if the Quantity element exists -->
<xsl:choose>
<!-- If it exists, use its value -->
<xsl:when test="Quantity">
<ITEM>
<ID>
<xsl:value-of select="../OrderId"/>
</ID>
<POSITION>
<xsl:value-of select="$no"/>
</POSITION>
<PRODUCT>
<xsl:value-of select="$product"/>
</PRODUCT>
<QUANTITY>
<xsl:value-of select="Quantity"/>
</QUANTITY>
</ITEM>
</xsl:when>
<!-- If it does not exist, use a default value of 1 -->
<xsl:otherwise>
<ITEM>
<ID>
<xsl:value-of select="../OrderId"/>
</ID>
<POSITION>
<xsl:value-of select="$no"/>
</POSITION>
<PRODUCT>
<xsl:value-of select="$product"/>
</PRODUCT>
<QUANTITY>1</QUANTITY>
</ITEM>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<ORDERLIST>
<xsl:for-each select="Orders/Order">
<xsl:variable name="orderId"
select="OrderId"/>
<xsl:for-each select="Positions/Position">
<ITEM>
<ID>
<xsl:value-of select="$orderId"/>
</ID>
<POSITION>
<xsl:value-of select="No"/>
</POSITION>
<PRODUCT>
<xsl:value-of select="Product"/>
</PRODUCT>
<!-- <QUANTITY><xsl:value-of select="Quantity/text() | '1'"/></QUANTITY>-->
<QUANTITY>
<xsl:choose>
<xsl:when test="Quantity">
<xsl:value-of select="Quantity"/>
</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</QUANTITY>
</ITEM>
</xsl:for-each>
</xsl:for-each>
</ORDERLIST>
</xsl:template>
</xsl:stylesheet>
--------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<!-- Match the root element of the input document -->
<xsl:template match="Orders">
<ORDERLIST>
<!-- Apply templates to each Order element -->
<xsl:apply-templates select="Order"/>
</ORDERLIST>
</xsl:template>
<!-- Match each Order element -->
<xsl:template match="Order">
<!-- Get the OrderId value -->
<xsl:variable name="orderId" select="OrderId"/>
<!-- Apply templates to each Position element -->
<xsl:apply-templates select="Positions/Position">
<!-- Pass the OrderId value as a parameter -->
<xsl:with-param name="orderId" select="$orderId"/>
</xsl:apply-templates>
</xsl:template>
<!-- Match each Position element -->
<xsl:template match="Position">
<!-- Get the No and Product values -->
<xsl:variable name="no" select="No"/>
<xsl:variable name="product" select="Product"/>
<!-- Check if the Quantity element exists, if not set the default value to 1 -->
<xsl:variable name="quantity" select="Quantity || '1'"/>
<ITEM>
<ID><xsl:value-of select="../OrderId"/></ID>
<POSITION><xsl:value-of select="$no"/></POSITION>
<PRODUCT><xsl:value-of select="$product"/></PRODUCT>
<QUANTITY><xsl:value-of select="$quantity"/></QUANTITY>
</ITEM>
</xsl:template>
</xsl:stylesheet>