XML to Text使用XSLT转换为新行

时间:2016-03-03 22:49:32

标签: xml xslt text newline xmlconvert

我查看了多个示例,并尝试了每个示例。不知道我错过了什么。我从其他示例中发现的唯一区别是我在<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 to convert XML to text

Producing a new line in XSLT

how to add line breaks at the end of an xslt output?

not adding new line in my XSLT

2 个答案:

答案 0 :(得分:0)

使用.gameContainer替换newline变量有什么帮助,比如

&#10;

因此取代

<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)" /> 命名空间添加到XML

urn:

和像这样的XSLT

<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"> 匹配项中删除urn前缀。

答案 1 :(得分: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>

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

感谢大家投球。

干杯!