XSLT带有文本输出中的所有选项卡



愚蠢,简单的问题。当我输出文本时,它仍然根据我的格式/缩进的XSL结构获取选项卡。我如何指示变压器忽略样式表中的间距,同时保持整齐的格式?

            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:template match="/">
        <xsl:apply-templates select="Foo/Bar"></xsl:apply-templates>
      </xsl:template>
      <xsl:template match="Bar">   
<xsl:for-each select="AAA"><xsl:for-each select="BBB"><xsl:value-of select="Label"/>|<xsl:value-of select="Value"/><xsl:text>&#10;</xsl:text></xsl:for-each></xsl:for-each>
</xsl:template>
      </xsl:stylesheet>

逐行产生输出,没有选项卡:

SomeLabel|SomeValue
SomeLabel|SomeValue
SomeLabel|SomeValue

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:apply-templates select="Foo/Bar"></xsl:apply-templates>
  </xsl:template>
  <xsl:template match="Bar">   
    <xsl:for-each select="AAA">   
        <xsl:for-each select="BBB">   
            <xsl:value-of select="Label"/>|<xsl:value-of select="Value"/>
             <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
  </xsl:stylesheet>

用选项卡产生输出:

SomeLabel|SomeValue
    SomeLabel|SomeValue
    SomeLabel|SomeValue

更新:添加此问题无法解决:

<xsl:output method="text" indent="no"/>
  <xsl:strip-space elements="*"></xsl:strip-space> 

这是人为的,但是您可以想象XML看起来像这样:

<Foo>
  <Bar>
    <AAA>
      <BBB>
        <Label>SomeLabel1</Label>
        <Value>SomeValue1</Value>
      </BBB>
      <BBB>    
        <Label>SomeLabel2</Label>
        <Value>SomeValue2</Value>
      </BBB>
      <BBB>
        <Label>SomeLabel3</Label>
        <Value>SomeValue3</Value>
      </BBB>
    </AAA>
  </Bar>
</Foo>

您可以尝试将所有当前文本节点包装在 XSL:text 中。例如,尝试此

  <xsl:for-each select="BBB">
    <xsl:value-of select="Label"/>
     <xsl:text>|</xsl:text>
     <xsl:value-of select="Value"/>
     <xsl:text>|</xsl:text>
  </xsl:for-each>

另外,您可以使用 concat 函数。

  <xsl:for-each select="BBB">
    <xsl:value-of select="concat(Label, '|')"/>
    <xsl:value-of select="concat(Value, '|')"/>
  </xsl:for-each>

如果您想要

  <xsl:for-each select="BBB">
    <xsl:value-of select="concat(Label, '|', Value, '|')"/>
  </xsl:for-each>

编辑:如果您不想多次输入分隔符|,则使用模板匹配来输出归档。首先,用替换 apply-templates so so

    <xsl:for-each select="BBB">   
        <xsl:apply-templates select="Label"/>
        <xsl:apply-templates select="Value"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>

然后,您将拥有一个特定的模板可以匹配标签,您不需要输出分离器,而另一个更通用的模板匹配 bbb >/p>

<xsl:template match="BBB/Label" priority="1">
   <xsl:value-of select="." />
</xsl:template>
<xsl:template match="BBB/*">
   <xsl:text>|</xsl:text><xsl:value-of select="." />
</xsl:template>

优先级需要确保标签与第一个模板匹配,而不是一般模板)。当然,在这种情况下,您也不能在 label 上进行 apply-templates ,而只是 XSL:value-of of 为此。

此外,如果字段按以XML出现的顺序输出,则可以简化 for-east 仅为此

    <xsl:for-each select="BBB">   
        <xsl:apply-templates />
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>

最新更新