Counting the elements in xml using XSLT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saritha2008
    New Member
    • Sep 2008
    • 9

    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:

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    
    
    <!--  RSS generated by JIRA 91 at Thu Sep 11 06:23:06 EDT 2008 -->
    
    <rss version="0.92">
    <channel>
        <title>Sentinel Issue Tracking System</title>
        <link>http://bugs.esecurity.net:8090</link>
        <description>This file is an XML representation of an issue</description>
        <language>en</language>
        <item>
    <title>[SEN-8214] Add Consistent Support for putting event labels (not tags) in between %% and $$ in Correlation Actions and Menu Configuration</title>
    <link>http://bugs.esecurity.net:8090/browse/SEN-8214</link>    
            <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>   
            <environment><![CDATA[]]></environment>
            <key id="86829">SEN-8214</key>
            <summary>Add Consistent Support for putting event labels (not tags) in between %% and $$ in Correlation Actions and Menu Configuration</summary>
    		<type id="4">Enhancement</type>
            <priority id="4">Minor</priority>
            <status id="6">Closed</status>
            <resolution id="1">Fixed</resolution>
            <assignee username="michael.cooper">Michael Cooper</assignee>
            <reporter username="john.gassner">John Gassner</reporter>
            <created>Thu, 19 Jun 2008 16:43:42 -0400 (EDT)</created>
            <updated>Fri, 27 Jun 2008 07:11:54 -0400 (EDT)</updated>
                <version>6.1.0.07</version>
                 <fixVersion>6.1.0.09</fixVersion>
             <component>Menu Configuration</component>
             <component>Correlation</component>
            <due></due>
            <votes></votes>
            <comments>
                <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>
                <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
    Environment:SLES 10(32 bit) with Oracle10(32 bit) 
    Status: Fixed 
    Comments:
    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>
            </comments>
            <customfields>
            </customfields>
        </item>
    </channel>
    </rss>
    XSLT Code:
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
    <bugzilla version="3.0.4" urlbase="http://172.16.6.51/proj11/" maintainer="sunitha.akula@applabs.com" exporter="sunitha.akula@applabs.com">
    <!--Repeat the below for all the bugs in the xml-->
    <xsl:for-each select="/rss/channel/item">
    <bug>
    <xsl:variable name="ComponentCount" select="count(component)"/>
    	<xsl:choose>
    	<xsl:when test="$ComponentCount = '1'">
    		<component><xsl:value-of select="component"/></component>
    	</xsl:when>
    	<xsl:otherwise>
    	<xsl:for-each select="component">
    	<xsl:sort select="."/>
    	<xsl:if test=". != 'Documentation'">
    	<component>
    		<xsl:value-of select="."/>
    	</component>
    	</xsl:if>
    	</xsl:for-each>
    	</xsl:otherwise>
    	</xsl:choose>
    </bug>
    </xsl:for-each>
    </bugzilla>
    </xsl:template>
    </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
    Last edited by Dormilich; Dec 8 '09, 08:23 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Counting the elements in xml using XSLT

    1) element text starting with a space
    Code:
    starts-with(self::text(), ' ') // or maybe 
    starts-with(., ' ')
    2) drop the for-each for
    Code:
    <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
    Last edited by Dormilich; Sep 11 '08, 02:39 PM. Reason: [code] info added

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      noticed a mistake... of cause the text node axis is child, not self (since the text node is a child of the element node)
      Code:
      starts-with(child::text(), ' ')

      Comment

      • saritha2008
        New Member
        • Sep 2008
        • 9

        #4
        Hi Dormilich,

        Thankyou for your reply.

        Code [not(starts-with(child::tex t(), ' '))] 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

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by saritha2008
          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

          Comment

          Working...