Friday, July 31, 2009

XML & XSL Advance Example

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="GeneralReport/Carlines/Carline/Prices"/>
<xsl:apply-templates select="GeneralReport/Prices"/>
</xsl:template>

<xsl:template match="Prices">
<xsl:apply-templates select="LocalReport"/>
<xsl:apply-templates select="DomesticOptionPrice"/>
<xsl:apply-templates select="ExportModelPrice"/>
<xsl:apply-templates select="ExportOptionPrice"/>
</xsl:template>

<xsl:template match="DomesticOptionPrice">
<xsl:for-each select="Models/Model">
<xsl:apply-templates select="Code"/>
<xsl:apply-templates select="../../Code"/>
<xsl:apply-templates select="../../ORS"/>
<xsl:apply-templates select="Name"/>
<xsl:apply-templates select="../../Name"/>
<xsl:apply-templates select="../../MSRP"/>
<xsl:apply-templates select="../../MSRPNet"/>
<xsl:apply-templates select="../../DealerNet"/>
<xsl:apply-templates select="../../Taxes"/>
<xsl:apply-templates select="../../EffectiveDate"/>
<xsl:apply-templates select="../../MY"/>
<xsl:apply-templates select="../../MYS"/>
<xsl:apply-templates select="../../Carline"/>
<xsl:apply-templates select="../../DealerIndex"/>
<xsl:apply-templates select="../../Currency"/>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:for-each>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:template>

<xsl:template match="ExportOptionPrice">
<xsl:for-each select="Models/Model">
<xsl:apply-templates select="Code"/>
<xsl:apply-templates select="../../Code"/>
<xsl:apply-templates select="../../ORS"/>
<xsl:apply-templates select="Name"/>
<xsl:apply-templates select="../../Name"/>
<xsl:apply-templates select="../../PlantPrice"/>
<xsl:text>+000000000</xsl:text>
<xsl:text>+000000000</xsl:text>
<xsl:apply-templates select="../../Taxes"/>
<xsl:apply-templates select="../../EffectiveDate"/>
<xsl:apply-templates select="../../MY"/>
<xsl:apply-templates select="../../MYS"/>
<xsl:apply-templates select="../../Carline"/>
<xsl:apply-templates select="../../DealerIndex"/>
<xsl:apply-templates select="../../Currency"/>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:for-each>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:template>


<xsl:template match="LocalReport">
<xsl:apply-templates select="Models/Model/Code"/>
<!-- 10 blanks for Option Code -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="ORS"/>
<xsl:apply-templates select="Models/Model/Name"/>
<!-- 40 blanks for Option Name -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="MSRP"/>
<xsl:apply-templates select="MSRPNet"/>
<xsl:apply-templates select="DealerNet"/>
<xsl:apply-templates select="Taxes"/>
<xsl:apply-templates select="EffectiveDate"/>
<xsl:apply-templates select="MY"/>
<xsl:apply-templates select="MYS"/>
<xsl:apply-templates select="Carline"/>
<xsl:apply-templates select="DealerIndex"/>
<xsl:apply-templates select="Currency"/>
<xsl:call-template name="newline"/>
</xsl:template>

<xsl:template match="ExportModelPrice">
<xsl:apply-templates select="Models/Model/Code"/>
<!-- 10 blanks for Option Code -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="ORS"/>
<xsl:apply-templates select="Models/Model/Name"/>
<!-- 40 blanks for Option Name -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="PlantPrice"/>
<xsl:text>+000000000</xsl:text>
<xsl:text>+000000000</xsl:text>
<xsl:apply-templates select="Taxes"/>
<xsl:apply-templates select="EffectiveDate"/>
<xsl:apply-templates select="MY"/>
<xsl:apply-templates select="MYS"/>
<xsl:apply-templates select="Carline"/>
<xsl:apply-templates select="DealerIndex"/>
<xsl:apply-templates select="Currency"/>
<xsl:call-template name="newline"/>
</xsl:template>



<xsl:template name="fillBefore">
<xsl:param name="objectString"/>
<xsl:param name="numberOfChars"/>
<xsl:param name="fillCharacter"/>
<xsl:variable name="size" select="string-length($objectString)"/>
<xsl:choose>
<xsl:when test="$size < $numberOfChars">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="concat($fillCharacter, $objectString)"/></xsl:with-param>
<xsl:with-param name="numberOfChars"><xsl:value-of select="$numberOfChars"/></xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:value-of select="$fillCharacter"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="$size > $numberOfChars">
<xsl:value-of select="substring($objectString,1,$numberOfChars)"/>
<!-- todo : better solution needed here -->
<!-- <xsl:message terminate="yes">ObjectString is too long</xsl:message> -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$objectString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="fillAfter">
<xsl:param name="objectString"/>
<xsl:param name="numberOfChars"/>
<xsl:param name="fillCharacter"/>
<xsl:variable name="size" select="string-length($objectString)"/>
<xsl:choose>
<xsl:when test="$size < $numberOfChars">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="concat($objectString,$fillCharacter)"/></xsl:with-param>
<xsl:with-param name="numberOfChars"><xsl:value-of select="$numberOfChars"/></xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:value-of select="$fillCharacter"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="$size > $numberOfChars">
<xsl:value-of select="substring($objectString,1,$numberOfChars)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$objectString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<!-- leave this as it was made, it is essential for the newline -->
<xsl:template name="newline">
<xsl:text>
</xsl:text>
</xsl:template>



