Solved

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

Posted on 2011-09-27
14
588 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
Comment Utility
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
Comment Utility
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
Comment Utility
need out put like this

<msubjectgen>
long-term care; pressure ulcer; wound healing; tissue repair
</msubjectgen>
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
http://my.safaribooksonline.com/book/xml/0596000537
is an excellent book, available through safari online
0
IT, Stop Being Called Into Every Meeting

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!

 

Author Comment

by:mmalik15
Comment Utility
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
Comment Utility

<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
Comment Utility
$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
Comment Utility
Many thanks buddy
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome my friend
0
 

Author Comment

by:mmalik15
Comment Utility
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
Comment Utility
working my way back up, recent first, so I guess this is answered in another question
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Importing QRDA file 2 45
XML template problem 4 58
Parse data from XMLtype data in Oracle SQL 5 69
surrogate key in database world 6 72
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

13 Experts available now in Live!

Get 1:1 Help Now