我使用XML来存储一个小的联系人列表,并试图写一个XSL模板,将其转化为一个CSV文件。 我遇到的问题是与输出空白。
输出:
Friend, John, Smith, Home,
123 test,
Sebastopol,
California,
12345,
Home 1-800-123-4567, Personal john.smith@gmail.com
我已经缩进/间距都源XML文件及相关的XSL模板,使其更容易阅读和发展,但所有这些额外的白色空间越来越本身到输出。 XML本身不具备节点内多余的空格,就在他们的格式化,和同样的XSLT。
为了CSV文件是有效的,每个条目必须在它自己的线,不破的。 除了从XML和XSLT剥离所有额外空白(使它们只是一个长的代码行),有另一种方式来摆脱在输出中的空白?
编辑:这是一个小的XML示例:
<PHONEBOOK>
<LISTING>
<FIRST>John</FIRST>
<LAST>Smith</LAST>
<ADDRESS TYPE="Home">
<STREET>123 test</STREET>
<CITY>Sebastopol</CITY>
<STATE>California</STATE>
<ZIP>12345</ZIP>
</ADDRESS>
<PHONE>1-800-123-4567</PHONE>
<EMAIL>john.smith@gmail.com</EMAIL>
<RELATION>Friend</RELATION>
</LISTING>
</PHONEBOOK>
这里是XSLT:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:for-each select="//LISTING">
<xsl:value-of select="RELATION" /><xsl:text>, </xsl:text>
<xsl:value-of select="FIRST" /><xsl:text>, </xsl:text>
<xsl:value-of select="LAST" /><xsl:text>, </xsl:text>
<xsl:if test="ADDRESS">
<xsl:for-each select="ADDRESS">
<xsl:choose>
<xsl:when test="@TYPE">
<xsl:value-of select="@TYPE" />,
</xsl:when>
<xsl:otherwise>
<xsl:text>Home </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="STREET" />,
<xsl:value-of select="CITY" />,
<xsl:value-of select="STATE" />,
<xsl:value-of select="ZIP" />,
</xsl:for-each>
</xsl:if>
<xsl:for-each select="PHONE">
<xsl:choose>
<xsl:when test="@TYPE">
<xsl:value-of select="@TYPE" />
</xsl:when>
<xsl:otherwise><xsl:text>Home </xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." /><xsl:text >, </xsl:text>
</xsl:for-each>
<xsl:if test="EMAIL">
<xsl:for-each select="EMAIL">
<xsl:choose>
<xsl:when test="@TYPE">
<xsl:value-of select="@TYPE" /><xsl:text > </xsl:text>
</xsl:when>
<xsl:otherwise><xsl:text >Personal </xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." /><xsl:text >, </xsl:text>
</xsl:for-each>
</xsl:if>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Answer 1:
在XSLT,空格是默认保留,因为它可以很好地将相关的数据。
以防止不必要的输出空白的最好方法是不要摆在首位创建它。 不要做:
<xsl:template match="foo">
foo
</xsl:template>
因为这是"\n··foo\n"
,从处理器的点。 宁做
<xsl:template match="foo">
<xsl:text>foo</xsl:text>
</xsl:template>
只要只有XML元素之间发生它在样式表中的空白将被忽略。 简单地说:从来没有使用“裸露”的文本中的任何地方你的XSLT代码,始终将它的元素。
此外,使用非特异性的:
<xsl:apply-templates />
是有问题的,因为文本节点的默认XSLT规则说“它们复制到输出”。 这适用于“白色空间,只有”节点以及。 例如:
<xml>
<data> value </data>
</xml>
包含三个文本节点:
-
"\n··"
右后<xml>
-
"·value·"
- “
\n"
(右前</xml>
为了避免这种情况#1和#3潜入输出(这是不需要的空间最常见的原因),你可以通过声明一个空的模板替换为文本节点的默认规则:
<xsl:template match="text()" />
所有文本节点,现在静音和文本输出必须明确创建:
<xsl:value-of select="data" />
从值中删除空格,你可以使用的normalize-space()
XSLT函数:
<xsl:value-of select="normalize-space(data)" />
但是小心,因为函数标准化字符串中发现的任何空白,如"·value··1·"
将成为"value·1"
。
此外可以使用<xsl:strip-space>
和<xsl:preserve-space>
元素,尽管通常这不是必需的(和个人来说,我更喜欢明确的空白处理如上所述)。
Answer 2:
默认情况下,XSLT模板具有<xsl:preserve-space>
集,这将保持空白在您的输出。 可以添加<xsl:strip-space elements="*">
以告诉它在何处删除空格。
您可能还需要包括一个正常化空间指令,像这样:
<xsl:template match="text()"><xsl:value-of select="normalize-space(.)"/></xsl:template>
下面是一个用于保存从W3学校/条空间实例 。
Answer 3:
至于删除标签,但保留单独的线路,我尝试以下XSLT 1.0的做法,它的工作原理相当好。 您使用的版本1.0或2.0,很大程度上取决于你使用的平台。 它看起来像.NET技术仍依赖于XSLT 1.0,所以你只限于非常混乱的模板(见下文)。 如果您使用的是Java或别的东西,请参阅向最底部列出的更清洁的XSLT 2.0方法。
这些例子是为了被你扩展,以满足您的特定需求。 我在这里使用标签作为一个例子,但是这应该是通用的,足以可扩展的。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<text>
adslfjksdaf
dsalkfjdsaflkj
lkasdfjlsdkfaj
</text>
...和XSLT 1.0模板(如果需要使用.NET):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template name="search-and-replace">
<xsl:param name="input"/>
<xsl:param name="search-string"/>
<xsl:param name="replace-string"/>
<xsl:choose>
<xsl:when test="$search-string and
contains($input,$search-string)">
<xsl:value-of
select="substring-before($input,$search-string)"/>
<xsl:value-of select="$replace-string"/>
<xsl:call-template name="search-and-replace">
<xsl:with-param name="input"
select="substring-after($input,$search-string)"/>
<xsl:with-param name="search-string"
select="$search-string"/>
<xsl:with-param name="replace-string"
select="$replace-string"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text">
<xsl:call-template name="search-and-replace">
<xsl:with-param name="input" select="text()" />
<xsl:with-param name="search-string" select="'	'" />
<xsl:with-param name="replace-string" select="''" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
XSLT 2.0使得这种琐碎的replace
功能:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="text">
<xsl:value-of select="replace(text(), '	', '')" />
</xsl:template>
</xsl:stylesheet>
Answer 4:
其他人已经指出的一般问题。 具体适合您的样式表,你忘了<xsl:text>
的逗号:
<xsl:choose>
<xsl:when test="@TYPE">
<xsl:value-of select="@TYPE" />,
</xsl:when>
<xsl:otherwise>Home </xsl:otherwise>
</xsl:choose>
<xsl:value-of select="STREET" />,
<xsl:value-of select="CITY" />,
<xsl:value-of select="STATE" />,
<xsl:value-of select="ZIP" />,
这使得空格下面每一个逗号显著,所以它在输出结束。 如果您在包裹每个逗号<xsl:text>
,问题消失。
此外,摆脱的disable-output-escaping
。 它没有做任何事情,在这里,因为你不输出XML。
Answer 5:
添加一个模板到XSLT
<xsl:template match="text()"/>
Answer 6:
我previouse答案是错的,所有的逗号必须通过标签“文”输出
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/PHONEBOOK">
<xsl:for-each select="LISTING">
<xsl:value-of select="RELATION" /><xsl:text>, </xsl:text>
<xsl:value-of select="FIRST" /><xsl:text>, </xsl:text>
<xsl:value-of select="LAST" /><xsl:text>, </xsl:text>
<xsl:for-each select="ADDRESS">
<xsl:choose>
<xsl:when test="@TYPE">
<xsl:value-of select="@TYPE" /><xsl:text>,</xsl:text>
</xsl:when>
<xsl:otherwise><xsl:text>Home </xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select="STREET/text()" /><xsl:text>,</xsl:text>
<xsl:value-of select="CITY/text()" /><xsl:text>,</xsl:text>
<xsl:value-of select="STATE/text()" /><xsl:text>,</xsl:text>
<xsl:value-of select="ZIP/text()" /><xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:for-each select="PHONE">
<xsl:choose>
<xsl:when test="@TYPE">
<xsl:value-of select="@TYPE" />
</xsl:when>
<xsl:otherwise><xsl:text>Home </xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." /><xsl:text >, </xsl:text>
</xsl:for-each>
<xsl:if test="EMAIL">
<xsl:for-each select="EMAIL">
<xsl:choose>
<xsl:when test="@TYPE">
<xsl:value-of select="@TYPE" /><xsl:text > </xsl:text>
</xsl:when>
<xsl:otherwise><xsl:text >Personal </xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." /><xsl:text >, </xsl:text>
</xsl:for-each>
</xsl:if>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()|@*">
<xsl:text>-</xsl:text>
</xsl:template>
</xsl:stylesheet>
Answer 7:
修改我们使用下面的删除线将删除导出的Excel中添加额外的空白空格来格式化原始XML文件中的代码。
虽然有齿痕属性系统格式是增加这些额外的空白空间。
注释行有关格式化XML像下面线和尝试。
xmlWriter.Formatting = System.Xml.Formatting.Indented;
文章来源: XSLT - remove whitespace from template