473,811 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

<Need Help>How to throw an error based on node count in XSLT?

17 New Member
Hi,
I have the following XML snippet:

Expand|Select|Wrap|Line Numbers
  1. <root>
  2.  
  3. <template1>
  4. <elem1>1000</elem1>
  5. <elem2>
  6.     <subelem1>65</subelem1>
  7. </elem2>
  8.  </template1>
  9.  
  10. <template1>
  11. <elem1>2000</elem1>
  12. <elem2>
  13. <subelem1>45</subelem1>
  14. </elem2>
  15. </template1>
  16.  
  17. <template2>
  18. <type1>
  19.  <tag1>1000</tag1>
  20. </type1>
  21.  
  22. <type1>
  23. <tag1>1000</tag1>
  24. </type1>
  25.  
  26. <type1>
  27. <tag1>1000</tag1>
  28. </type1>
  29.  
  30. <type1>
  31.  <tag1>2000</tag1>
  32.  </type1>
  33.  
  34. <type1>
  35. <tag1>2000</tag1>
  36. </type1>
  37.  
  38. <type1>
  39. <tag1>2000</tag1>
  40. </type1>
  41.  
  42. </template2>
  43. </root>
  44.  
  45.  
In the above code,there are 3 <tag1> elements whose value is 1000 and 3 <tag1> elements whose value is 2000.
The value of subelem1 = 65(for which the elem1 is 1000)
The value of subelem1 = 45(for which the elem1 is 2000)

Now the requirement is:
1.There should be only 2 <tag1> elements if subelem1 >60(in this case we have to get the subelem1 value by comparing the <tag1>=<elem1>. Here it is 1000).otherwise error has to be thrown.

2.There should be only 1 <tag1> element if subelem1 <60(in this case we have to get the subelem1 value by comparing the <tag1>=<elem1>. Here it is 2000).otherwise error has to be thrown.

Using your XSLT code that you replied to my previous question,when i print the count in error message it is giving the total count as 6 where as we have to get the count as 3 for <tag1>=1000

Please help me,how can we do the above requirement.

Thanks,
Sep 16 '08
16 2602
Dormilich
8,658 Recognized Expert Moderator Expert
I understand it so, that the count of <tag1> has to be now less than 3 if any <subelem1> is > 60 (although this will kill the condition <tag1> = <elem1>). so it should be an easier expression now.
Expand|Select|Wrap|Line Numbers
  1. <xsl:if test="count(//subelem1[. &gt; 60]) and count(//tag1) &gt; 2">
  2.   <xsl:message terminate="yes">
  3.   <xsl:text>Only 2 tags are allowed for tag1</xsl:text>
  4. </xsl:if>
please correct me, if my understanding of the conditions is wrong
The ones meeting the criteria are:
/root/template2/type1/tag1[[/root/template1[elem2/subelem1 &gt; 60]/elem1] = .]
In other words, there exists a template1 node with subelem1 > 60, such that the elem1 node equals the current tag1 node.
I don't think this will work in the intended way since [expression] will always return true or false.

regards
Oct 6 '08 #11
jkmyoung
2,057 Recognized Expert Top Contributor
Edited above message after Dormilich's comment.
Oct 6 '08 #12
Dormilich
8,658 Recognized Expert Moderator Expert
still the question remains, if the condition <tag1> = <elem1> must be fulfilled. unfortunately, that's not clear from the actual question. (this was the case for the original question)
Oct 6 '08 #13
njsimha
17 New Member
Hi young,
Thanks for the mail.
The explanation you gave seems to be logically perfect but when I executed this,I am encountering an error saying the "XPath error : Invalid expression"

Please suggest me.


Xpath for values (>60?) you're looking at is /root/template1/elem2/subelem1

Find all the template 1 nodes with that value > 60
/root/template1[elem2/subelem1 &gt; 60]

Now we find each of the indexes for these values > 60, eg the elem1 node.
/root/template1[elem2/subelem1 &gt; 60]/elem1

general xpath for type1's tag1 values.
/root/template2/type1/tag1

