简体   繁体   English

骆驼:xsl转换不会缩进xml

[英]Camel: xsl transformation doesn't indent xml

I have started to use Camel for spring boot and xsl for a new project. 我已经开始将Camel用于弹簧靴,将xsl用于新项目。 I have the following example: 我有以下示例:

students.xml 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>

which is transformed with the following xsl(ignore comments;new to xsl): 使用以下xsl转换(忽略注释; xsl的新功能):

students.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>

Im performing this transformation via Camel. 我通过骆驼执行此转换。 the route configuration is as follows(camel spring boot starter, version 2.17.3): 路由配置如下(骆驼弹簧启动启动器,版本2.17.3):

SampleCamelRouter.java 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");
    }

}

Whenever I run the application, the transform happens with regards to creating the new elements, stripping out the others, copying values etc,. 每当我运行该应用程序时,转换都会发生在创建新元素,删除其他元素,复制值等方面。 but it doesn't indent despite the xsl:output line near the beginning of the file. 但是,尽管文件开头附近有xsl:output行,它也不会缩进。 I have also tried adding the xsl:strip-space tag in a previous run and it didn't affect the output. 我还尝试过在先前的运行中添加xsl:strip-space标记,它不会影响输出。 What I get when I run the transform: 运行转换后得到的结果是:

students-output.xml 学生-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>

Is there something I'm missing? 有什么我想念的吗? Is a template interfering with the indentation? 模板是否影响缩进?

Try changing: 尝试更改:

<xsl:output method="xml" indent="yes" />

to: 至:

<xsl:output method="xml" indent="yes" xalan:indent-amount="2" xmlns:xalan="http://xml.apache.org/xalan"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM