XML – Working with XSLT then formatting the name

I’m trying to write an XSLT style sheet to handle the author’s name and create a referenced APA version. The APA citation format for the author’s name: the name lists the last name, then the initials, If the name is the first element of the citation. Use a comma to separate the names and use an ampersand (&) before the last author. I followed Dimitre Novatchev’s solution in this article: Using XSLT to select after EACH instance in a string/substring but I didn’t get the result I wanted.

Enter:


Lio-Po, Gilda D.
Primavera, Jurgenne H.
Cuvin-Aralar, Ma. Lourdes A.
Cruz, ER
Catacutan, MR
Agbayani, RF
pre>

The desired output will be: Lio-Po, GD, Primavera, JH, Cuvin-Aralar, MLA, Cruz, ER, Catacutan, MR, & Agbayani, RF

For only 2 digits Author’s record:


Lio-Po, Gilda D.
Primavera, Jurgenne H .

The expected output is: Lio-Po,GD,& Primavera,JH

Thanks in advance. Below is my code , Some of the codes are from Dimitre.







&




,






























Running the above code gives the output: Lio-Po,G,D,Primavera,J,H,Cuvin-Aralar,M,L,A,Cruz,E,Catacutan,M,& Agbayani,R< /p>

update

This is useful, I even tested it myself:< /p>






&




















,




< /xsl:choose>




The output of your given XML will be:

Lio-Po, GD, Primavera, JH, Cuvin-Aralar, MLA, Cruz, ER, Catacutan, MR , & Agbayani, RF,

If you can tolerate the last extra comma;)

Addition:
This is the complete java code I tested, and it looks pretty good .

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

import javax.xml.transform.TransformerException;

public class XLSTTester {

public static void main(String[] params){
try {
transform("d:\workspace1\test.xml","d:\workspace1\test.xsl");
} catch (Transform erException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block< br /> e.printStackTrace();
}
}

private static void transform(String xmlFileFullPath, String xsltFileFullPath) throws TransformerException, IOException{
File xmlFile = new File(xmlFileFullPath);
File xsltFile = new File(xsltFileFullPath);
StringWriter sw = new StringWriter();
javax.xml.transform.Source xmlSource = new javax.xml.transform .stream.StreamSource(xmlFile);
javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
javax.xml.transform.Result transResult = new javax .xml.transform.stream.StreamResult(sw);

// create an instance of TransformerFactory
javax.xml.transform.TransformerFactory tra nsFact = javax.xml.transform.TransformerFactory.newInstance( );

javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, transResult) ;
}

)

I'm trying to write an XSLT style sheet to handle the author's name and create a referenced APA version. Regarding the APA citation format of the author’s name: the name lists the last name, then the initials of the name, if the name is the first element of the citation. Use a comma to separate the names, and use the ampersand (&) before the last author. I’m in This article followed Dimitre Novatchev's solution: Using XSLT to select after EACH instance in a string/substring but I did not get the result I wanted.

Enter:


Lio-Po, Gilda D.
Primavera, Jurgenne H.
Cuvin-Aralar, Ma. Lourdes A.
Cruz, ER
Catacutan, MR
< author>Agbayani, RF

The desired output will be: Lio-Po,GD,Primavera,JH,Cuvin-Aralar,MLA,Cruz,ER, Catacutan,MR,& Agbayani,RF

For records with only 2 authors:


Lio-Po , Gilda D.
Primavera, Jurgenne H.

The desired output is: Lio-Po,GD,& Primavera,JH

Thanks in advance . Below is my code, some of which are from Dimitre.







&




,






























Run The above code gives the output: Lio-Po,G,D,Primavera,J,H,Cuvin-Aralar,M,L,A,Cruz,E,Catacutan,M,& Agbayani,R

Update

This is useful, I even tested it myself:






&






< br />







< br />




,









The output of your given XML will be:

Lio-Po, GD, Primavera, JH, Cuvin-Aralar, MLA, Cruz, ER, Catacutan, MR, & Agbayani, RF,

If you can tolerate the last extra comma;)

Addition:
this It is the complete java code I tested, and it looks good.

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

import javax.xml.transform.TransformerException;

public class XLSTTester {

public static void main(String[] params){
try {
transform("d:\workspace1\test.xml","d:\workspace1\test.xsl");
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto- generated catch block
e.printStackTrace();
}
}

private static void transform(String xmlFileFullPath, String xsltFileFullPath) throws TransformerException, IOException{
File xmlFile = new File(xmlFileFullPath);
File xsltFile = new File(xsltFileFullPath);
StringWriter sw = new StringWriter();
javax.xml.transform.Source xmlSource = new javax .xml.transform.stream.StreamSource(xmlFile);
javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
javax.xml.transform.Result transResult = new javax.xml .transform.stream.StreamResult(sw);

// create an instance of TransformerFactory
javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance( );

javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, transResult);
}

}

Leave a Comment

Your email address will not be published.