[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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

0
mmalik15
Asked:
mmalik15
  • 3
  • 3
1 Solution
 
Geert BormansCommented:
slightly altered this,
you were doing good, but some spaces came in between :-)
<?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"/>
                    </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:variable name="keyw-norm" select="normalize-space(.)"/>
        <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)), ';', '')"/>
        <xsl:if test="normalize-space($keyw-norm)">
            <xsl:text>;</xsl:text>
        </xsl:if>
        <xsl:if test="normalize-space(../groups)">
            <xsl:text> </xsl:text>
            <xsl:value-of select="../groups"/>
        </xsl:if>
        
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
mmalik15Author Commented:
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)), ';', '')"/>
0
 
Geert BormansCommented:
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)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mmalik15Author Commented:
substring function in the 2nd line is taking  two parameters usually it carries three parameters may  be thats confusing me a bit.

0
 
mmalik15Author Commented:
thanks
0
 
Geert BormansCommented:
welcome

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

Featured Post

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now