本文介绍了如何将 XSLT 中的文本(元素的属性)格式化为一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可以格式化xslt文件的函数.我认为他可能会在一开始的某个地方使用.我的问题是什么?

Is there any function that can format xslt file. I think he'll probably use somewhere at the beginning. What is my problem?

我创建了基本的 xslt:

I created basic xslt:

 ...<xsl:template name="model"  match="uml:Model">
        <xsl:element name="uml:Model">
            <xsl:attribute name="xmi:type">
              <xsl:value-of select="@xmi:type"/>
            </xsl:attribute>
            <xsl:attribute name="name">
              <xsl:value-of select="@name" />
            </xsl:attribute>
            <xsl:attribute name="visibility">
              <xsl:value-of select="@visibility" />
            </xsl:attribute>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="packagedElement[@xmi:type='uml:Package']">
        <xsl:element name="packagedElement">
            <xsl:attribute name="xmi:type">uml:Package</xsl:attribute>
            <xsl:attribute name="xmi:id">
                <xsl:value-of select="@xmi:id"></xsl:value-of>
            </xsl:attribute>
            <xsl:attribute name="name">sequenceD</xsl:attribute>
            <xsl:attribute name="visibility">public</xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>...

在第一个模板中,一切都很好,元素的属性在一行中.在每个下一个元素中,在自己的行中都有属性.我不知道为什么.这是 XML 输出:

In first template, everything is fine, attributes of element are in one row. In every next element there are attributes in own line. I don´t know why. This is XML output:

<uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
      <packagedElement xmi:type="uml:Package"
                       xmi:id="EAPK_9E00FA41_6F28_4e47_9AE6_2A04EE92CDBB"
                       name="sequenceD"
                       visibility="public"/>
</uml:Model>

这就是我所期待的:

   <uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
      <packagedElement xmi:type="uml:Package" xmi:id="EAPK_9E00FA41_6F28_4e47_9AE6_2A04EE92CDBB" name="sequenceD" visibility="public">
         <packagedElement xmi:type="uml:Collaboration" xmi:id="EAID_AC000001_6F28_4e47_9AE6_2A04EE92CDBB" name="EA_Collaboration1" visibility="public">
         </packageElement>
      </packageElement>
   </uml:Model>

我也尝试像在第二个模板中一样将自己的文本添加到第一个模板,但结果相同.在 xml 中,第一个元素在一行中具有属性.请问有什么帮助吗?谢谢.

I also try to add own text to first template as in second template but with the same result. In xml first element has attributes in one row. Any help, please? Thank you.

推荐答案

如果您使用 Saxon 并指定 indent="yes",那么如果行长度超过某个阈值,则属性将拆分为多行以提高可读性:indent="yes" 声明的目的是为了让人类更容易阅读输出,需要水平滚动的长行通常被认为导致可读性差.

If you're using Saxon, and specifying indent="yes", then attributes are split onto multiple lines for readability if the line length would otherwise exceed some threshold: the stated purpose of indent="yes" is to make it easier for humans to read the output, and long lines requiring horizontal scrolling are generally considered to result in poor readability.

Saxon(PE 和更高版本)允许您使用 saxon:line-length 序列化属性控制最大行长度,您可以在 xsl:output 旁边指定该属性indent="yes".

Saxon (PE and higher) allows you to control the maximum line length using the saxon:line-length serialization property, which you can specify on xsl:output alongside indent="yes".

这篇关于如何将 XSLT 中的文本(元素的属性)格式化为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:21