2

我有一些用标签替换换行符的 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 引擎。

4

1 回答 1

2

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

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

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

于 2012-11-28T12:05:49.357 回答