我已经开始使用Camel进行spring boot,使用xsl进行新项目。我有以下示例:
students.xml
<?xml version="1.0" encoding="utf-8"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
使用以下xsl进行转换(忽略注释; xsl的新内容):
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="*"><!-- matches any node -->
<xsl:copy><!-- copies the current node and namespace nodes,NOT CHILD OR ATTRIBUTE NODES -->
<xsl:apply-templates select="student" /><!-- applies template to students elements -->
</xsl:copy>
</xsl:template>
<xsl:template match="student">
<xsl:element name="person"><!-- renames element to person -->
<xsl:attribute name="id-num"><!-- applies an attribute to new element called id-num -->
<xsl:value-of select="@rollno" /><!-- copies value of student eles rollno attribute -->
</xsl:attribute>
<xsl:apply-templates select="lastname" /><!-- applies template to lastname child nodes -->
<xsl:apply-templates select="marks" /><!-- applies template to marks child nodes -->
</xsl:element>
</xsl:template>
<xsl:template match="lastname">
<xsl:element name="student-surname">
<xsl:attribute name="nick-name">
<xsl:value-of select="../nickname" /><!-- moves up a level in xml tree and copies value from nickname element -->
</xsl:attribute>
<xsl:value-of select="." /><!-- copies text value of current node -->
</xsl:element>
</xsl:template>
<xsl:template match="marks">
<xsl:element name="grade">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我通过Camel进行这种转换。路由配置如下(camel spring boot starter,版本2.17.3):
SampleCamelRouter.java
@Component
public class SampleCamelRouter extends RouteBuilder {
/* (non-Javadoc)
* @see org.apache.camel.builder.RouteBuilder#configure()
*/
@Override
public void configure() throws Exception {
// TODO Auto-generated method stub
from("file:src/main/resources/input/?fileName=students.xml&noop=true")
.to("xslt:./transforms/students.xsl")
.to("file:target/messages/?fileName=studentsout.xml");
}
}
每当我运行应用程序时,转换都会发生在创建新元素,剥离其他元素,复制值等方面。但它不会缩进,尽管xsl:输出行靠近文件的开头。我还尝试在之前的运行中添加xsl:strip-space标记,但它不会影响输出。我在运行转换时得到的结果:
学生-的Output.xml
<?xml version="1.0" encoding="UTF-8"?><class>
<person id-num="393">
<student-surname nick-name="Dinkar">Kad</student-surname>
<grade>85</grade>
</person>
<person id-num="493">
<student-surname nick-name="Vinni">Gupta</student-surname>
<grade>95</grade>
</person>
<person id-num="593">
<student-surname nick-name="Jazz">Singh</student-surname>
<grade>90</grade>
</person>
</class>
有什么我想念的吗?模板是否会干扰缩进?
答案 0 :(得分:1)
尝试更改:
<xsl:output method="xml" indent="yes" />
为:
<xsl:output method="xml" indent="yes" xalan:indent-amount="2" xmlns:xalan="http://xml.apache.org/xalan"/>