473,473 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

group nodes based on predicates in xpath string

2 New Member
I am populating a xmldocument and only if the selectsinglenode of the given path exist i try to clone. I do this to accommodate same name tags. The issue in which i got stuck is I am able to build the xml with accommodating same name tags but i am unable to group them because logically it appends to the parent. I need a way to figure out to differentiate the same name tags in the xpath string and group them. e.g
if i have a list of xpath strings like this
Code:
Expand|Select|Wrap|Line Numbers
  1. root/parent/one
  2. root/parent/two
  3. root/parent/three
  4. root/parent/one
  5. root/parent/two
  6. root/parent/three
  7. root/parent/one
  8. root/parent/two
  9. root/parent/three
  10.  
i am able to get the xmldocument build which is like this
Code:
Expand|Select|Wrap|Line Numbers
  1. <root>
  2.  <parent>
  3.    <one/>
  4.    <two/>
  5.    <three/>
  6.    <one/>
  7.    <two/>
  8.    <three/>
  9.    <one/>
  10.    <two/>
  11.   <three/>
  12.  </parent>
  13. </root>
  14.  
if i have some index in the xpath string something like this
Code:
Expand|Select|Wrap|Line Numbers
  1. root/parent/one
  2. root/parent/two
  3. root/parent/three
  4. root/parent/one[2]
  5. root/parent/two[2]
  6. root/parent/three[2]
  7. root/parent/one[3]
  8. root/parent/two[3]
  9. root/parent/three[3]
  10.  
I need to group them like this
Code:

Expand|Select|Wrap|Line Numbers
  1. <root>
  2.  <parent>
  3.     <one/>
  4.     <two/>
  5.     <three/>
  6.  </parent>
  7.  <parent>
  8.     <one/>
  9.     <two/>
  10.     <three/>
  11.  </parent>
  12.  <parent>
  13.     <one/>
  14.     <two/>
  15.     <three/>
  16.  </parent>
  17. </root>
  18.  
can this be done using appendchild method in c# to group based on the predicate index or any other method
thank you
Jan 10 '13 #1
2 2787
Anas Mosaad
185 New Member
Hi there,

This a sample XSLT template that does what you need. Not sure how you can integrate this with C#, but this is the core part - the XSLT/XPath part.

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" indent="yes"/>
  3.  
  4. <xsl:template match="/root/parent">
  5.     <xsl:variable name="p" select="count(../parent/*) div 3"></xsl:variable>
  6.     <root>
  7.     <xsl:if test="$p &gt; 0">
  8.         <xsl:call-template name="oneGroup">
  9.             <xsl:with-param name="pos" select="1"></xsl:with-param>
  10.             <xsl:with-param name="_count" select="$p"></xsl:with-param>
  11.         </xsl:call-template>
  12.     </xsl:if>
  13.     </root>
  14. </xsl:template>
  15.  
  16. <xsl:template name="oneGroup">
  17.     <xsl:param name="pos"></xsl:param>
  18.     <xsl:param name="_count"></xsl:param>
  19.     <parent>
  20.         <xsl:copy-of select="one[$pos]"></xsl:copy-of>
  21.         <xsl:copy-of select="two[$pos]"></xsl:copy-of>
  22.         <xsl:copy-of select="three[$pos]"></xsl:copy-of>
  23.     </parent>
  24.     <xsl:if test="$pos &lt; $_count">
  25.         <xsl:call-template name="oneGroup">
  26.             <xsl:with-param name="pos" select="$pos + 1"></xsl:with-param>
  27.             <xsl:with-param name="_count" select="$_count"></xsl:with-param>
  28.         </xsl:call-template>
  29.     </xsl:if>
  30. </xsl:template>
  31.  
  32. </xsl:stylesheet>
  33.  
  34.  
Jan 10 '13 #2
tsbalaji69
2 New Member
Thank you very much anas for a pointer to do this. I got to do this in c#. will try if i can transform xsl code using c#
thank you
Jan 11 '13 #3

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

Similar topics

1
by: Alastair Cameron | last post by:
VB6, MSXML 3.2 installed: Q1. I am having a problem selecting nodes with XPATH expressions when an attribute values contain backslashes (\\) in as part of its value: For example the...
2
by: Peter Gerstbach | last post by:
Hi, I want to convert with XSLT/XPATH a String like "Aaa bbb ccc" with variant length into to "AaaBbbCcc". I think it should be possible with these steps: 1) tokenize the String with ' ' as...
4
by: Larry R | last post by:
I am trying to use XPath (XSLT 1.0), EXSLT 1.1 (.Net) to select the nodelist consisting of the 'top n' nodes. THe counter is the count of item/value. Using a traditional for-each logic, the...
1
by: Dinesh | last post by:
Hello, I have a very complex XmlDocument. I want to create a new XmlDocument that contain only certain certain elements, based on a set of XPATH expressions I have. What is the easiest and...
1
by: Yossi | last post by:
Nice tool! I am looking for something that will do the reverse. I have a whole bunch of XPath statements and I need to use them to generate and populate an XML document. I have mapping file...
2
by: Nishad | last post by:
Is anybody know how to write xpath for string Value Comparisons thank you
1
by: RobAMacAF | last post by:
How do I go about making a variable based on a string? For instance.... string test = "Bob"; System.Windows.Forms.TextBox test = new System.Windows.Forms.TextBox(); What I want to do is...
3
by: Paul Jung | last post by:
Hello there, how to cut a certain group of words in a string, for example, "<img src='images/smiles/grin.gif' smilietext=':grin:' border='0' style='vertical-align:middle' alt=':grin:'...
4
by: Manikrag | last post by:
Hi Team, Is it possible to sort select query based on input string? I am looking for somthing like: select TOP 20 PREFERRED_NAME from FRS_TABLE where Lower(PREFERRED_NAME) like...
1
by: Kris tarun | last post by:
I have a table of a retail store which has almost 13000 customers. and i want to write a query for this.. Group products based on their sales patterns. Highest, lowest, and median values. Use...
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,...
0
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.