Link to home
Start Free TrialLog in
Avatar of mmalik15
mmalik15

asked on

how to merge and seperate different entries with a text using xslt

In the attached xml i have a requirement to save the Contents of  <keywords> and <groups> in msubjectgen node. Also separate contents of 2 fields with a semicolon if one doesn't already exist. e.g. "<keywords>Learning methods; Teaching methods; Schoolchildren; Students; Science Education;</keywords>
<groups>Education</groups>"

should be like
"Learning methods; Teaching methods; Schoolchildren; Students; Science Education; Education"

I have tried xslt attached for msubjectgen but it does not seem to work


<?xml version="1.0" encoding="UTF-8"?>
<projects>
	<project id="195">
		<title>The use of virtual worlds for effective education</title>
		<url>http://www.campbellcollaboration.org/lib/project/195/</url>
		<published_authors>Stewart Martin</published_authors>
		<publishing_date>2011-10-18</publishing_date>
		<keywords/>
		<groups>Education</groups>
		<phase>title_proposal</phase>
		<files>
			<file id="1513">
				<title>Martin_Virtual_Worlds_Title.pdf</title>
				<url>http://www.campbellcollaboration.org/lib/download/1513/</url>
				<filename>Martin_Virtual_Worlds_Title.pdf</filename>
				<filesize>78729</filesize>
				<project_phase>title_proposal_publishing</project_phase>
				<type>title_proposal</type>
				<mime_type>application/pdf</mime_type>
				</file>
		</files>
	</project>
	
	<project id="171">
		<title>The effect of benefit exhaustion on employment</title>
		<url>http://www.campbellcollaboration.org/lib/project/171/</url>
		<published_authors>Trine Filges, Lars Pico Geerdsen, Anne-Marie Jørgensen, Krystyna Kowalski, Anne-Sofie  Due Knudsen</published_authors>
		<publishing_date>2011-10-14</publishing_date>
		<keywords>Unemployment benefit; Employment status; </keywords>
		<groups>Social Welfare</groups>
		<phase>new</phase>
		<files>
			<file id="1507">
				<title>Filges_Benefit_Exhaustion_Protocol.pdf</title>
				<url>http://www.campbellcollaboration.org/lib/download/1507/</url>
				<filename>Filges_Benefit_Exhaustion_Protocol.pdf</filename>
				<filesize>248238</filesize>
				<project_phase>protocol_publishing</project_phase>
				<type>protocol</type>
				<mime_type>application/pdf</mime_type>
				</file>
		</files>
	</project>
	
	<project id="179">
		<title>The effect of benefit exhaustion on employment</title>
		<url>http://www.campbellcollaboration.org/lib/project/171/</url>
		<published_authors>Trine Filges, Lars Pico Geerdsen, Anne-Marie Jørgensen, Krystyna Kowalski, Anne-Sofie  Due Knudsen</published_authors>
		<publishing_date>2011-10-14</publishing_date>
		<keywords>Unemployment benefit; Employment status; </keywords>
		<groups>Social Welfare</groups>
		<phase>protocol</phase>
		<files>
			<file id="1507">
				<title>Filges_Benefit_Exhaustion_Protocol.pdf</title>
				<url>http://www.campbellcollaboration.org/lib/download/1507/</url>
				<filename>Filges_Benefit_Exhaustion_Protocol.pdf</filename>
				<filesize>248238</filesize>
				<project_phase>protocol_publishing</project_phase>
				<type>protocol</type>
				<mime_type>application/pdf</mime_type>
				</file>
		</files>
	</project>
	</projects

Open in new window

<?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:template match="/">
		<Records>
			<xsl:for-each select="/projects/project[not(normalize-space(phase) = 'new')]">
				<!-- do your stuff here -->
				<record>
					<mdbid>
						<xsl:value-of select="@id"/>
					</mdbid>
					<title>
						<xsl:value-of select="title"/>
					</title>
					<xsl:for-each select="published_authors">
						<mauthorpersons>
							<xsl:value-of select="normalize-space(translate(.,',',';'))"/>
						</mauthorpersons>
					</xsl:for-each>
					<mdatepublished>
						<xsl:value-of select="substring(publishing_date,1,4)"/>
					</mdatepublished>
					<mdatepublishedfull>
						<xsl:call-template name="formatDate">
							<xsl:with-param name="date" select="publishing_date"/>
						</xsl:call-template>
					</mdatepublishedfull>
					<mrtype>
						<xsl:value-of select="phase"/>
					</mrtype>
					<msubjectgen>
						<xsl:apply-templates select="keywords"/>
						<xsl:value-of select="groups"/>
					</msubjectgen>
				</record>
			</xsl:for-each>
		</Records>
	</xsl:template>
	<xsl:template name="formatDate">
		<xsl:param name="date"/>
		<xsl:variable name="year" select="substring-before($date, '-')"/>
		<xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')"/>
		<xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')"/>
		<xsl:value-of select="concat($day, '/', $month, '/', $year)"/>
	</xsl:template>
	<xsl:template match="keywords">
				<xsl:value-of select="substring(., 1, string-length(.) - 1)"/>
		<xsl:value-of select="translate(substring(., string-length(.)), ';', '')"/>
		<xsl:text>;</xsl:text>
	</xsl:template>
</xsl:stylesheet>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmalik15
mmalik15

ASKER

Thanks Gertone. Works perfectly as always. Could you please explain me how the following two lines work? thanks

<xsl:value-of select="substring($keyw-norm, 1, string-length($keyw-norm) - 1)"/>
<xsl:value-of select="translate(substring($keyw-norm, string-length($keyw-norm)), ';', '')"/>
I just copied them from your original
instead of '.' I used a normalized form of '.' stored in variable $keyw-norm

first line takes all char but the last
second line removes the last char if a ';'
the third line puts the ';' there, but on the condition that the variable si not empty (as in teh first example)
substring function in the 2nd line is taking  two parameters usually it carries three parameters may  be thats confusing me a bit.

thanks
welcome

if substring lacks a third argument, it means "to the end of string"