El ejemplo XMLT muestra como puede transformarse un mensaje XML utilizando un nodo XMLTransformation y una hoja de estilos XSL.
La ubicación de la hoja de estilos que se usa para la transformación de XML puede especificarse de varias formas:
La hoja de estilos de este ejemplo es así:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <SaleEnvelope> <xsl:for-each select="/SaleEnvelope/SaleList"> <SaleList> <xsl:for-each select="Invoice"> <xsl:if test="not(contains(Surname,'Shop'))"> <Statement> <xsl:attribute name="Type">Monthly</xsl:attribute> <xsl:attribute name="Style">Full</xsl:attribute> <Customer> <Initials> <xsl:for-each select="Initial"> <xsl:value-of select="."/> </xsl:for-each> </Initials> <Name><xsl:value-of select="Surname"/></Name> <Balance><xsl:value-of select="Balance"/></Balance> </Customer> <Purchases> <xsl:for-each select="Item"> <Article> <Desc><xsl:value-of select="Description"/></Desc> <Cost><xsl:value-of select='format-number((number(Price)*1.6),"####.##")'/></Cost> <Qty><xsl:value-of select="Quantity"/></Qty> </Article> </xsl:for-each> </Purchases> <Amount> <xsl:attribute name="Currency"> <xsl:value-of select="Currency" /> </xsl:attribute> <xsl:call-template name="sumSales"> <xsl:with-param name="list" select="Item"/> </xsl:call-template> </Amount> </Statement> </xsl:if> </xsl:for-each> </SaleList> </xsl:for-each> </SaleEnvelope> </xsl:template> <xsl:template name="sumSales"> <xsl:param name="list" /> <xsl:param name="result" select="0"/> <xsl:choose> <xsl:when test="$list"> <xsl:call-template name="sumSales"> <xsl:with-param name="list" select="$list[position()!=1]"/> <xsl:with-param name="result" select="$result + number($list[1]/Price)*number($list[1]/Quantity)*1.6"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select='format-number(number($result),"####.##")'/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
Esto se utiliza para transformar el siguiente mensaje XML (suministrado en el ejemplo como XMLT_sample_msg.enqueue) :
<SaleEnvelope> <Header> <SaleListCount>1</SaleListCount> </Header> <SaleList> <Invoice> <Initial>K</Initial> <Initial>A</Initial> <Surname>Braithwaite</Surname> <Item> <Code>00</Code> <Code>01</Code> <Code>02</Code> <Description>Twister</Description> <Category>Games</Category> <Price>00.30</Price> <Quantity>01</Quantity> </Item> <Item> <Code>02</Code> <Code>03</Code> <Code>01</Code> <Description>The Times Newspaper</Description> <Category>Books and Media</Category> <Price>00.20</Price> <Quantity>01</Quantity> </Item> <Balance>00.50</Balance> <Currency>Sterling</Currency> </Invoice> <Invoice> <Initial>T</Initial> <Initial>J</Initial> <Surname>Dunnwin</Surname> <Item> <Code>04</Code> <Code>05</Code> <Code>01</Code> <Description>The Origin of Species</Description> <Category>Books and Media</Category> <Price>22.34</Price> <Quantity>02</Quantity> </Item> <Item> <Code>06</Code> <Code>07</Code> <Code>01</Code> <Description>Microscope</Description> <Category>Miscellaneous</Category> <Price>36.20</Price> <Quantity>01</Quantity> </Item> <Balance>81.84</Balance> <Currency>Euros</Currency> </Invoice> </SaleList> <Trailer> <CompletionTime>12.00.00</CompletionTime> </Trailer> </SaleEnvelope>
El siguiente diagrama muestra el flujo de mensajes usado para transformar el mensaje XML. Si desea ver información más detallada, abra XMLT_Sample_flow.msgflow en el Editor de flujos de mensajes y examine cada nodo.