The ones meeting the criteria are:
(/root/template2/type1/tag1[/root/template1[elem2/subelem1 &gt; 60]/elem1 = .]
In other words, there exists a template1 node with subelem1 > 60, such that the elem1 node equals the current tag1 node.

Putting it together, it should look like:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/root">
  2.     <xsl:if test="count(/root/template2/type1/tag1[/root/template1[elem2/subelem1 &gt; 60]/elem1 = .] ) &gt; 2">
  3.         <xsl:message terminate="yes">
  4.             <xsl:value-of select="'Only 2 tags are allowed for tag1 &gt; 60 '"/>
  5.         </xsl:message>
  6.     </xsl:if>
  7. </xsl:template>
  8.  
  9.  
Oct 7 '08 #14
njsimha
17 New Member
Hi Dormilich,
what you said is correct.The condition <tag1> = <elem1> has also to be fulfilled.

To make more clear on my question:
In the XML code mentioned in comment # 9:
a) There are 2 <type1> tags whose <tag1> value = <elem1> (that is 1000)
b) There are 2 <type1> tags whose <tag1> value = <elem1> (that is 2000)
Individual count is 2 where as the combined count is 4(error has to be thrown for this also as the total count is greater than 2)

In both the above cases <subelem1> is greater than 60 for each <template1> under which the <elem1> is 1000,2000.

Now the requirement is :
The error has to be thrown in two cases:
1.If the count of all the <tag1> = <elem1> and <subelem1 > 60 is greater than 2

2.If the total count of all the sets meeting the above criteria is greater than 2.

According to the code mentioned in comment # 9,if I add one more <type1> with <tag1> =1000,it throws error saying only 2 tags are allowed for <tag1> = <elem1> where as there are already 4 <type1> tags whose <tag1>= <elem1> but with different <subelem1> values >60.

I guess I made my question clearly.

Thanks,

still the question remains, if the condition <tag1> = <elem1> must be fulfilled. unfortunately, that's not clear from the actual question. (this was the case for the original question)
Oct 7 '08 #15
Dormilich
8,658 Recognized Expert Moderator Expert
Now the requirement is :
The error has to be thrown in two cases:
1.If the count of all the <tag1> = <elem1> and <subelem1> > 60 is greater than 2

2.If the total count of all the sets meeting the above criteria is greater than 2.
wouldn't the 2nd requirement always include the 1st one (thus making it obsolete)?

The explanation you gave seems to be logically perfect but when I executed this,I am encountering an error saying the "XPath error : Invalid expression"
couldn't verify that, the expression was working perfectly, when I tried it.

regards
Oct 8 '08 #16
njsimha
17 New Member
Hi,
It worked well.

Thanks for your help.

wouldn't the 2nd requirement always include the 1st one (thus making it obsolete)?

couldn't verify that, the expression was working perfectly, when I tried it.

regards
Oct 21 '08 #17

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

Similar topics

31
14364
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
1
1556
by: Chuck | last post by:
I have a question tht I'm sure someone out there can answer. A friend of mine has developed an Access application that is a very business specific (niche type of application). The application would only be used by one type of business but there are a lot of these businesses out there along without too much competition. He is interested in selling the application (a runtime version of Access would be supplied as well) but he works...
2
309
by: Chuck | last post by:
I am trying to retrive one field from the "company info" table that contains several fields but I want the "company name" field. There is only one record in this table. When I entered the code below (which I copied out of a book). I get the following error. The hickup comes in the "dim db as database" line. However if I comment this out. It doesn't like the next line either. I appreciate any help you can give me. Also is this a problem...
3
2080
by: WØCBF | last post by:
I know this code has worked before but now appears to get a compile error. The code it seems to choke on line 12. I receive the following message and it highlights the "rst!" statement. Error received: Microsoft Visual Basic Compile error: Type declaration character does not match declared data type Private Sub Form_Load() 1 Dim db As Database
1
1566
by: WØCBF | last post by:
Greetings, I have a form that is bound to a table that has about 15 fields in it and all of the fields are displayed on the main form. I have 4 fields that will be updated and I want to write that information to another table that only has only these 4 fields in it. Since the form is bound to one table, how can I write these 4 fields to a different table. These fields will be written when on on click event occurs on a button. Any help...
1
7728
by: Robert Dodier | last post by:
Hello, Sorry for asking what must be a FAQ, but I wasn't able to find the answer. I have an XML document fragment which I want to store as a text string. I want a function to convert any XML special characters such as < > into the corresponding character entities. I'm working with Java.
3
2319
by: ALKASER266 | last post by:
Hey guyz I am a java beginner and I have problem with part of the code. The thing that I don't know how to do it in this code is how to show the user that he/she has input an invalid input In my code the valid input will be anything except 1 to 7 and I hope that your answers will be as simple way as ever coz i am just a beginner, thanks This is the code: /**
5
1964
by: njsimha | last post by:
Hi, In the folowing XML snippet: <template1> <name>student</name> <elem1> <subelem1>65</subelem1> <subelem2>15</subelem2> </elem1>
2
3173
by: njsimha | last post by:
Hi, I have a query regarding the selection of a particular tag based on a condition. In the following XML code: <root> <template1> <elem1>1000</elem1> <elem2>
0
9724
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.