tags:

views:

124

answers:

1

I'm transforming XML to HTML. In part of the XML, I need to count elements that seem "identical" when specific descendants are compared, while other descendants and any attributes must be ignored.

Here's a simplified example of my XML. The real thing is much more complicated; I've just removed elements that aren't relevant to the counting:

<complaints>
    <lodgedComplaint>
        <defendant>First Person</defendant>
        <charge>
            <actionNumber>1</actionNumber>
            <offence>
                <offenceSection>
                    <legislation>SA1</legislation>
                    <description>Stealing</description>
                </offenceSection>
                <placeOfOffence>Sydney</placeOfOffence>
                <dateOfOffence>2010-01-01</dateOfOffence>
                <particular>value=20</particular>
            </offence>
        </charge>
        <charge>
            <actionNumber>2</actionNumber>
            <offence>
                <offenceSection>
                    <legislation>SA2</legislation>
                    <description>Theft</description>
                </offenceSection>
                <placeOfOffence>Sydney</placeOfOffence>
                <dateOfOffence>2010-01-01</dateOfOffence>
            </offence>
        </charge>
        <charge>
            <actionNumber>3</actionNumber>
            <offence>
                <offenceSection>
                    <legislation>SA2</legislation>
                    <description>Theft</description>
                </offenceSection>
                <placeOfOffence>London</placeOfOffence>
                <dateOfOffence>2010-01-01</dateOfOffence>
            </offence>
        </charge>
        <charge>
            <actionNumber>4</actionNumber>
            <offence>
                <offenceSection>
                    <legislation>SA1</legislation>
                    <description>Stealing</description>
                </offenceSection>
                <placeOfOffence>Sydney</placeOfOffence>
                <dateOfOffence>2010-01-01</dateOfOffence>
                <particular>value=50</particular>
            </offence>
        </charge>
        <charge>
            <actionNumber>5</actionNumber>
            <offence>
                <offenceSection>
                    <legislation>SA2</legislation>
                    <description>Theft</description>
                </offenceSection>
                <placeOfOffence>Sydney</placeOfOffence>
                <dateOfOffence>2010-01-02</dateOfOffence>
            </offence>
        </charge>
    </lodgedComplaint>
    <lodgedComplaint>
        <defendant>Second Person</defendant>
        <charge>
            <actionNumber>1</actionNumber>
            <offence>
                <offenceSection>
                    <legislation>SA1</legislation>
                    <description>Stealing</description>
                </offenceSection>
                <placeOfOffence>Sydney</placeOfOffence>
                <dateOfOffence>2010-01-01</dateOfOffence>
                <particular>value=35</particular>
            </offence>
        </charge>
    </lodgedComplaint>
</complaints>

