group nodes based on predicates in xpath string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tsbalaji69
    New Member
    • Jan 2013
    • 2

    group nodes based on predicates in xpath string

    I am populating a xmldocument and only if the selectsinglenod e 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:
    Code:
    root/parent/one
    root/parent/two
    root/parent/three
    root/parent/one
    root/parent/two
    root/parent/three
    root/parent/one
    root/parent/two
    root/parent/three
    i am able to get the xmldocument build which is like this
    Code:
    Code:
    <root>
     <parent>
       <one/>
       <two/>
       <three/>
       <one/>
       <two/>
       <three/>
       <one/>
       <two/>
      <three/>
     </parent>
    </root>
    if i have some index in the xpath string something like this
    Code:
    Code:
    root/parent/one
    root/parent/two
    root/parent/three
    root/parent/one[2]
    root/parent/two[2]
    root/parent/three[2]
    root/parent/one[3]
    root/parent/two[3]
    root/parent/three[3]
    I need to group them like this
    Code:

    Code:
    <root>
     <parent>
        <one/>
        <two/>
        <three/>
     </parent>
     <parent>
        <one/>
        <two/>
        <three/>
     </parent>
     <parent>
        <one/>
        <two/>
        <three/>
     </parent>
    </root>
    can this be done using appendchild method in c# to group based on the predicate index or any other method
    thank you
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    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.

    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/root/parent">
    	<xsl:variable name="p" select="count(../parent/*) div 3"></xsl:variable>
    	<root>
    	<xsl:if test="$p &gt; 0">
    		<xsl:call-template name="oneGroup">
    			<xsl:with-param name="pos" select="1"></xsl:with-param>
    			<xsl:with-param name="_count" select="$p"></xsl:with-param>
    		</xsl:call-template>
    	</xsl:if>
    	</root>
    </xsl:template>
    
    <xsl:template name="oneGroup">
    	<xsl:param name="pos"></xsl:param>
    	<xsl:param name="_count"></xsl:param>
    	<parent>
    		<xsl:copy-of select="one[$pos]"></xsl:copy-of>
    		<xsl:copy-of select="two[$pos]"></xsl:copy-of>
    		<xsl:copy-of select="three[$pos]"></xsl:copy-of>
    	</parent>
    	<xsl:if test="$pos &lt; $_count">
    		<xsl:call-template name="oneGroup">
    			<xsl:with-param name="pos" select="$pos + 1"></xsl:with-param>
    			<xsl:with-param name="_count" select="$_count"></xsl:with-param>
    		</xsl:call-template>
    	</xsl:if>
    </xsl:template>
    
    </xsl:stylesheet>

    Comment

    • tsbalaji69
      New Member
      • Jan 2013
      • 2

      #3
      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

      Comment

      Working...