[英]Formatting the output of XML transformation using XSLT to generate the Text file
I just started using XSLT
for transforming an XML
to output as test
file. 我刚刚开始使用XSLT
将XML
转换为输出为test
文件。
Here is my XML input file: 这是我的XML输入文件:
<?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的XML文件是:
<?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: 最后,我的Java程序可以帮助我进行转换:
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. 我尝试多次更改XSL文件以删除多余的空格并格式化输出,但是我无法确切弄清楚要获得这样的输出需要做什么。 Can someone please help me on this. 有人可以帮我吗
If you are using the Xalan processor, you can take advantage of the EXSLT str:align() function, for example: 如果使用Xalan处理器,则可以利用EXSLT str:align()函数,例如:
XSLT 1.0 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 Name: </xsl:text>
<xsl:value-of select="student/@name" />
<xsl:text> ID: </xsl:text>
<xsl:value-of select="student/@id" />
<!-- COURSES -->
<xsl:text> Course Name | Grade | Credits </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> </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 在此处查看工作示例: http : //xsltransform.net/bdxtpM
Note: you need a non-buggy version of Xalan for this to work correctly. 注意:您需要非越野车版本的Xalan才能正常工作。 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. 否则,您将必须编写一个递归命名模板来进行填充,或使用substring()函数从空格字符串中“切出”匹配的块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.