Re: [xsl] Convert XML to Excel using XSLT Question II.

Subject: Re: [xsl] Convert XML to Excel using XSLT Question II.
From: "Karen Yang" <kyang94@xxxxxxxxx>
Date: Tue, 20 Jun 2006 15:42:32 -0400
Thanks for your reply. Yeah, I think I need some basic information,
since the task is kind of timing, I was trying to grasp it as other
programming languages. But I think I've put myself in to a infinite
loop, since I still can't understand the apply-template.

As you said, if the XSLT will go through each node and find the match,
and apply-template when no "select =" would apply to the current
node's children, then in my following example, if I comment out the
<xsl:apply-template/>, I would assume other nodes other than the root
would still get worked on the rules, because I have other code
paragraphs/templates to match the other nodes. Then it doesn't seem to
be necessary to put a <xsl:apply-template> here. But when I commented
it out, all the rest nodes are ignored. Only <workbook ....>
<worksheet></worksheet></workbook>. Can you please explain this?

<xsl:template match="/">
 <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:o="urn:schemas-microsoft-com:office:office"
   xmlns:x="urn:schemas-microsoft-com:office:excel"
   xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:html="http://www.w3.org/TR/REC-html40";>
	<Worksheet>
	<xsl:attribute name="ss:Name">
	<xsl:value-of select="name(/*/*/*)"/>
	</xsl:attribute>
   <Table x:FullColumns="1" x:FullRows="1">
	<xsl:apply-templates/>
   </Table>
	</Worksheet>
	</Workbook>
</xsl:template>


On 6/20/06, CyberSpace Industries 2000 Inc. <csi2000@xxxxxxxxxxxxxxx> wrote:
I think it is important to have clearly in your mind what an XSLT processor
is doing.  Basically it take your XML document and creates an in-memory tree
of nodes.  Then, it processes this tree in document order.

When it encounters a node (and incidentally - everything in your XML
document - element, text, etc will create a node)
it will check to see if you provided a template for that node.  e.g.
<xsl:template match="x">... </xsl:template>

If so it will trigger that template - if not the default built in templates
will trigger.

Inside the triggered template is what you want to have happen whenever the
processor processes that node.

One thing you might want to do is to continue processing all children of
that node.  In that case you would have
<xsl:apply-templates/>.  For example you may be transforming XML to XHTML
and have encountered a node named List which you want to transform into an
XHTML unordered list <ul>... </ul> ... with the list values coming from
lower level child nodes..  You could have something like.
 <xsl:template match="List">
    <ul>
        <xsl:apply-templates/>  <== the results of processing the children
of List will be placed between the "ul" tags
    </ul>
 </xsl:template>

You may only want to do something with the content of the current node in
which case you would use
<xsl:value-of select ="."/>. ... something like your cell below.

It all depends on how you want to transform the input. Hope this helps...

Cheers...Hugh
CyberSpace Industries 2000 Inc.
XML Training and Consulting

----- Original Message -----
From: "Karen Yang" <kyang94@xxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Tuesday, June 20, 2006 11:35 AM
Subject: Re: [xsl] Convert XML to Excel using XSLT Question II.


