Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For specific requirements of the desired algorithm, I should prepare the input in a table-like format in which it is not possible to have cells with more than one data item. Simply, for doc as follows (this is a fragment of complete doc):

HTML
<Articles>
<article>
<author>Frank Manola</author>
<title>title1</title>
<journal>GTE</journal>
<month>December</month>
<year>1991</year>
</article>

<article>
<author>Frank Manola</author>
<author>Sandra Heiler</author>
<title>title2</title>
<journal>ASF</journal>
<month>August</month>
<year>1993</year>
</article>
</Articles>


the desired output is such as follows:

Frank Manola, title1, GTE, December, 1991

Frank Manola, title2, ASF, August, 1993

Sandra Heiler, title2, ASF, August, 1993


In fact, for those records that have more than one authors (author tag) (there are instances with 4 or more ones), each one should be retrieved in a separate line.

How to do that using XSLT?

Update: This is a solution that is close to what I want:

HTML
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">(<xsl:apply-templates select=".//text()"/>)<xsl:if test="position() != last()">, </xsl:if>
</xsl:template>
<xsl:template match="text()">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()">, </xsl:if>
</xsl:template>
</xsl:transform>
Posted
Updated 2-Feb-15 2:11am
v2
Comments
_Asif_ 2-Feb-15 8:03am    
What have you tried so far?
Eilia98 2-Feb-15 8:09am    
I could find a solution that is not completely sufficient for what I want to do?
It's added as update to the question.

1 solution

Something like this should work:
XML
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="article">
        <xsl:apply-templates select="author"/>
        <xsl:if test="position() != last()">, </xsl:if>
    </xsl:template>

    <xsl:template match="author">(<xsl:apply-templates select="./text()"/>, <xsl:apply-templates select="../title/text()"/>, <xsl:apply-templates select="../journal/text()"/>, <xsl:apply-templates select="../month/text()"/>, <xsl:apply-templates select="../year/text()"/>)<xsl:if test="position() != last()">, </xsl:if></xsl:template>
</xsl:transform>
 
Share this answer
 
Comments
Eilia98 2-Feb-15 11:03am    
@Richard, Thanks for that, it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900