Search code examples
xsltxslt-2.0

Handling namespaces in the XML when using XSLT


I have the following XML with name, say, "screen.xml":

    <tc:transform xmlns:tc="http://www.w2.prg/1999/XSL/Transform" version="1.0">
<tc:blockTemplate blockTitle="Court/Agency">
<link flush="true" href="./customimages/customBlockStylesheet.css" rel="stylesheet" type="text/css"/>
    <!-- BEGIN Court Information Content -->
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <!-- Control Row -->
    <tr>
    <td class="altFieldColor" height="0" width="20%"/>
    <td class="altFieldColor" height="0" width="10"/>
    <td class="altFieldColor" height="0" width="25%"/>
    <td class="altFieldColor" height="0" width="2"/>
    <td class="altFieldColor" height="0" width="10"/>
    <td class="altFieldColor" height="0" width="20%"/>
    <td class="altFieldColor" height="0" width="10"/>
    <td class="altFieldColor" height="0" width="100%"/>
    </tr>
    <tr>
    <td colspan="8">
     <img border="0" height="2" src="./customimages/spacer.gif" width="1"/>
    </td>
    </tr>
    <tr>
    <td class="altFieldColor" height="40">
     <tc:label category="DISP" name="CoreDocketNumber"/>:</td>
    <td class="altFieldColor" width="10">
     <img border="0" height="1" src="./customimages/spacer.gif" width="10"/>
    </td>
    <td class="altFieldColor" nowrap="true">
     **<tc:field category="DISP" name="CoreDocketNumber"/>**
    </td>
    <td width="2">
     <img border="0" height="1" src="./customimages/spacer.gif" width="2"/>
    </td>
    <td class="altFieldColor" width="10">
     <img border="0" height="1" src="./customimages/spacer.gif" width="10"/>
    </td>
    <td class="altFieldColor" height="40">
     <tc:label category="DISP" name="CoreCourt"/>:</td>
    <td class="altFieldColor" width="10">
     <img border="0" height="1" src="./customimages/spacer.gif" width="10"/>
    </td>
    <td class="altFieldColor" nowrap="true">
     **<tc:field category="DISP" name="CoreCourt"/>**
    </td>
    </tr>   
        </table>
    <!-- END Court Information Content -->
</tc:blockTemplate>
</tc:transform>

I am trying to write an XSL that will mostly ignore the html tags and only look at the tags with name "tc:field" and give me the following:

<H1>screen.xml</H1>
<ul>
<li>DISP.CoreDocketNumber</li>
<li>DISP.CoreCourt</li>
</ul>

The DISP is the value of the category attribute of the tc:field tag and CoreDocketNumber is the value of the name attribute.

Here's my attempt at the XSL

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tc="http://www/w2.prg/1999/XSL/Tansform" xmlns="http://www.w3.org/1999/xhtml">
    <xsl:output method="html" indent="yes"/>
      <xsl:template match="@*|node()">
      <ul>
            <xsl:apply-templates select="tc:field"/>
            </ul>
      </xsl:template>
      
      <xsl:template match="tc:field">
      <li><xsl:value-of select="@category"/>.<xsl:value-of select="@name"/></li>
      </xsl:template>
</xsl:stylesheet>

I only get the following output when I apply the xsl to the xml:

<ul xmlns:tc="http://www/w2.prg/1999/XSL/Tansform" xmlns="http://www.w3.org/1999/xhtml"></ul>

I am not sure I am handling the tc namespace correctly. Any guidance would be greatly appreciated.


Solution

  • Try something like:

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tc="http://www.w2.prg/1999/XSL/Transform"
    exclude-result-prefixes="tc">
    
    <xsl:template match="/">
        <html>
            <body>
                <H1>screen.xml</H1>
                <ul>
                    <xsl:apply-templates select="//tc:field"/>
                </ul>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="tc:field">
        <li>
            <xsl:value-of select="@category"/>
            <xsl:text>.</xsl:text>
            <xsl:value-of select="@name"/>
        </li>
    </xsl:template>
    
    </xsl:stylesheet>