1

我查看了多个示例,并尝试了每个示例。不知道我错过了什么。我发现与其他示例的唯一区别是我<Line><RecordSet>.

XML:

<?xml version="1.0" encoding="utf-8"?>
<urn:FlatStructure">
  <Recordset>
    <Line> 12345678</Line>
    <Line> abcdefgh</Line>
  </Recordset>
</urn:FlatStructure>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<!-- First Trial  -->

<xsl:variable name="newline"><xsl:text>
</xsl:text></xsl:variable>

<xsl:template match="/urn:FlatStructure/RecordSet">
   <xsl:value-of select="concat(Line,$newline)" />
</xsl:template> 

<!-- Second Trial  -->
<xsl:template match="/urn:FlatStructure">
  <xsl:apply-templates select="RecordSet/Line" />
</xsl:template>

<!-- Third Trial  -->
<xsl:template match="/urn:FlatStructure">
<xsl:value-of select="concat(Line,'&#10;')" />
</xsl:template>

</xsl:stylesheet>

当前文本输出:

12345678 abcdefgh

所需的文本输出:

12345678
abcdefgh

我在 XSLT 中缺少什么?请让我知道我该如何纠正它。

谢谢

我查看了以下示例(有些可能是重复的),但没有一个对我有用:

XSLT 将 XML 转换为文本

在 XSLT 中生成新行

如何在 xslt 输出的末尾添加换行符?

不在我的 XSLT 中添加新行

4

2 回答 2

0

有什么帮助是用&#10;like替换换行变量

<xsl:variable name="newline"><xsl:text>&#10;</xsl:text></xsl:variable>

因此替换

<xsl:value-of select="concat(Line,'&#10;')" />

<xsl:value-of select="concat(Line,$newline)" />

这给出了期望的结果。

但是,您的代码有一些名称空间问题需要解决......所以将urn:名称空间添加到您的 XML

<urn:FlatStructure xmlns:urn="http://some.urn">

和这样的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:urn="http://some.urn">

然后,从XSLT 模板中urn的匹配项中删除前缀。FlatStructure

于 2016-03-03T23:07:36.313 回答
0

找到了解决方案。循环遍历<Line>下面的每个节点<Recordset>并选择文本。

有效的 XSLT:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:urn="someurn">

<xsl:output method="text" />

<xsl:template match="/urn:FlatStructure/Recordset">
    <xsl:for-each select="Line">
        <xsl:value-of select="text()"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

多个具有相同名称的子节点似乎是问题所在。

谢谢大家投稿。

干杯!

于 2016-03-04T15:35:07.020 回答