469,360 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

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 3727
Dormilich
8,652 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,652 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,652 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

Post your reply

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

Similar topics

1 post views Thread by walexand | last post: by
3 posts views Thread by Graham | last post: by
4 posts views Thread by Herr Herrner | last post: by
23 posts views Thread by Mikko Ohtamaa | last post: by
1 post views Thread by Max Evans | last post: by
2 posts views Thread by manishj | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.