XSLT:避免某些节点中的缩进

标签 xslt xslt-1.0 indentation

我有一些 XSLT 将换行符替换为 <Break/>标签,只要没有多个连续的换行符,它就可以正常工作。我认为是indent="yes"这会造成问题。 可以对某些节点禁用它吗?

基本上,具有混合内容(文本和元素)的节点不能包含任何换行符。

输入xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Account xmlns="http://example.com/account">
    <Owner>
        <ID>012345789</ID>
        <Name>Peter Johnson</Name>
    </Owner>
    <Notes>
        <NoteID>012345789</NoteID>
        <Text>This is the description:
Line 1
Line 2
Line 3

Line 4, after double linebreak
Line 5</Text>
    </Notes>
</Account>

XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://example.com/account" version="1.0">

<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>

<xsl:template name="replace_sab">
    <!-- with string s, replace substring a by string b -->
    <!-- s, a and b are parameters determined upon calling  -->
    <xsl:param name="s" />
    <xsl:param name="a" />
    <xsl:param name="b" />
    <xsl:choose>
        <xsl:when test="contains($s,$a)">
            <xsl:value-of select="substring-before($s,$a)" />
            <xsl:copy-of select="$b" />
            <xsl:call-template name="replace_sab">
                <xsl:with-param name="s" select="substring-after($s,$a)" />
                <xsl:with-param name="a" select="$a" />
                <xsl:with-param name="b" select="$b" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$s" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="text()[not(normalize-space())]"/>

  <xsl:template match="text()[boolean(normalize-space())]">
    <xsl:call-template name="replace_sab">
        <xsl:with-param name="s" select="." />
        <xsl:with-param name="a" select="'&#xA;'" />
        <xsl:with-param name="b"><Break/></xsl:with-param>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

我得到的输出:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Account xmlns="http://example.com/account">
    <Owner>
        <ID>012345789</ID>
        <Name>Peter Johnson</Name>
    </Owner>
    <Notes>
        <NoteID>012345789</NoteID>
        <Text>This is the description:<Break/>Line 1<Break/>Line 2<Break/>Line 3<Break/>
            <Break/>Line 4, after double linebreak<Break/>Line 5</Text>
    </Notes>
</Account>

我想要的输出:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Account xmlns="http://example.com/account">
    <Owner>
        <ID>012345789</ID>
        <Name>Peter Johnson</Name>
    </Owner>
    <Notes>
        <NoteID>012345789</NoteID>
        <Text>This is the description:<Break/>Line 1<Break/>Line 2<Break/>Line 3<Break/><Break/>Line 4, after double linebreak<Break/>Line 5</Text>
    </Notes>
</Account>

我在 Tibco BusinessWorks 流程中使用“TIBCO XSLT 1.0”XSLT 引擎。

最佳答案

没有标准方法可以做到这一点。

如果您使用 Saxon,则可以使用 saxon:suppress-indentation 输出参数,该参数成为 XSLT 3.0 中的标准选项。

即使您坚持使用 Tibco XSLT 引擎,也许您也可以找到一种将 Saxon 序列化器插入处理管道的方法。

关于XSLT:避免某些节点中的缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601731/

相关文章:

html - 如何在 XSL 生成的文件中包含正确的 XML/HTML 定义?

xslt - 如何在 xsl 模板中匹配这个或那个?

xslt - 在XPath 1.0中,如何测试长度为1的字符串是否落在两个其他代码点之间?

vim - ERB 文件缩进被 .rb 设置覆盖

xslt 转换分数

xml - 编辑 SharePoint 2010 Rss Feed WebPart 的 XSL

xml - For 循环与应用模板

java - 从 XSL 访问 Java 公共(public)静态

java - 如何在不使用数组或任何正则表达式的情况下缩进包含特殊关键字的文本文件中的文本

php - PHP中的切换/中断缩进