Solved

how to truncate, merge and remove duplicate values from xml nodes using xslt 1.0

Posted on 2011-09-27
14
634 Views
Last Modified: 2012-08-14
My xml is attached I need to truncate the leading colon(:), spaces from all the three nodes MSHValues,MTHValues and SNOMEDCTValues, merge the values and remove the duplicates
<?xml version="1.0" encoding="UTF-8"?>

<documents>
	<document id="http://www.qualitymeasures.ahrq.gov/content.aspx?id=26727">

<element name="MSHValues">
			<value>: 

                                            Long-Term Care; Pressure Ulcer; Wound Healing

                                        </value>
		</element>
		<element name="MTHValues">
			<value>: 

                                            long-term care; Pressure Ulcer; Wound Healing

                                        </value>
		</element>
		<element name="SNOMEDCTValues">
			<value>: 

                                            Pressure ulcer (399912005); Pressure ulcer (400192002); Pressure ulcer (418172001); Pressure ulcer (420226006); Tissue repair (18867006); Tissue repair (298005009)

                                        

                                </value>
		</element>
	</document>
</documents>

Open in new window

0
Comment
Question by:mmalik15
  • 8
  • 6
14 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36710614
I think you need to be more explicit.
Do you want unique individual terms?
Case insensitive?
Care about the numbers?

result is
long-term care; pressure ulcer; wound healing; tissue repair

or something else, please specify

XSLT1? or XSLT2?
Which XSLT processor (with or without nodesets?)
0
 

Author Comment

by:mmalik15
ID: 36710694
yeah i need the unique individual terms and dont need the numbers like this  (399912005) and no concerns about case of the terms and still using xstl 1.0

thanks
0
 

Author Comment

by:mmalik15
ID: 36710698
need out put like this

<msubjectgen>
long-term care; pressure ulcer; wound healing; tissue repair
</msubjectgen>
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36710751
OK, bit tricky,
I would do this in two steps, one two extract the terms and one to get the duplicates out
Which processor are you using (so I can introduce a nodeset)?
Or can you do a two-step XSLT?

Note that this is a very good usecase for a migration to XSLT2
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36710778
Need to go now, but here is waht you can do

- recursively go through all the values from ';' to ';'
- strip out the rubish and lowercase
- add a new found term to a build list (with markers between the terms) and only add if not yet present

after you are done, remove the markers

This way it can be done in one pass in XSLT1

be gone for two hours or so, if you haven't done it by then, will help you with code
0
 

Author Comment

by:mmalik15
ID: 36711006
sorry tried it but no luck yet. I am still a beginner with xslt and xml. Do you know any good resources to learn xslt online?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36712263
http://my.safaribooksonline.com/book/xml/0596000537
is an excellent book, available through safari online
0
 

Author Comment

by:mmalik15
ID: 36717017
Thanks for referring this book.

I have tried the attached xslt and manage to remove spaces, colon and numbers but dont know how to extract the unique values

 
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space   elements="*"/>
<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="documents/document">
 <xsl:for-each select=".">
