Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 618603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:33:51+00:00 2026-05-13T18:33:51+00:00

I’m transforming XML to HTML. In part of the XML, I need to count

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:33:52+00:00Added an answer on May 13, 2026 at 6:33 pm

    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">
        <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.