In each lodgedComplaint, any two charges should be considered identical if their legislation, description, placeOfOffence and dateOfOffence elements match. The actionNumber and particular elements must be ignored (the particular element is optional and unbounded in the schema I've been given).

The desired output should look something like this:

First Person
    2 counts of Stealing at Sydney on 1/1/2010 under SA1
    1 count of Theft at Sydney on 1/1/2010 under SA2
    1 count of Theft at London on 1/1/2010 under SA2
    1 count of Theft at Sydney on 2/1/2010 under SA2
Second Person
    1 count of Stealing at Sydney on 1/1/2010 under SA1

Here's an XSLT I tried, based on things I've read on Stack Overflow and elsewhere. It doesn't work; the charge details don't appear at all. I think my use of concat() causes problems. How can I do this?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>
    <xsl:key name="matchOffence" match="offence" use="concat(offenceSection/legislation,offenceSection/description,placeOfOffence,dateOfOffence)"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>CRIMES Listings</title>
                <link rel="stylesheet" type="text/css" href="global_styles.css"/>
                <link rel="stylesheet" type="text/css" href="styles.css"/>
            </head>
            <body>
                <xsl:apply-templates select="complaints/lodgedComplaint"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="lodgedComplaint">
        <br/>
        <xsl:value-of select="defendant"/>
        <xsl:for-each select="charge/offence[generate-id(concat(offenceSection/legislation,offenceSection/description,placeOfOffence,dateOfOffence))=generate-id(key('matchOffence',.))]">
            <br/>
            <xsl:value-of select="count(../../charge/offence[(offenceSection/legislation=current()/offenceSection/legislation) and (offenceSection/description=current()/offenceSection/description) and (placeOfOffence=current()/placeOfOffence) and (dateOfOffence=current()/dateOfOffence)])"/>
            counts of <b><xsl:value-of select="offenceSection/description"/></b>
            at <b><xsl:value-of select="placeOfOffence"/></b>
            on <b><xsl:call-template name="date">
                <xsl:with-param name="text" select="dateOfOffence"/>
            </xsl:call-template></b>
            under <b><xsl:value-of select="offenceSection/legislation"/></b>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="date">
        <xsl:param name="text" select="."/>
        <xsl:choose>
            <xsl:when test="contains($text,'-')">
                <xsl:call-template name="date">
                    <xsl:with-param name="text" select="substring-after($text,'-')"/>
                </xsl:call-template>/<xsl:value-of select="substring-before($text,'-')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
+1  A: 

There are two problems with your stylesheet.

  1. You are trying to generate-id() using the results of the concat(), which produces a string result. generate-id() only works for nodes.

    It should be re-written as: <xsl:for-each select="charge/offence[generate-id(.)=generate-id(key('matchOffence',concat(offenceSection/legislation,./offenceSection/description,placeOfOffence,dateOfOffence)))]">

  2. The other problem is that the string value used for the key is not unique. The offence for the second person has the same values as one of the offense for the first person. This prevents you from generating output for the second person. You can make the key more unique by adding in the value of the defendant element in your key.

    After adjusting the key, then you would obviously need to adjust the for-each.

Putting it all together in your stylesheet:

<?xml version="1.0" encoding="UTF-8"?>    
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>
    <xsl:key name="matchOffence" match="offence" use="concat(../../defendant,offenceSection/legislation,offenceSection/description,placeOfOffence,dateOfOffence)"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>CRIMES Listings</title>
                <link rel="stylesheet" type="text/css" href="global_styles.css"/>
                <link rel="stylesheet" type="text/css" href="styles.css"/>
            </head>
            <body>
                <xsl:apply-templates select="complaints/lodgedComplaint"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="lodgedComplaint">
        <br/>
        <xsl:value-of select="defendant"/>
        <xsl:for-each select="charge/offence[generate-id(.)=generate-id(key('matchOffence',concat(../../defendant,offenceSection/legislation,./offenceSection/description,placeOfOffence,dateOfOffence)))]">

          <br/>
            <xsl:value-of select="count(../../charge/offence[(offenceSection/legislation=current()/offenceSection/legislation) and (offenceSection/description=current()/offenceSection/description) and (placeOfOffence=current()/placeOfOffence) and (dateOfOffence=current()/dateOfOffence)])"/>
            counts of <b><xsl:value-of select="offenceSection/description"/></b>
            at <b><xsl:value-of select="placeOfOffence"/></b>
            on <b><xsl:call-template name="date">
                <xsl:with-param name="text" select="dateOfOffence"/>
            </xsl:call-template></b>
            under <b><xsl:value-of select="offenceSection/legislation"/></b>

        </xsl:for-each>
    </xsl:template>

    <xsl:template name="date">
        <xsl:param name="text" select="."/>
        <xsl:choose>
            <xsl:when test="contains($text,'-')">
                <xsl:call-template name="date">
                    <xsl:with-param name="text" select="substring-after($text,'-')"/>
                </xsl:call-template>/<xsl:value-of select="substring-before($text,'-')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
Mads Hansen
Thanks - works perfectly.
Scott Leis