xsl-list
[Top] [All Lists]

Re: [xsl] How Can I Reference previous XML in Subsequent Iterations?

2007-04-18 09:14:03
Hi Chris,

Here's a stylesheet that does what you require (just run it against any conformant processor, even a browser, and you will get exactly the result you specified).

It only utilizes xsl:apply-template and matching templates (note that the order is completely irrelevant). No need for any variable or any xsl:for-each. It is quite straightforward, I think. Just specify what you want to do when the processor encounters a certain node as input. Of most interest to you are, I think, the lowest level of matches, where you simply say (in procedural wording) "if you find an attribute of @ss:FontName, create a text node with 'font-family' and the name of the @ss:FontName"

Please review it closely, I believe it should give you some insights in how powerful and easy XSLT can be with certain (seemingly rather complex) tasks (if you need professional services, don't hesitate! ;).

Cheers,
-- Abel Braaksma


<xsl:stylesheet
   xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"
   exclude-result-prefixes="ss xsl">

   <xsl:output indent="yes" />

   <xsl:template match="/">
       <xsl:apply-templates select="ss:Workbook/ss:Worksheet" />
   </xsl:template>

   <xsl:template match="ss:Row">
       <tr>
           <xsl:apply-templates select="*" />
       </tr>
   </xsl:template>

   <xsl:template match="ss:Worksheet">
       <table>
           <xsl:apply-templates select="*"/>
       </table>
   </xsl:template>

   <xsl:template match="ss:Cell">
       <td>
           <xsl:attribute name="style">
<xsl:apply-templates select="/ss:Workbook/ss:Styles/ss:Style[(_at_)ss:ID = current()/@ss:StyleID]" />
           </xsl:attribute>
       </td>
   </xsl:template>

   <xsl:template match="ss:Style">
       <xsl:apply-templates select="*"/>
   </xsl:template>

   <xsl:template match="ss:Alignment">
       <!-- select attributes -->
       <xsl:apply-templates select="@*" />
   </xsl:template>

   <xsl:template match="@ss:Vertical">
       <xsl:text>;vertical-align:</xsl:text>
       <xsl:value-of select="."/>
   </xsl:template>

   <xsl:template match="ss:Font">
       <!-- select attributes -->
       <xsl:apply-templates select="@*" />
   </xsl:template>

   <xsl:template match="@ss:FontName">
       <xsl:text>;font-family:</xsl:text>
<xsl:value-of select="."/> </xsl:template>

   <xsl:template match="@ss:Color">
       <xsl:text>;color:</xsl:text>
<xsl:value-of select="."/> </xsl:template>

   <xsl:template match="@ss:Underline">
       <xsl:text>;text-decoration:</xsl:text>
       <xsl:choose>
           <xsl:when test=". = 'Single'">underline</xsl:when>
           <xsl:otherwise>none</xsl:otherwise>
       </xsl:choose>
   </xsl:template>

</xsl:stylesheet>






Chris M. wrote:
Here's a bit of an example of what I'm talking about. The original XML is several thousand lines long:

<?xml version="1.0"?>
<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:html="http://www.w3.org/TR/REC-html40";
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <Styles>
        <Style ss:ID="s30" ss:Name="Normal">
            <Alignment ss:Vertical="Bottom"/>
            <Borders/>
            <Font ss:FontName="Verdana"/>
            <Interior/>
            <NumberFormat/>
            <Protection/>
        </Style>
        <Style ss:ID="s33" ss:Name="Hyperlink">
<Font ss:FontName="Verdana" ss:Color="#0000D4" ss:Underline="Single"/>
        </Style>
    </Styles>
    <Worksheet ss:Name="Sheet1">
        <Row>
<Cell ss:Index="5" ss:StyleID="s30"><Data ss:Type="String">Some String</Data></Cell> <Cell ss:Index="6" ss:StyleID="s30"><Data ss:Type="String">Some String 2</Data></Cell> <Cell ss:Index="7" ss:StyleID="s33"><Data ss:Type="String">http://example.com</Data></Cell>
        </Row>
    </Worksheet>
</Workbook>

I'd like to convert this to:

<table>
    <tr>
<td style="vertical-align:bottom;font-family:Verdana">Some String</td> <td style="vertical-align:bottom;font-family:Verdana">Some String 2</td> <td style="color:#0000D4;text-decoration:underline;font-family:Verdana"><a href="http://example.com";>http://example.com</a></td>
    </tr>
</table>

Chris Marshall
chris at getridofthiswholelongthingybecauseitstoconfusescrapers littlegreenviper dot com
http://www.cmarshall.net/




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

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