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 7523607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:51:08+00:00 2026-05-30T02:51:08+00:00

Hey i have the foolowing xml file : <?xml version=1.0?> <accidents> <accident> <org>1</org> <com>194</com>

  • 0

Hey i have the foolowing xml file :

<?xml version="1.0"?>
 <accidents>
 <accident>
  <org>1</org>
  <com>194</com>
  <dep>010</dep>
  <grav>0.64</grav>
 </accident>
 <accident>
  <org>1</org>
  <com>194</com>
  <dep>420</dep>
  <grav>0.54</grav>
 </accident>
 <accident>
  <org>1</org>
  <com>44</com>
  <dep>010</dep>
  <grav>0.4</grav>
 </accident>
</accidents>

And i want to apply an xslt 1.0 to have the number of accidents by dep :
the out put should be like this :

dep 010 : 2 accidents ; dep 420 : 1 accidents

thanks ,note i use php so the saxon cant be used

  • 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-30T02:51:10+00:00Added an answer on May 30, 2026 at 2:51 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kAccByDept" match="accident" use="dep"/>
    
     <xsl:template match=
      "accident
        [generate-id()
        =
         generate-id(key('kAccByDept', dep)[1])
         ]">
         <xsl:if test="position() > 1"> ; </xsl:if>
    
         <xsl:value-of select=
          "concat('dep ', dep, ' : ',
                  count(key('kAccByDept', dep)), ' accidents')
          "/>
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <accidents>
        <accident>
            <org>1</org>
            <com>194</com>
            <dep>010</dep>
            <grav>0.64</grav>
        </accident>
        <accident>
            <org>1</org>
            <com>194</com>
            <dep>420</dep>
            <grav>0.54</grav>
        </accident>
        <accident>
            <org>1</org>
            <com>44</com>
            <dep>010</dep>
            <grav>0.4</grav>
        </accident>
    </accidents>
    

    produces the wanted, correct result:

    dep 010 : 2 accidents ; dep 420 : 1 accidents
    

    Explanation:

    1. Muenchian method for grouping.

    2. Push style of XSLT processing.

    Update: The OP doesn’t have available a true XSLT processor — his tool doesn’t implement xsl:key

    In this case the following solution (note — this is much less efficient than Muenchian grouping) still solves the problem and produces the exact wanted output:

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output method="text"/>
    
         <xsl:template match=
          "accident
            [not(dep
                =
                 preceding-sibling::*/dep
                 )
            ]
          ">
             <xsl:value-of select=
              "concat('dep ', dep, ' : ',
                      count(following-sibling::*[dep = current()/dep]) +1,
                      ' accidents')
              "/>
    
             <xsl:if test=
             "following-sibling::*/dep
                  [not(. = current()/dep)
                  and
                   not(.
                =
                 ../preceding-sibling::*/dep)
                  ]"> ; </xsl:if>
         </xsl:template>
         <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the same XML document (above), the same wanted, correct result is produced:

    dep 010 : 2 accidents ; dep 420 : 1 accidents
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2.4 MB XML file, an export from Microsoft Project (hey I'm
Hey all, basically i have an empty AS3 fla file with just the following
Hey I have downloaded one of the xml editors from some site. But don't
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
Hey all i have code to write to an xml doc from asp string
Hey guys in my iptables file I have the following line: -A INPUT -m
Hey - I have the following method on my UITableView controller public void RemoveEventFromList(Event
Hey community I have the following ServerSocket which should listen to port 53000 and
Hey Guys, i have created the following Menu Structure: <div id=menu> <ul> <li><a href=#>Main
Hey all. I have a question on how to implement the following with Django.

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.