xsl-list
[Top] [All Lists]

RE: generating xml from xml

2003-11-10 12:27:12
Sure, "Trade/*" is just a guess at an appropriate match pattern based on 
the input sample you sent. You can match the elements any way that works 
for your data. (You only need a way to distinguish between elements that 
you want handled like this, and other elements.)

 Thanks for the solution. But I am not really good in this xsl. Solution to 
this problem will be helpful.

Here is my current xsl that works fine for me. 

<xsl:stylesheet version="1.0" xmlns="" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:template match="/">
                <xsl:apply-templates select="*"/>
        </xsl:template>
        <xsl:template match="*">
                <xsl:copy>
                        <xsl:apply-templates select="@* | node()"/>
                </xsl:copy>
        </xsl:template>
        <xsl:template match="Trade">
                <Trade>
                        <xsl:for-each select="*">
                                <xsl:element 
name="{/Data/Fields/_[(_at_)id=name(current())]/@fn}">
                                        <xsl:apply-templates select="@* | 
node()"/>
                                </xsl:element>
                        </xsl:for-each>
                </Trade>
        </xsl:template>
        <xsl:template match="@* | node()">
                <xsl:copy>
                        <xsl:apply-templates select="@* | node()"/>
                </xsl:copy>
        </xsl:template>
</xsl:stylesheet>

which works great on this xml :

<Data Timestamp="2003-11-05 21:00:07">
        <Fields>
                <_ fn="Level1" id="F0"/>
                <_ fn="Level2" id="F1"/>
                <_ fn="Level3" id="F2"/>
                <_ fn="TradeDate" id="F3"/>
                <_ fn="TradeId" id="F4"/>
                <_ fn="User" id="F5"/>
        </Fields>
        <Trades>
                <Trade>
                        <F0/>
                        <F1/>
                </Trade>
                <Trade>
                        <F0/>
                        <F1>Entry</F1>
                        <F2/>
                </Trade>
                <Trade>
                        <F0>Mid</F0>
                        <F1/>
                        <F2/>
                        <F3/>
                        <F4>126</F4>
                </Trade>
        </Trades>
</Data>

But I want it to work on this kind of xml also

<Data Timestamp="2003-11-05 21:00:07">
        <Fields>
                <_ fn="Level1" id="F0"/>
                <_ fn="Level2" id="F1"/>
                <_ fn="Level3" id="F2"/>
                <_ fn="TradeDate" id="F3"/>
                <_ fn="TradeId" id="F4"/>
                <_ fn="User" id="F5"/>
        </Fields>
        <Trades>
                <Trade>
                        <F0/>
                        <F1>Entry</F1>
                        <F2/>
                </Trade>
                <Trade>
                        <F0>Mid</F0>
                        <F1/>
                        <F2/>
                        <F3/>
                        <F4>126</F4>
                </Trade>
        </Trades>
        <DelTrades>
                <DelTrade>
                        <F0/>
                        <F1/>
                </DelTrade>
        </DelTrades>
</Data>

Notice that top nodes(Trade, DelTrade) on F0, F1 ..... are not same all the 
time. And I can't guarantee their names at run time. 
Is there any way directly(not depending on its top node) finding nodes F0, F1, 
F2 .......?

Thanks,
~Mur

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>