XSLT - 从模板中删除空格 [英] XSLT - remove whitespace from template

查看:24
本文介绍了XSLT - 从模板中删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XML 来存储一个小的联系人列表,并尝试编写一个 XSL 模板,将其转换为 CSV 文件.我遇到的问题是输出中有空格.

I am using XML to store a small contact list and trying to write a XSL template that will transform it into a CSV file. The problem I am having is with whitespace in the output.

输出:

Friend, John, Smith, Home,
        123 test,
       Sebastopol,
       California,
       12345,
     Home 1-800-123-4567, Personal john.smith@gmail.com

我对源 XML 文件和相关的 XSL 模板都进行了缩进/间隔,以使其更易于阅读和开发,但所有额外的空白都将自己放入输出中.XML 本身在节点内部没有额外的空白,只是在它们外部用于格式化,XSLT 也是如此.

I have indented/spaced both the source XML file and the associated XSL Template to make it easier to read and develop, but all that extra white space is getting itself into the output. The XML itself doesn't have extra whitespace inside the nodes, just outside of them for formatting, and the same goes for the XSLT.

为了使 CSV 文件有效,每个条目都需要在自己的行上,而不是分开.除了从 XML 和 XSLT 中去除所有多余的空格(使它们只是一行长代码)之外,还有其他方法可以去除输出中的空格吗?

In order for the CSV file to be valid, each entry needs to be on it's own line, not broken up. Besides stripping all extra white space from the XML and XSLT (making them just one long line of code), is there another way to get rid of the whitespace in the output?

这是一个小的 XML 示例:

Here is a small XML sample:

<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>&#10;&#13;</xsl:text>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

推荐答案

在 XSLT 中,默认情况下保留空白,因为它很可能是相关数据.

In XSLT, white-space is preserved by default, since it can very well be relevant data.

防止输出中出现不需要的空白的最好方法不是一开始就创建它.不要这样做:

The best way to prevent unwanted white-space in the output is not to create it in the first place. Don't do:

<xsl:template match="foo">
  foo
</xsl:template>

因为从处理器的角度来看,这是 "\n··foo\n".不如做

because that's "\n··foo\n", from the processor's point of view. Rather do

<xsl:template match="foo">
  <xsl:text>foo</xsl:text>
</xsl:template>

样式表中的空白只要出现在 XML 元素之间就会被忽略.简单地说:永远不要在 XSLT 代码的任何地方使用裸"文本,始终将其包含在一个元素中.

White-space in the stylesheet is ignored as long as it occurs between XML elements only. Simply put: never use "naked" text anywhere in your XSLT code, always enclose it in an element.

另外,使用非特定:

<xsl:apply-templates />

是有问题的,因为文本节点的默认 XSLT 规则是将它们复制到输出".这也适用于仅空白"节点.例如:

is problematic, because the default XSLT rule for text nodes says "copy them to the output". This applies to "white-space-only" nodes as well. For instance:

<xml>
  <data> value </data>
</xml>

包含三个文本节点:

  1. "\n··"(紧跟在 之后)
  2. "·值·"
  3. "\n" (就在 之前)
  1. "\n··" (right after <xml>)
  2. "·value·"
  3. "\n" (right before </xml>)

为了避免 #1 和 #3 潜入输出(这是不需要空格的最常见原因),您可以通过声明一个空模板来覆盖文本节点的默认规则:

To avoid that #1 and #3 sneak into the output (which is the most common reason for unwanted spaces), you can override the default rule for text nodes by declaring an empty template:

<xsl:template match="text()" />

现在所有文本节点都被静音,必须明确创建文本输出:

All text nodes are now muted and text output must be created explicitly:

<xsl:value-of select="data" />

要从值中删除空格,您可以使用 normalize-space() XSLT 函数:

To remove white-space from a value, you could use the normalize-space() XSLT function:

<xsl:value-of select="normalize-space(data)" />

但是要小心,因为该函数会标准化字符串中找到的任何空格,例如"·value··1·" 将变成 "value·1".

But careful, since the function normalizes any white-space found in the string, e.g. "·value··1·" would become "value·1".

此外,您可以使用 <xsl:strip-space> 元素,尽管通常这不是必需的(并且个人,我更喜欢如上所示的显式空白处理).

Additionally you can use the <xsl:strip-space> and <xsl:preserve-space> elements, though usually this is not necessary (and personally, I prefer explicit white-space handling as indicated above).

这篇关于XSLT - 从模板中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