-2

First sorry for probably inexact title!

Retrieving or extracting content of an XML document is a frequent question that has been answered in other posts in SO.

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):

<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?

Thanks,

Eilia
  • 11
  • 1
  • 3
  • 17
  • 1
    Its actually a simple problem - have you tried anything yourself before posting a question that you could show us? – Zyga Feb 02 '15 at 12:07
  • @Zyga, Unfortunately I don't have enough knowledge of XSLT; however my another question of such kind has been answered in this post (http://stackoverflow.com/questions/28032853/extracting-textual-content-from-xml-documents-using-xslt) – Eilia Feb 02 '15 at 13:00
  • you will not learn if you are provided full solution every time. You should start with various XSLT tutorials and take it from there. I will actually provide you an answer this time though. – Zyga Feb 02 '15 at 13:18
  • @Zyga, Thanks for kind advice and your coming answer. – Eilia Feb 02 '15 at 13:21

1 Answers1

1

The XSLT below should do what you want:

 <xsl:output method="text" />

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

    <xsl:template match="Articles">
        <xsl:for-each select="article/author">
            <xsl:value-of select="."/>, <xsl:value-of select="../title" />, <xsl:value-of select="../journal" />, <xsl:value-of select="../year" /><xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
Zyga
  • 2,367
  • 3
  • 22
  • 32