Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

xml/xsl

Options
  • 01-05-2008 3:03pm
    #1
    Registered Users Posts: 6,560 ✭✭✭


    Not sure if this should be here or web design but here goes.

    Piece of software in work uses xml and xsl both of which I am new to.
    -<trade type ="trades">
    +<trade id="trade_13709" mefClass="mxContractITRADE" mefClassInstanceType="92.000000" type="trade">
    +<trade id="trade_13710" mefClass="mxContractITRADE" mefClassInstanceType="92.000000" type="trade">
    </trades>

    There is 2 trades then with an exchange rate for each leg I would like to extract plus some other information for some reason I can only obtain information out of the first trade

    I have tried to access the information directly as follows

    <xsl:value-of select="trade[1]/tradeBody/forexSwapLeg/exchangeRate/rate"></xsl:value-of>

    <xsl:value-of select="trade[2]/tradeBody/forexSwapLeg/exchangeRate/rate"></xsl:value-of>

    and also as
    <xsl:for-each select="trade/tradeBody/forexSwapLeg/exchangeRate">
    <xsl:value-of select="./rate"></xsl:value-of>
    </xsl:for-each>

    However regardless of my method I can only obtain the exchange rate from the first trade and not the second one. Anyone have any idea what I need to be doing?

    Regards,

    Data


Comments

  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    Without seeing the full tree, I dont know what you're trying to do. What information (attribute or node) do you want to extract?

    The Xpath that you've provided does not refer to any attribute or node within the tree that you've provided.


  • Registered Users Posts: 6,560 ✭✭✭Woden


    Cheers for the response. I'll post up some more of the tree

    -<trade type ="trades">
    - <trade id="trade_13709" mefClass="mxContractITRADE" mefClassInstanceType="92.000000" type="trade">
    - <tradeBody type="tradeBody">
    - <forexSwapLeg type="fxLeg">
    - <exchangeRate type="fxRate">
    -<rate type="decimal">9.304436925</rate>

    .... close this trade

    - <trade id="trade_13710" mefClass="mxContractITRADE" mefClassInstanceType="92.000000" type="trade">
    - <tradeBody type="tradeBody">
    - <forexSwapLeg type="fxLeg">
    - <exchangeRate type="fxRate">
    -<rate type="decimal">9.30567390748</rate>

    I'm interesting in extracting the second rate I can only seem to get the first one at the moment with the two methods I mentioned above

    Regards,

    Data


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    Assuming you are traversing from the root of the tree
    <xsl:template match="trade">
    <xsl:for-each select="trade/tradeBody/forexSwapLeg/exchangeRate/rate">
    <xsl:value-of select="."/>
    </xsl:for-each>
    </xsl:template>
    

    would seem the most logical way. There are inherent inefficiencies in naming the parent and child node the same, in this case "trade", most notably readability :D. You should consider changing this if possible.

    To extract only the second node, I would do
    <xsl:template match="trade">
    <xsl:for-each select="trade/tradeBody/forexSwapLeg/exchangeRate/rate">
    <xsl:if test="position() = 2"> 
    <xsl:value-of select="."/>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>
    
    Although I cant remember whether node arrays are 0 based, so this may be position() = 1.

    Also, position(), I've read, is quite resource intensive so you may notice a dip in performance of your application if you use this quite a bit.

    Let me know how you get on.


  • Registered Users Posts: 6,560 ✭✭✭Woden


    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:template match="trades">
    		<xsl:for-each select="trade/tradeBody/forexSwapLeg/exchangeRate/rate">
    			<xsl:if test="position() = 1">
    				<xsl:value-of select="."/>
    			</xsl:if>
    		</xsl:for-each>
    	</xsl:template>
    </xsl:stylesheet>
    

    Right have given it a whirl. Firstly it seems to matter what i put in for the template match i.e trades trade or / If i use trades I can get a large string for position 2 which at the very end is the rate value like its obtaining everything else up to that point.
    <xsl:template match="trades">
    	
    				<xsl:value-of select="trade[2]/tradeBody/forexSwapLeg/exchangeRate/rate"/>
    		
    	</xsl:template>
    
    produces the same results
    <xsl:template match="/">
    		<xsl:for-each select="trade/tradeBody/forexSwapLeg/exchangeRate/rate">
    			<xsl:if test="position() = 1">
    				<xsl:value-of select="."/>
    			</xsl:if>
    		</xsl:for-each>
    	</xsl:template>
    
    returns me the first exchange rate but when I switch the position to 2 I get nothing back once more similar to the other methods

    Cheers again!

    Data


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    If its not returning anything for position 2 and you're positive there is data there, it could be that the XML is poorly formed. Are you using a DTD or schema for forming the XML? Maybe you could post up ALL the XML (and change the names to protect the innocent) and I'll give it a go for you?

    By the way, what are you using to transform? And what processor are you using?


  • Advertisement
  • Registered Users Posts: 6,560 ✭✭✭Woden


    Cheers again for the reply.

    Yep I might be able to post up the whole xml or put it online somewhere at least from work tomorrow. Unfortunately I don't have any control over its generation I think it is done with a DTD. A piece of financial software (murex) makes the files when you save them down. The software then has a testing function where you can write formulas in xsl and such and test them on your xml code. What we do then is use these formulas in templates to generate html documents as required. Its all very new to me so I'm probably going about it a bit arse about face.

    I thought it might be something small and stupid I was doing and could potentially find out here quickly so I don't want to waste too much of your time! Particularly when the company is paying for licence/suppport of this software anyway!

    Regards,

    Data


  • Registered Users Posts: 6,560 ✭✭✭Woden


    Hey again. This kinda got put on the back burner there for a month but had reared it head again and I took a look at it today and fortunately managed to work something out.
    The software is supplied with formulas already developed by the company. The main one we use for testing is a formula which is designed to dig into the xml and go straight for the first trade id. Typically not an issue as most of the contracts only have one trade. With this one though it meant no access to the second trade. Was able to get to it then via
    <xsl:template match="/">
    		<xsl:value-of select="MxML/trades/trade[2]/tradeBody/forexSwapLeg/exchangeRate/rate"></xsl:value-of>
    	</xsl:template>
    

    Simple now that I look at it and should have realised was not going from the root :/

    Thanks again was good to read over your posts today

    Regards,

    Data


Advertisement