Formatting the output of XML transformation using

2019-08-25 22:22发布

问题:

I just started using XSLT for transforming an XML to output as test file.

Here is my XML input file:

<?xml version="1.0" ?>
<transcript>
    <student id="STU12345" name="name 1" status="active">
        <home_address>35 Wall Street, Wonderland, NJ</home_address>
        <interests>
            <interest>interest 1</interest>
            <interest>interest 2</interest>
            <interest>interest 3</interest>
        </interests>
    </student>
    <term>
        <heading name="Winter 1999" />
        <course>
            <course-name>course 1</course-name>
            <grade>A-</grade>
            <credits>4</credits>
        </course>
        <course>
            <course-name>course 2</course-name>
            <grade>B+</grade>
            <credits>3</credits>
        </course>
    </term>
    <summary>summary</summary>
    <comments>
        comments
    </comments>
</transcript>

My XML file to transform this xml is:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="no" />

    <xsl:template match="transcript">
        Student Transcript
        <xsl:apply-templates select="student" />

Course Name | Grade | Credits
        <xsl:apply-templates select="term" />
    </xsl:template>

    <xsl:template match="student">
        Name: <xsl:value-of select="@name" />
        ID: <xsl:value-of select="@id" />
    </xsl:template>

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

    <xsl:template match="course"><xsl:value-of select="course-name"/> |<xsl:value-of select="grade" /> | <xsl:value-of select="credits" />
</xsl:template>

</xsl:stylesheet>

The output that I am getting now is:

        Student Transcript

        Name: name 1
        ID: STU12345

Course Name | Grade | Credits


        course 1 |A- | 4
        course 2 |B+ | 3

But I am trying to get the output as:

        Student Transcript

        Name: name 1
        ID: STU12345

Course Name | Grade | Credits
course 1    |A-     | 4
course 2    |B+     | 3

Finally my Java program that helps me to do the transformation:

public static void main(String[] args) {
    String xml = "input.xml";
    String xslt = "input.xsl";
    String output = "output.txt";
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer tr = tf.newTransformer(new StreamSource(xslt));
        tr.transform(new StreamSource(xml), new StreamResult(
                new FileOutputStream(output)));

        System.out.println("Output to " + output);
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
}

I tried changing my XSL file multiple times to remove extra spaces and to format the output but I am not able to figure out exactly what needs to be done to get an output like this. Can someone please help me on this.

回答1:

If you are using the Xalan processor, you can take advantage of the EXSLT str:align() function, for example:

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/transcript">
    <!-- STUDENT -->
    <xsl:text>        Student Transcript&#10;        Name: </xsl:text>
    <xsl:value-of select="student/@name" />
    <xsl:text>&#10;        ID: </xsl:text>
    <xsl:value-of select="student/@id" />
    <!-- COURSES -->
    <xsl:text>&#10;&#10;Course Name | Grade | Credits&#10;</xsl:text>
    <xsl:for-each select="term/course">
        <xsl:value-of select="str:align(course-name, '            | ', 'left')" />
        <xsl:value-of select="str:align(grade, '      | ', 'left')" />
        <xsl:value-of select="credits" />
        <xsl:if test="position()!=last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Applied to your example input, this will return:

        Student Transcript
        Name: name 1
        ID: STU12345

Course Name | Grade | Credits
course 1    | A-    | 4
course 2    | B+    | 3

See a working example here: http://xsltransform.net/bdxtpM

Note: you need a non-buggy version of Xalan for this to work correctly. Otherwise you will have to write a recursive named template to do the padding, or use the substring() function to "cut" a matching piece out of a string of spaces.



标签: java xml xslt