Matthew
Matthew

Reputation: 957

XSL 101 - How to generate a basic XSL style sheet for a given XML document?

Below is a snippet of what you get when you save the configuration of IIS 6/Windows Server 2003 web sites to a file:

<?xml version ="1.0"?>
<configuration xmlns="urn:microsoft-catalog:XML_Metabase_V64_0">
<MBProperty>
<IIsWebServer   Location ="/LM/W3SVC/1"
        AppPoolId="DefaultAppPool"
        DefaultDoc="Default.htm,Default.asp,index.htm,iisstart.htm,Default.aspx"
    ServerAutoStart="FALSE"
    ServerBindings=":80:"
    ServerComment="Default Web Site"
    ServerSize="1"
>
</IIsWebServer>
<IIsWebServer   Location ="/LM/W3SVC/1020944513"
    AuthFlags="0"
    LogPluginClsid="{FF160663-DE82-11CF-BC0A-00AA006111E0}"
    ServerAutoStart="TRUE"
    ServerBindings="161.230.8.183:80:"
    ServerComment="staging.magazinefundraising.com"
>
</IIsWebServer>
<IIsWebServer   Location ="/LM/W3SVC/103632312"
    AuthFlags="0"
    LogPluginClsid="{FF160663-DE82-11CF-BC0A-00AA006111E0}"
    ServerAutoStart="TRUE"
    ServerBindings="161.230.9.48:80:"
    ServerComment="QSP2.Downtime"
>
</IIsWebServer>
</MBProperty>
</configuration>

As you can see, there are multiple <IIsWebServer> sections between the <MBProperty> and </MBProperty> elements.

I'd like to create a simple xsl stylesheet which outputs a multi-column (tabular) result containing the following attributes for each IIsWebServer instance:

ServerComment
ServerBindings
AuthFlags

So the result might look something like:

ServerComment     ServerBindings     AuthFlags  
-------------     --------------     ---------  
QSP2.Downtime     161.230.9.48:80:   0  

and so on...

Thanks!

Upvotes: 2

Views: 4412

Answers (2)

cmptrgeekken
cmptrgeekken

Reputation: 8092

As Emil said, XSL isn't exactly made for text formatting. However, you should be able to achieve your goal if you output HTML. Something like the following:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
<table>
    <tr>
        <th>ServerComment</th>
        <th>ServerBindings</th>
        <th>AuthFlags</th>
    </tr>
        <xsl:for-each select="/configuration/MBProperty/IIsWebServer">
    <tr>
        <td><xsl:value-of select="@ServerComment" /></td>
        <td><xsl:value-of select="@ServerBindings" /></td>
        <td><xsl:value-of select="@AuthFlags" /></td>
    </tr>
        </xsl:for-each>
</table>
    </xsl:template>
</xsl:stylesheet>

If you're going for developing a tabular display for viewing purposes only, this may be your best bet. However, if you're planning on using the data in another way, it may be better to go with Marc or Emil's approach.

Upvotes: -1

marc_s
marc_s

Reputation: 755073

XSL could be something like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:mb="urn:microsoft-catalog:XML_Metabase_V64_0"
    exclude-result-prefixes="msxsl">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="/">
      ServerComment&#160;&#160;&#160;ServerBindings&#160;&#160;&#160;AuthFlags

      <xsl:apply-templates />
    </xsl:template>

  <xsl:template match="mb:IIsWebServer">

    <xsl:value-of select="./@ServerComment" />
    <xsl:text>&#160;</xsl:text>
    <xsl:value-of select="./@ServerBindings" />
    <xsl:text>&#160;</xsl:text>
    <xsl:value-of select="./@AuthFlags" />

  </xsl:template>
</xsl:stylesheet>

OF course, you'll want to prettify it :-) but it basically works.

Whether it's the right choice and best technology to use for this, is totally up to you :-) But it can be done - no problem.

Marc

Upvotes: 3

Related Questions