O.O.
O.O.

Reputation: 2013

Extracting Values after matching Attribute Values in XSL

I have the following XML File:

<?xml version="1.0" encoding="UTF-8"?>
 <response>
<result name="response" numFound="10000" start="0">
    <doc>
        <str name="Title">Title 1</str>
        <str name="GUID">IMG_1</str>
        <str name="Desc">Desc 1</str>
    </doc>
    <doc>
        <str name="Title">Title 2</str>
        <str name="GUID">IMG_2</str>
        <str name="Desc">Desc 2</str>
    </doc>
</result>
</response>

I would like to transform it into the following HTML file:

<html>
<head> </head>
<body>
    <table border="1">
        <tr>
            <td colspan="2">
                {Title}
            </td>
        </tr>
        <tr>
            <td>
                <img src="http://myserver/images/{GUID}">
            </td>
            <td>
                {Desc}
            </td>
        </tr>
        <tr>
            <td colspan="2">
                {Title}
            </td>
        </tr>
        <tr>
            <td>
                <img src="http://myserver/images/{GUID}">
            </td>
            <td>
                {Desc}
            </td>
        </tr>
    </table>
</body>
  </html>

Where the content in {} should be replaced from the XML file. I am curious how to do this using XSL. My attempt is as follows, but it comes out blank i.e. I get the Table structure, but no Titles or Descriptions.

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
<xsl:output media-type="text/html; charset=UTF-8" encoding="UTF-8"/>

<xsl:template match='/'>
    <html>
        <head> </head>
        <body>
            <table border="1">
                <xsl:apply-templates select="response/result/doc"/>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="doc">
    <xsl:variable name="title" select="doc/str[@name='Title']"/>
    <xsl:variable name="guid" select="doc/str[@name='GUID']"/>
    <xsl:variable name="desc" select="doc/str[@name='Desc']"/>
    <tr>
        <td colspan="2">
            <xsl:value-of select="$title"/>
        </td>
    </tr>
    <tr>
        <td>
            <img src="http://myserver/images/{GUID}" />
        </td>
        <td>
            <xsl:value-of select="$desc"/>
        </td>
    </tr>
</xsl:template>
 </xsl:stylesheet>

I'd be grateful for any help as I am new to this.

Upvotes: 1

Views: 192

Answers (2)

Avner Solomon
Avner Solomon

Reputation: 1506

You are almost there you just added an extra "doc/"

since you are using

<xsl:template match="doc">

everything is relative to doc so you should only

<xsl:variable name="title" select="str[@name='Title']"/>
<xsl:variable name="guid" select="str[@name='GUID']"/>
<xsl:variable name="desc" select="str[@name='Desc']"/>

Upvotes: 1

hr_117
hr_117

Reputation: 9627

There are only some small errors in your xlst. Because the current node for select for the variables in template doc is the doc element you have to remove the doc/.

<xsl:variable name="title" select="str[@name='Title']"/>
<xsl:variable name="guid" select="str[@name='GUID']"/>
<xsl:variable name="desc" select="str[@name='Desc']"/>

To add the variable value to the link use:

<img src="http://myserver/images/{$guid}" />

Therefore try:

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
    <xsl:output media-type="text/html; charset=UTF-8" encoding="UTF-8"/>

    <xsl:template match='/'>
        <html>
            <head> </head>
            <body>
                <table border="1">
                    <xsl:apply-templates select="response/result/doc"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="doc">
        <xsl:variable name="title" select="str[@name='Title']"/>
        <xsl:variable name="guid" select="str[@name='GUID']"/>
        <xsl:variable name="desc" select="str[@name='Desc']"/>
        <tr>
            <td colspan="2">
                <xsl:value-of select="$title"/>
            </td>
        </tr>
        <tr>
            <td>
                <img src="http://myserver/images/{$guid}" />
            </td>
            <td>
                <xsl:value-of select="$desc"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions