473,799 Members | 3,350 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 #1
16 2601
jkmyoung
2,057 Recognized Expert Top Contributor
Can you post the previous code?
It's probably as easy as adding a condition[elem1=$value]
Sep 16 '08 #2
njsimha
17 New Member
Hi Young,
I tried the following code:

Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="rules" select="count(/template2/type1[tag1 = /template1/elem1])"/>
  2. <xsl:if test="/template1/elem2/subelem1 >60 and $rules >2">
  3. <xsl:message terminate="yes">
  4. <xsl:value-of select="concat("Only 2 tags are allowed for tag1 =1000")/>
  5. </xsl:if>
  6. </xsl:if>
  7.  
The problem with this code is it is giving the total count for variable rules as 6 where as we have to get the count as 3 for <tag1>=1000.T he condition to evaluate rules is selecting all the <tag1> nodes and giving count as 6.How to solve this?

Also please note that there will be 10 <template1> nodes.I have to check across all <template1> tags.

Thanks,
Sep 16 '08 #3
jkmyoung
2,057 Recognized Expert Top Contributor
Assuming you're in template:
<xsl:template match="/root/template1/elem1/subelem1">
The value you want for each one is in relative path: ../../elem1
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="elem1" select="../../elem1"/>
  2. <xsl:variable name="rules" select="count(/template2/type1[tag1 = $elem1])"/>
  3. .......
  4. <xsl:value-of select="concat('Only 2 tags are allowed for tag1 =',$elem1)"/>
  5.  
Sep 16 '08 #4
njsimha
17 New Member
Hi Young,
Do you mean to say :

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/root/template1/elem1/subelem1[. &gt; 60]">
  2. <xsl:variable name="elem1" select="../../elem1"/>
  3. <xsl:variable name="rules" select="count(/template2/type1[tag1 = $elem1])"/>
  4. <xsl:if test="$rules &gt; 2">
  5. <xsl:message terminate="yes">
  6. <xsl:value-of select="concat("Only 2 tags are allowed for tag1 = ', $elem1)")/>
  7. </xsl:if>
  8. </xsl:template>
  9.  
The above code is not working.
In the template match we have to select all <template1> whose <subelem1> value is > 60,so i changed the template match.is it ok?
whatelse has to be changed in the above code to make it working?

Thanks,


Assuming you're in template:
<xsl:template match="/root/template1/elem1/subelem1">
The value you want for each one is in relative path: ../../elem1
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="elem1" select="../../elem1"/>
  2. <xsl:variable name="rules" select="count(/template2/type1[tag1 = $elem1])"/>
  3. .......
  4. <xsl:value-of select="concat('Only 2 tags are allowed for tag1 =',$elem1)"/>
  5.  
Sep 16 '08 #5
Dormilich
8,658 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/root/template1/elem1/subelem1[. &gt; 60]">
  2. <xsl:variable name="elem1" select="../../elem1"/>
  3. <xsl:variable name="rules" select="count(/template2/type1[tag1 = $elem1])"/>
  4. <xsl:if test="$rules &gt; 2">
  5. <xsl:message terminate="yes">
  6. <xsl:value-of select="concat("Only 2 tags are allowed for tag1 = ', $elem1)")/>
  7. </xsl:if>
  8. </xsl:template>
  9.  
The above code is not working.
In the template match we have to select all <template1> whose <subelem1> value is > 60,so i changed the template match.is it ok?
whatelse has to be changed in the above code to make it working?
line #1: "//template1[elem2/subelem1 &gt; 60]" (ok I shifted the condition a bit, but that's only to look better... adjust $elem1 accordingly)

in the above code you're not selecting <template1> but <subelem1>

line #3: /root/template2/type1[tag1 = $elem1] or //template2/type1[tag1 = $elem1]

line #5: element not closed

line #6: typing mistake, 2nd quotation mark must be ' not "

regards
Sep 16 '08 #6
njsimha
17 New Member
Hi Dormilich,
Thanks a lot.Its working perfectly.

line #1: "//template1[elem2/subelem1 &gt; 60]" (ok I shifted the condition a bit, but that's only to look better... adjust $elem1 accordingly)

in the above code you're not selecting <template1> but <subelem1>

line #3: /root/template2/type1[tag1 = $elem1] or //template2/type1[tag1 = $elem1]

line #5: element not closed

line #6: typing mistake, 2nd quotation mark must be ' not "

regards
Sep 17 '08 #7
Dormilich
8,658 Recognized Expert Moderator Expert
glad I could be of help
Sep 17 '08 #8
njsimha
17 New Member
Hi Dormilich,

The following is the code you suggested for my question:

<xsl:template match="/root/template1[elem2/subelem1 &gt; 60]">
<xsl:variable name="elem1" select="elem1"/>
<xsl:variable name="rules" select="count(/root/template2/type1[tag1 = $elem1])"/>
<xsl:if test="$rules &gt; 2">
<xsl:message terminate="yes" >
<xsl:value-of select="concat( "Only 2 tags are allowed for tag1 = ', $elem1)')/>
</xsl:if>
</xsl:template>

It is working fine but in one scenario this is not working.I changed 45 to 75 value for subelem1.The $rules in the code is selecting the count for each subelem1 but I want to throw the error if the count is > 2 for all the subelem1 whose value is > 60.Please note that there can be any number of <template1> tags.

In the following code,(according to your code) it works fine as there are only two <tag1> tags for each subelem1 but I want to throw error after combining the count for all the <subelem1> tags whose value is > 60.

So it has to throw error for the following input saying that "The combined total count is 4.Only 2 <tag1> are allowed under <template2>"


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>75</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>2000</tag1>
  28. </type1>
  29.  
  30. <type1>
  31.  <tag1>2000</tag1>
  32. </type1>
  33.  
  34. </template2>
  35.  
Please help me in adding this extra check for the code you suggested.
Thanks.
glad I could be of help
Oct 6 '08 #9
jkmyoung
2,057 Recognized Expert Top Contributor
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 6 '08 #10

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

Similar topics

31
14353
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
1554
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
2079
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
1565
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
7720
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
2318
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
1963
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
9688
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
9544
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
10490
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...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
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...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.