XML to XML transformation using XSL

xml Romania
  • 12 years ago

    Hi All,

    I am writing an XSL to transform the XML to XML.Only change i need in xml is to add the value for the price element
    under book tag for which publisher location is "World".
    The value for the price tag should be the sum of the value of price tag of two book tags (location for one is US and location of other is India).

    The issue is with the declaration of variable The variable is not getting accessible in the clause where we are looking for the sum.

    price(world) = price(US) + price(India) ? this is right

    <xsl:if test="(publisher/location = 'World')">
    <xsl:if test="(publisher/identity[@id] = '000')">
    <price><xsl:value-of select='$A1SoloTotal' + '$A2SoloTotal' /></price>
    </xsl:if>
    </xsl:if>

    For first price Constraint is that the location should be US and id=222 .
    For second price Constraint is that the location should be India and id=111.

    Those two should go as the sum for the book tag having location World and id="000"
    I hope it clears the issue?

    The first issue is getting access to variable declared in different if clauses.


    The other is how to put this as the sum for an element with blank text value? 


    here is the snippets from the code:

    book.xml
    ---------------------------------------------------

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="test.xsl"?>
    <booklist>
    <book>
    <publisher>
    <location>India</location>
    <identity id="111">book.com</identity>
    </publisher>
    <title>bookname1</title>
    <price>80</price>

    </book>



    <book>
    <publisher>
    <location>US</location>
    <identity id="222">usbook.com</identity>
    </publisher>
    <title>bookname2</title>
    <price>60</price>

    </book>

    <book>
    <publisher>
    <location>UK</location>
    <identity id="333">ukbook.com</identity>
    </publisher>
    <title>bookname3</title>
    <price>50</price>

    </book>

    <book>
    <publisher>
    <location>Singapore</location>
    <identity id="444">sgbook.com</identity>
    </publisher>
    <title>bookname4</title>
    <price>50</price>

    </book>


    <book>
    <publisher>
    <location>World</location>
    <identity id="000">worldbook.com</identity>
    </publisher>
    <title>bookname5</title>
    <price></price>

    </book>
    </booklist>



    ---------------------------------------------------

    ----------------book.xsl--------------------------

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

    <xsl:template match="/ | @* | node()">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="bookList/book" >


    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>

    <xsl:if test="(publisher/location = 'India')">
    <xsl:if test="(publisher/identity[@id] = '111')">
    <xsl:variable name="A1SoloTotal"><xsl:value-of select="//booklist/book/price"/></xsl:variable>
    </xsl:if>
    </xsl:if>

    <xsl:if test="(publisher/location = 'US')">
    <xsl:if test="(publisher/identity[@id] = '222')">
    <xsl:variable name="A2SoloTotal"><xsl:value-of select="//booklist/book/price"/></xsl:variable>
    </xsl:if>
    </xsl:if>

    <xsl:if test="(publisher/location = 'World')">
    <xsl:if test="(publisher/identity[@id] = '000')">
    <price><xsl:value-of select='$A1SoloTotal'+'$A2SoloTotal'/></price>
    </xsl:if>
    </xsl:if>

    </xsl:copy>

    </xsl:template>

    </xsl:stylesheet>

     


     

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The question of whether computers can think is just like the question of whether submarines can swim.” - Edsger W. Dijkstra