<xsl:template match="Code">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">10</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<xsl:template match="Model">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">10</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<xsl:template match="MSRP | MSRPNet | DealerNet | PlantPrice">
<xsl:choose>
<xsl:when test="@sign">
<xsl:variable name="price" select="."/>
<xsl:call-template name="getPrice">
<xsl:with-param name="value" select="substring-after($price,'-')"/>
</xsl:call-template>
<xsl:value-of select="@sign"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="getPrice">
<xsl:with-param name="value" select="."/>
</xsl:call-template>
<xsl:text>+</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="getPrice">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="not(contains($value,'.'))">
<xsl:text>000000000</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="before">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="substring-before($value,'.')"/></xsl:with-param>
<xsl:with-param name="numberOfChars">7</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text>0</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="after" select="substring-after($value,'.')"/>
<xsl:value-of select="concat($before, $after)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="getTax">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="not(contains($value,'.'))">
<xsl:text>000000</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="before">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="substring-before($value,'.')"/></xsl:with-param>
<xsl:with-param name="numberOfChars">3</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text>0</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<!-- BugFix #287 - eVPS-2006-03: format the Tax values to 3 digital places before the point and 3 after -->
<xsl:variable name="after_long">
<xsl:value-of select="substring-after($value,'.')"/>
</xsl:variable>
<xsl:variable name="after">
<xsl:value-of select="substring($after_long,1,3)"/>
</xsl:variable>
<xsl:value-of select="concat($before, $after)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="getSpecialTaxAmount">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="not(contains($value,'.'))">
<xsl:text>+000000000</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="minsign">
<xsl:if test="(contains($value,'-'))">
<xsl:text>-</xsl:text>
</xsl:if>
<xsl:if test="not(contains($value,'-'))">
<xsl:text>+</xsl:text>
</xsl:if>
</xsl:variable>
<xsl:variable name="removeminus">
<xsl:if test="(contains($value,'-'))">
<xsl:value-of select="substring-after($value,'-')"/>
</xsl:if>
<xsl:if test="not(contains($value,'-'))">
<xsl:value-of select="string($value)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="before">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="substring-before($removeminus,'.')"/></xsl:with-param>
<xsl:with-param name="numberOfChars">7</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text>0</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="after_long">
<xsl:value-of select="substring-after($before,'.')"/>
</xsl:variable>
<xsl:variable name="after">
<xsl:value-of select="substring($before,1,2)"/>
</xsl:variable>
<xsl:value-of select="concat($minsign,$before, $after)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="EffectiveDate">
<xsl:variable name="date" select="."/>
<xsl:value-of select="substring($date, 1, 2)"/>
<xsl:value-of select="substring($date, 4, 2)"/>
<xsl:value-of select="substring($date, 7, 4)"/>
</xsl:template>


<xsl:template match="ORS">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">25</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<!--
<xsl:template match="Name">

</xsl:template>
-->

<xsl:template match="Name">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">40</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<xsl:template match="Taxes">
<xsl:call-template name="getTax">
<xsl:with-param name="value" select="./SpecialTax"/>
</xsl:call-template>
<xsl:call-template name="getTax">
<xsl:with-param name="value" select="./Tax"/>
</xsl:call-template>

<xsl:call-template name="getSpecialTaxAmount">
<xsl:with-param name="value" select="./SpecialTaxAmount"/>
</xsl:call-template>


</xsl:template>

<xsl:template match="Carline">
<xsl:value-of select="substring(.,1,2)"/>
</xsl:template>

</xsl:stylesheet>




<!-- Stylus Studio meta-information - (c)1998-2001 eXcelon Corp.
<metaInformation>
<scenarios ><scenario default="yes" name="toExport" userelativepaths="yes" url="..\results\Kopie von sample.xml" htmlbaseurl="" processortype="internal" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext=""/></scenarios><MapperInfo srcSchemaPath="" srcSchemaRoot="" srcSchemaPathIsRelative="yes" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" />
</metaInformation>
-->


============================
XML Data
============================
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="export.xsl"?>
<GeneralReport module="Domestic">
<Meta>
<User>TZG136</User>
<CreationDate>31.07.2009</CreationDate>
<CreationTime>15:16</CreationTime>
<Comment></Comment>
<SelectionCriteria>
<LegalEntityName>Austria</LegalEntityName>
<Carline>Agila</Carline>
<MY>2</MY>
<MYS>A</MYS>
<DealerIndex>000</DealerIndex>
<Currency>EUR</Currency>
<EffectiveDate>= 14.12.2001</EffectiveDate>
<ModelPrices>true</ModelPrices>
<Model>0HF68</Model>
<Option>*</Option>
<OptionIndicator>M</OptionIndicator>
<Active>true</Active>
<ORS apply="false">*</ORS>
</SelectionCriteria>
</Meta>
<AllModels>
<Model>0HF68</Model>
<Model>0HL68</Model>
<Model>0HN68</Model>
<Model>0HP68</Model>
</AllModels>
<Models>
<Model>0HF68</Model>
</Models>
<Prices>
<LocalReport>
<LegalEntityName>Austria</LegalEntityName>
<OptionIndicator>M</OptionIndicator>
<Carline>Agila</Carline>
<Code></Code>
<Name></Name>
<Models>
<Model>
<Code>0HF68</Code>
<Name>Agila Agila 5</Name>
</Model>
</Models>
<MY>2</MY>
<MYS>A</MYS>
<EffectiveDate>14.12.2001</EffectiveDate>
<DealerIndex>000</DealerIndex>
<MSRP sign="-">-90.00</MSRP>
<MSRPNet sign="-">-182.73</MSRPNet>
<DealerNet sign="-">-182.73</DealerNet>
<TransferPrice sign="-">-183.00</TransferPrice>
<Currency>EUR</Currency>
<ORS></ORS>
<Taxes>
<Tax>0.0000</Tax>
<SpecialTax>10.0000</SpecialTax>
<SpecialTaxAmount>500.00</SpecialTaxAmount>
</Taxes>
</LocalReport>
</Prices>
</GeneralReport>

No comments: