Solved

Group all sequential comments all in one binder <p>, using XSL

Posted on 2016-08-13
10
41 Views
Last Modified: 2016-08-14
Dear all,
Using XSL Need to group all sequential @LineItemType="COMMENTS" to one <p>  

here's the XML:
<children LineItemType="COMMENTS" FixText="'-------------------------------"/>
<children LineItemType="COMMENTS" FixText="' xxxxxxx  "/>
<children LineItemType="COMMENTS" FixText="'-------------------------------"/>
<children LineItemType="COMMENTS" FixText="  Dom rx"/>
<children LineItemType="COMMENTS" FixText="  Dom Where"/>
<children LineItemType="TEXT" FixText="Just Simple TEXT1"/>
<children LineItemType="Text" FixText="Just Simple TEXT2"/>
<children LineItemType="COMMENTS" FixText="'-------------------------------"/>
<children LineItemType="Text" FixText="Just Simple TEXT3"/>
<children LineItemType="Text" FixText="Just Simple TEXT4"/>

Open in new window


Thanks
0
Comment
Question by:ethar1
  • 7
  • 3
10 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41755147
Try this (XSLT2)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"></xsl:output>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"></xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*[children]">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:for-each-group select="*" group-adjacent="if(self::children and @LineItemType='COMMENTS') then('comments') else(generate-id())">
                <xsl:choose>
                    <xsl:when test="current-grouping-key() = 'comments'">
                        <p>
                            <xsl:copy-of select="current-group()"/>
                        </p>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:ethar1
ID: 41755480
Dear Geert Bormans
Thanks for your reply.

I got this error
for-each-group' is not a recognized extension element. An error occurred at 101,14
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41755559
That is because you are likely not using xslt2
Xslt2 is now common use for almost ten years, so i assume xslt2 unless a different context is given

Solving this grouping issue is harder in xslt1

Please, in your questions specify the execution context
Please check if xslt2 can be used, if not i will do an xslt1 variant
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41755602
I have assumed, you want this result using an XSLT1 (note that I wrapped your non wellformed XML in a root element to make it valid)

?xml version="1.0" encoding="UTF-8"?>
<root>
   <p>
      <children LineItemType="COMMENTS" FixText="'-------------------------------"/>
      <children LineItemType="COMMENTS" FixText="' xxxxxxx  "/>
      <children LineItemType="COMMENTS" FixText="'-------------------------------"/>
      <children LineItemType="COMMENTS" FixText="  Dom rx"/>
      <children LineItemType="COMMENTS" FixText="  Dom Where"/>
   </p>
   <children LineItemType="TEXT" FixText="Just Simple TEXT1"/>
   <children LineItemType="Text" FixText="Just Simple TEXT2"/>
   <p>
      <children LineItemType="COMMENTS" FixText="'-------------------------------"/>
   </p>
   <children LineItemType="Text" FixText="Just Simple TEXT3"/>
   <children LineItemType="Text" FixText="Just Simple TEXT4"/>
</root>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41755603
This would be the code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()[1]"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    
    <xsl:template match="children[@LineItemType='COMMENTS']">
        <p>
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="node()[1]"/>
            </xsl:copy>
            <xsl:apply-templates select="following-sibling::node()[1][self::children[@LineItemType='COMMENTS']]" mode="inner-p"/>
        </p>
        <xsl:apply-templates select="following-sibling::node()[not(self::children[@LineItemType='COMMENTS'])][1]"/>
    </xsl:template>
    
    <xsl:template match="children[@LineItemType='COMMENTS']" mode="inner-p">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()[1]"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1][self::children[@LineItemType='COMMENTS']]" mode="inner-p"/>
    </xsl:template>
        
    
    
</xsl:stylesheet>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ethar1
ID: 41755610
Dear Geert Bormans,
I got issues here :) , I am ready to open questions as much as you like to follow.
First issue , I am very confused about the xslt2 and xslt1 , is it enough to define
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >?
Actually I am using VC# 2013 to transform like this:
 
DOMDocument XMLSourceDataSingleDoc = new DOMDocument( );
XMLSourceDataSingleDoc.transformNode(SingleXSLFile);

Open in new window


Second Issue : Here's my XML:

?xml version="1.0" encoding="UTF-8"?>
<root>
      <children LineItemType="COMMENTS" FixText="'-------------------------------"/>
      <children LineItemType="COMMENTS" FixText="' xxxxxxx  "/>
      <children LineItemType="COMMENTS" FixText="'-------------------------------"/>
      <children LineItemType="COMMENTS" FixText="  Dom rx"/>
      <children LineItemType="COMMENTS" FixText="  Dom Where"/>
   <children LineItemType="TEXT" FixText="Just Simple TEXT1"/>
   <children LineItemType="Text" FixText="Just Simple TEXT2"/>
      <children LineItemType="COMMENTS" FixText="'-------------------------------"/>
   <children LineItemType="Text" FixText="Just Simple TEXT3"/>
   <children LineItemType="Text" FixText="Just Simple TEXT4"/>
</root>

Open in new window


And here's the output:
<p>
'-------------------------------
' xxxxxxx  
'-------------------------------
  Dom rx
  Dom Where
</p>
Just Simple TEXT1
Just Simple TEXT2
<p>'-------------------------------</p>
Just Simple TEXT3
Just Simple TEXT4

Open in new window


Thanks for your efforts
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41755613
No, you need a processor that supports XSLT2
VC# does not

I don't need you to open many question, I need you to be specific enough and do some of the work yourself

The XML you sent is exactly the one I used for testing

The output is not what comes out of my stylesheet
0
 

Author Comment

by:ethar1
ID: 41755614
ok, Thanks
in the XML you use there is a <p> where it is not in my source.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 41755617
In case the output you sent is "what you want" (please be clear in your questions)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()[1]"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    
    <xsl:template match="children">
        <xsl:text>&#10;</xsl:text>
        <xsl:value-of select="@FixText"/>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    
    <xsl:template match="children[@LineItemType='COMMENTS']">
        <p>
            <xsl:text>&#10;</xsl:text>
            <xsl:value-of select="@FixText"/>
             <xsl:apply-templates select="following-sibling::node()[1][self::children[@LineItemType='COMMENTS']]" mode="inner-p"/>
        </p>
        <xsl:apply-templates select="following-sibling::node()[not(self::children[@LineItemType='COMMENTS'])][1]"/>
    </xsl:template>
    
    <xsl:template match="children[@LineItemType='COMMENTS']" mode="inner-p">
        <xsl:text>&#10;</xsl:text>
        <xsl:value-of select="@FixText"/>
        <xsl:apply-templates select="following-sibling::node()[1][self::children[@LineItemType='COMMENTS']]" mode="inner-p"/>
    </xsl:template>
    
    
    
</xsl:stylesheet>

Open in new window

1
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41755618
I did not post any source, I only posted a result
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now