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

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

mmalik15Asked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:

<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
 
Geert BormansInformation ArchitectCommented:
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
 
mmalik15Author Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mmalik15Author Commented:
need out put like this

<msubjectgen>
long-term care; pressure ulcer; wound healing; tissue repair
</msubjectgen>
0
 
Geert BormansInformation ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
mmalik15Author Commented:
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
 
Geert BormansInformation ArchitectCommented:
http://my.safaribooksonline.com/book/xml/0596000537
is an excellent book, available through safari online
0
 
mmalik15Author Commented:
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
 
Geert BormansInformation ArchitectCommented:
$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
 
mmalik15Author Commented:
Many thanks buddy
0
 
Geert BormansInformation ArchitectCommented:
welcome my friend
0
 
mmalik15Author Commented:
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
 
Geert BormansInformation ArchitectCommented:
working my way back up, recent first, so I guess this is answered in another question
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.