> Thank you for your reply, Michael. I'm still a bit confused about the > "apply-templates". Now I think <xsl:template match="blah"> means the > template will apply to "blah" tag in XML, i.e. when the XML is reading > "blah" tag out, it'll find the template and run it, right? If so, why > we still need to trigger the "apply-template"? Also, in some cases, > the "apply-template" is not used, for the example, in below code, then > what does this part do? > > <xsl:template match="/*/*/*/*"> > <Cell><Data ss:Type="String"> > <xsl:value-of select="."/> > </Data></Cell> > </xsl:template> > > Also, just by calling <xsl:apply-tmeplate/>, will this apply to the > current tag or what would this apply against? > > Thanks again for your answer, > > Karen. > On 6/19/06, Michael Kay <mike@xxxxxxxxxxxx> wrote: >> > In the code I attached, in line 22, if I put >> > <xsl:apply-templates select = "/"> then a infinite loop will >> > occur. How does it come a loop? I couldn't understand. >> >> apply-templates selects a node, in this case the root node, and looks for >> a >> template rule matching that node, typically the one that specifies >> match="/". If this template rule includes the instruction apply-templates >> select="/" then the same thing happens again, and again.... >> >> >> Also, >> > the result Excel/XML file have the output of "<Cell >> > xmlns="urn:schemas-microsoft-com:office:spreadsheet"><Data >> > ss:Type="String" >> > xmlns="urn:schemas-microsoft-com:office:spreadsheet">0</Data></Cell>", >> > I don't understand how come the cell element has the xmlns attribute? >> > How did this happen? >> >> Your stylesheet has a default namespace >> xmlns="urn:schemas-microsoft-com:office:spreadsheet". So the <Cell> >> element >> in the stylesheet is really >> <{urn:schemas-microsoft-com:office:spreadsheet}Cell>, because an >> unprefixed >> element name is qualified by the default namespace. The effect of a >> literal >> result element like <Cell> is to create an element in the result document >> with the same name, that is >> <{urn:schemas-microsoft-com:office:spreadsheet}Cell>. But of course this >> isn't real XML syntax, so when it gets output as XML it becomes <Cell >> xmlns="urn:schemas-microsoft-com:office:spreadsheet">. >> >> Remember that your stylesheet is responsible for creating elements in the >> right namespace; the serializer takes care of generating the namespace >> declarations to achieve this. >> >> Michael Kay >> http://www.saxonica.com/ >> >> > >> > Below is the xslt code and xml source code. >> > >> > Thanks again for your help. >> > >> > Karen. >> > --------- >> > <?xml version='1.0' encoding='utf-8'?> >> > >> > <xsl:stylesheet version="1.0" >> > xmlns="urn:schemas-microsoft-com:office:spreadsheet" >> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >> > xmlns:msxsl="urn:schemas-microsoft-com:xslt" >> > xmlns:user="urn:my-scripts" >> > xmlns:o="urn:schemas-microsoft-com:office:office" >> > xmlns:x="urn:schemas-microsoft-com:office:excel" >> > xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"> >> > >> > <xsl:template match="/"> >> > <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" >> > xmlns:o="urn:schemas-microsoft-com:office:office" >> > xmlns:x="urn:schemas-microsoft-com:office:excel" >> > xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >> > xmlns:html="http://www.w3.org/TR/REC-html40";> >> > <!-- <Worksheet> >> > <xsl:attribute name="ss:Name"> >> > <xsl:value-of select="name(/*/*/*)"/> >> > </xsl:attribute> --> >> > <xsl:apply-templates/> >> > <!-- </Worksheet> --> >> > </Workbook> >> > </xsl:template> >> > >> > <!-- >> > <xsl:template match="/*/*"> >> > <Worksheet> >> > <xsl:attribute name="ss:Name"> >> > <xsl:value-of select="name(/*/*)"/> >> > </xsl:attribute> >> > <xsl:apply-templates/> >> > </Worksheet> >> > </xsl:template> >> > --> >> > <!--<xsl:template match="NoteDetail"> >> > test >> > <xsl:apply-templates/> >> > </xsl:template> >> > --> >> > >> > <xsl:template match="TAX_CASH_FLOW_ANALYSIS/TaxCashFlowUnit"> >> > <Table x:FullColumns="1" x:FullRows="1"> >> > <Row> >> > <xsl:for-each select="*[position() = 1]/*"> >> > <Cell><Data ss:Type="String"> >> > <xsl:value-of select="local-name()"/> >> > </Data></Cell> >> > </xsl:for-each> >> > </Row> >> > <xsl:apply-templates/> >> > </Table> >> > </xsl:template> >> > >> > <xsl:template >> > match="TAX_CASH_FLOW_ANALYSIS/TaxCashFlowUnit/TaxCashFlowDetail"> >> > <Row> >> > <xsl:apply-templates/> >> > </Row> >> > </xsl:template> >> > >> > <xsl:template >> > match="TAX_CASH_FLOW_ANALYSIS/TaxCashFlowUnit/TaxCashFlowDetail/*"> >> > <Cell><Data ss:Type="String"> >> > <xsl:value-of select="."/> >> > </Data></Cell> >> > </xsl:template> >> > >> > >> > </xsl:stylesheet> >> > >> > -------------------- >> > xml source: >> > >> > <?xml version="1.0" encoding="utf-8"?> >> > <TAX_CASH_FLOW_ANALYSIS >> > UpdateDateTime="2006-06-13T11:26:09.0000000-04:00"> >> > <TaxCashFlowUnit> >> > <TaxCashFlowDetail> >> > <EndingAIP>786593727.21233881</EndingAIP> >> > <PV>786593727.21233881</PV> >> > <PrecapOID>0</PrecapOID> >> > <OID>0</OID> >> > <RemainingOID>-35744066.285386205</RemainingOID> >> > <TaxableIncome>0</TaxableIncome> >> > <QtrInt>0</QtrInt> >> > <QtrOID>0</QtrOID> >> > <QtrIncome>0</QtrIncome> >> > <QSI>0</QSI> >> > <NQSI>0</NQSI> >> > <Principal>0</Principal> >> > <CurrentBalance>745850848.91000021</CurrentBalance> >> > <MarketDF>0</MarketDF> >> > </TaxCashFlowDetail> >> > <TaxCashFlowDetail> >> > <EndingAIP>786593727.21233881</EndingAIP> >> > <PV>786593727.21233881</PV> >> > <PrecapOID>0</PrecapOID> >> > <OID>0</OID> >> > <RemainingOID>-35744066.285386205</RemainingOID> >> > <TaxableIncome>0</TaxableIncome> >> > <QtrInt>0</QtrInt> >> > <QtrOID>0</QtrOID> >> > <QtrIncome>0</QtrIncome> >> > <QSI>0</QSI> >> > <NQSI>0</NQSI> >> > <Principal>0</Principal> >> > <CurrentBalance>745850848.91000021</CurrentBalance> >> > <MarketDF>0</MarketDF> >> > </TaxCashFlowDetail> >> > <TaxCashFlowDetail> >> > <EndingAIP>786593727.21233881</EndingAIP> >> > <PV>786593727.21233881</PV> >> > <PrecapOID>0</PrecapOID> >> > <OID>0</OID> >> > <RemainingOID>-35744066.285386205</RemainingOID> >> > <TaxableIncome>0</TaxableIncome> >> > <QtrInt>0</QtrInt> >> > <QtrOID>0</QtrOID> >> > <QtrIncome>0</QtrIncome> >> > <QSI>0</QSI> >> > <NQSI>0</NQSI> >> > <Principal>0</Principal> >> > <CurrentBalance>745850848.91000021</CurrentBalance> >> > <MarketDF>0</MarketDF> >> > </TaxCashFlowDetail> >> > </TaxCashFlowUnit> >> > </TAX_CASH_FLOW_ANALYSIS>

Current Thread