473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Counting the elements in xml using XSLT

Hi,

Iam working on converting one of xml file to other form of xml using XSLT.
As part of this, I need to count the no. of "component" nodes in the xml file given below.
If there is only one "component" node,i have to assign the value of this node to "component" node in my output.xml file.
If there are more than one "component" nodes in my input xml file, then i should take the first "component" alphabetically that does not start with a space.

XML File:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2.  
  3.  
  4. <!--  RSS generated by JIRA 91 at Thu Sep 11 06:23:06 EDT 2008 -->
  5.  
  6. <rss version="0.92">
  7. <channel>
  8.     <title>Sentinel Issue Tracking System</title>
  9.     <link>http://bugs.esecurity.net:8090</link>
  10.     <description>This file is an XML representation of an issue</description>
  11.     <language>en</language>
  12.     <item>
  13. <title>[SEN-8214] Add Consistent Support for putting event labels (not tags) in between %% and $$ in Correlation Actions and Menu Configuration</title>
  14. <link>http://bugs.esecurity.net:8090/browse/SEN-8214</link>    
  15.         <description><![CDATA[Add support for putting labels between $xxx$ and %xxx%.  When the strings are saved to the DB, replace the labels with the tags.  Make sure this is done consistently between the Menu Configuration and Correlation Actions.]]></description>   
  16.         <environment><![CDATA[]]></environment>
  17.         <key id="86829">SEN-8214</key>
  18.         <summary>Add Consistent Support for putting event labels (not tags) in between %% and $$ in Correlation Actions and Menu Configuration</summary>
  19.         <type id="4">Enhancement</type>
  20.         <priority id="4">Minor</priority>
  21.         <status id="6">Closed</status>
  22.         <resolution id="1">Fixed</resolution>
  23.         <assignee username="michael.cooper">Michael Cooper</assignee>
  24.         <reporter username="john.gassner">John Gassner</reporter>
  25.         <created>Thu, 19 Jun 2008 16:43:42 -0400 (EDT)</created>
  26.         <updated>Fri, 27 Jun 2008 07:11:54 -0400 (EDT)</updated>
  27.             <version>6.1.0.07</version>
  28.              <fixVersion>6.1.0.09</fixVersion>
  29.          <component>Menu Configuration</component>
  30.          <component>Correlation</component>
  31.         <due></due>
  32.         <votes></votes>
  33.         <comments>
  34.             <comment author="hemendra.parmar" created="Thu, 26 Jun 2008 05:38:21 -0400 (EDT)" level="">Verified this issue using the latest code and found to be fixed. </comment>
  35.             <comment author="vinutha.vasanthu" created="Fri, 27 Jun 2008 07:11:54 -0400 (EDT)" level="">Bug Verified on the Build(s):2008-06-20_SENTINEL_6.1.0.9
  36. Environment:SLES 10(32 bit) with Oracle10(32 bit) 
  37. Status: Fixed 
  38. Comments:
  39. Added labels with both $xx$ and %xx% in Menu configuration and correlation Action and the labels are replaced with tags which is as expected.Hence closed.</comment>
  40.         </comments>
  41.         <customfields>
  42.         </customfields>
  43.     </item>
  44. </channel>
  45. </rss>
XSLT Code:
Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  3. <xsl:template match="/">
  4. <bugzilla version="3.0.4" urlbase="http://172.16.6.51/proj11/" maintainer="sunitha.akula@applabs.com" exporter="sunitha.akula@applabs.com">
  5. <!--Repeat the below for all the bugs in the xml-->
  6. <xsl:for-each select="/rss/channel/item">
  7. <bug>
  8. <xsl:variable name="ComponentCount" select="count(component)"/>
  9.     <xsl:choose>
  10.     <xsl:when test="$ComponentCount = '1'">
  11.         <component><xsl:value-of select="component"/></component>
  12.     </xsl:when>
  13.     <xsl:otherwise>
  14.     <xsl:for-each select="component">
  15.     <xsl:sort select="."/>
  16.     <xsl:if test=". != 'Documentation'">
  17.     <component>
  18.         <xsl:value-of select="."/>
  19.     </component>
  20.     </xsl:if>
  21.     </xsl:for-each>
  22.     </xsl:otherwise>
  23.     </xsl:choose>
  24. </bug>
  25. </xsl:for-each>
  26. </bugzilla>
  27. </xsl:template>
  28. </xsl:stylesheet>
Can anyone please let me know
1) how to find whether an element value started with a space?
2) how to exit from the for-each when the first element with no space is found?

Any suggestions / help is highly urgent & appreciated.

Thanks,
Saritha
Sep 11 '08 #1
4 3862
Dormilich
8,658 Expert Mod 8TB
1) element text starting with a space
Expand|Select|Wrap|Line Numbers
  1. starts-with(self::text(), ' ') // or maybe 
  2. starts-with(., ' ')
2) drop the for-each for
Expand|Select|Wrap|Line Numbers
  1. <xsl:value-of select="component[not(starts-with(self::text(), ' '))][self::text() != 'Documentation']"/>
I've not yet heard of a break instruction

regards

Please use [code] tags when posting code
Sep 11 '08 #2
Dormilich
8,658 Expert Mod 8TB
noticed a mistake... of cause the text node axis is child, not self (since the text node is a child of the element node)
Expand|Select|Wrap|Line Numbers
  1. starts-with(child::text(), ' ')
Sep 11 '08 #3
Hi Dormilich,

Thankyou for your reply.

Code [not(starts-with(child::text(), ' '))] worked fine.

Reg break / exit in XSLT for-each loop, I got to know that xslt does not support break / exit in for-each loop. So, to handle my requirement of getting the first component that does nt start with space, i have used position() = '1' in the for-each loop. It worked fine

Thanks,
Saritha
Sep 15 '08 #4
Dormilich
8,658 Expert Mod 8TB
Reg break / exit in XSLT for-each loop, I got to know that xslt does not support break / exit in for-each loop. So, to handle my requirement of getting the first component that does nt start with space, i have used position() = '1' in the for-each loop. It worked fine
how do you know, that position() = '1' doesn't contain a starting whitespace?

btw, you can trim whitespace from a string in XPath, too.

regards
Sep 15 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: walexand | last post by:
I have mistake by counting a recursive element. Can someone help me. XML Sample File ---------------------------------------------------- <content> <section> <title>index1</title> <section>...
3
by: Edwin G. Castro | last post by:
Hi, I'm new to XSLT and I'm having a hard time figuring out whether XSLT will do what I need it to do. I have a XML file with a whole bunch of <message> elements. These <message> elements...
3
by: Graham | last post by:
Hi, I am having trouble getting XSL to count the members of a group. What I am trying to do is group by <objectid.Contactid> and count the number of <activityid>'s for each <objectid.contactid>....
4
by: Herr Herrner | last post by:
hi, i'm trying to count something in xml via xsl and don't quite get it. i have something like (don't mind it's not logical) <doc> <s> <w type="a">how</w> <w type="b">are</w> <w...
23
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
1
by: Max Evans | last post by:
I have a XML file, which contains itemid-elements, e.g.: <itemid>3</itemid> <itemid>12</itemid> Now I want to convert these IDs to the corresponding name via XSLT. I thought I could do it this...
1
by: tslettebo | last post by:
Hi all. I've read Michael Kay's "XSLT" book, and used XSLT successfully as an HTML template system at our company (using basically the "fill-in-the-blanks" pattern of XSLT use: A template...
2
by: manishj | last post by:
Hello! I m trying to transform a XML file into new XML file . Description: Each unique value from the <name> elements in the input record are to be uniqued, alphabetized and stored with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.