<xsl:value-of select="translate(translate(substring(., 2, string-length(.)),':',';'),'(0123456789)','')"/>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 36720506

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:variable name="lc" select="'azertyuiopqsdfghjklmwxcvbn'"/>
        <xsl:variable name="uc" select="'AZERTYUIOPQSDFGHJKLMWXCVBN'"/>
        <xsl:variable name="all-string">
            <xsl:for-each select="//value">
                <xsl:if test="not(position() = 1)"><xsl:text>; </xsl:text></xsl:if>
                <xsl:value-of select="translate(normalize-space(substring-after(., ':')), $uc, $lc)"/>
            </xsl:for-each>
        </xsl:variable>
       <!-- <xsl:value-of select="$all-string"/>-->
        <xsl:call-template name="process-values">
            <xsl:with-param name="list" select="';'"/>
            <xsl:with-param name="remaining" select="$all-string"/>
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="process-values">
        <xsl:param name="list"/>
        <xsl:param name="remaining"/>
        <xsl:choose>
            <xsl:when test="contains($remaining, ';')">
                <xsl:variable name="new-list">
                    <xsl:call-template name="filter-and-add">
                        <xsl:with-param name="list" select="$list"/>
                        <xsl:with-param name="term" select="substring-before($remaining, ';')"/>
                    </xsl:call-template>
                </xsl:variable>
                 <xsl:call-template name="process-values">
                    <xsl:with-param name="list" select="$new-list"/>
                    <xsl:with-param name="remaining" select="substring-after($remaining, ';')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="new-list">
                    <xsl:call-template name="filter-and-add">
                        <xsl:with-param name="list" select="$list"/>
                        <xsl:with-param name="term" select="$remaining"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="substring($new-list, 3)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="filter-and-add">
        <xsl:param name="list"/>
        <xsl:param name="term"/>
        <xsl:param name="norm-term" select="normalize-space(translate($term, '()1234567890', ''))"></xsl:param>
        <xsl:choose>
            <xsl:when test="not(contains(concat($list, '; '), concat('; ', $norm-term, '; ')))">
                <xsl:value-of select="concat($list, '; ', $norm-term)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$list"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
     
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36720585
$all-string is a lower-cased (by using translate() version of the normalize-spaced values
basically this string has all terms listed, seperated by ';'

then I recurse through that string per term (that is what process-values does

filter-and-add is a function (named template) that removes the (nnnnnn) from a term (not only at the end, but all, this might be to rough)
checks if the term is on the list and if not adds it to the list
filter-and-add takes the term and the list as a parameter and returns the altered list
That is how I ensure uniqueness... it takes some playing with '; '
0
 

Author Comment

by:mmalik15
ID: 36899232
Many thanks buddy
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36899826
welcome my friend
0
 

Author Comment

by:mmalik15
ID: 36916307
hi gertone,

I have few changes in the above xslt as  you can see in the code part

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	
	  <xsl:variable name="lc" select="'azertyuiopqsdfghjklmwxcvbn'"/>
        <xsl:variable name="uc" select="'AZERTYUIOPQSDFGHJKLMWXCVBN'"/>
         
        
	<xsl:template match="/">
		<Records>
			<xsl:for-each select="/documents/document">
				<record>
				
					
        <xsl:call-template name="strip-tags">
			<xsl:with-param name="text" select="translate(normalize-space(substring-after(concat(element[@name='MSHValues']/value,element[@name='MTHValues']/value,element[@name='SNOMEDCTValues']/value), ':')), $uc, $lc)" />
          </xsl:call-template>
          
          
       </record>
			</xsl:for-each>
				</Records>
			  </xsl:template>
			
    <xsl:template name="process-values">
        <xsl:param name="list"/>
        <xsl:param name="remaining"/>
        <xsl:choose>
            <xsl:when test="contains($remaining, ';')">
                <xsl:variable name="new-list">
                    <xsl:call-template name="filter-and-add">
                        <xsl:with-param name="list" select="$list"/>
                        <xsl:with-param name="term" select="substring-before($remaining, ';')"/>
                    </xsl:call-template>
                </xsl:variable>
                 <xsl:call-template name="process-values">
                    <xsl:with-param name="list" select="$new-list"/>
                    <xsl:with-param name="remaining" select="substring-after($remaining, ';')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="new-list">
                    <xsl:call-template name="filter-and-add">
                        <xsl:with-param name="list" select="$list"/>
                        <xsl:with-param name="term" select="$remaining"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="substring($new-list, 3)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="filter-and-add">
        <xsl:param name="list"/>
        <xsl:param name="term"/>
        <xsl:param name="norm-term" select="normalize-space(translate($term, '()1234567890', ''))"></xsl:param>
        <xsl:choose>
            <xsl:when test="not(contains(concat($list, '; '), concat('; ', $norm-term, '; ')))">
                <xsl:value-of select="concat($list, '; ', $norm-term)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$list"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="strip-tags">
		<xsl:param name="text"/>
		<xsl:choose>
			<xsl:when test="contains($text, '&lt;')">
				<xsl:value-of select="substring-before($text, '&lt;')"/>
				<xsl:call-template name="strip-tags">
					<xsl:with-param name="text" select="substring-after($text, '&gt;')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$text"/>
			</xsl:otherwise>
		</xsl:choose>
		
		
	</xsl:template>
	
	  
		
     
</xsl:stylesheet>

Open in new window


After running this template

        <xsl:call-template name="strip-tags">
                  <xsl:with-param name="text" select="translate(normalize-space(substring-after(concat(element[@name='MSHValues']/value,element[@name='MTHValues']/value,element[@name='SNOMEDCTValues']/value), ':')), $uc, $lc)" />
          </xsl:call-template>

how can we pass the output of this template as an input to the template <xsl:template name="process-values"> as i m trying to strip html tags first before sending the data to the other template.

I thought of using a global variable but then came to know you can't replace the value of a variable in xslt          
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36925674
working my way back up, recent first, so I guess this is answered in another question
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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