【问题标题】:Help on converting XML to positional text file without stripping spaces帮助将 XML 转换为位置文本文件而不去除空格
【发布时间】:2011-07-21 13:49:53
【问题描述】:

我正在尝试使用 XML 创建位置文件。我创建了 XSLT 并且工作正常,除非该字段充满空格。在这种情况下,XSL 只返回一个空格。我正在使用 MSXML (6.0)。

我尝试了以下方法,但没有成功:

<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="*"/>

<fo:block white-space-collapse="false" white-space-treatment="preserve" >
    <!-- Code here -->
</fo:block>

这是 XML 输入、XSLT 和输出。

<Document>
      <Header>
        <Title>Long life to the queen   </Title>
        <Author>Sam Catnip     </Author>
        <Year>1996</Year>
        <Edition>  1</Edition>
        <Price>          12.99</Price>
        <Pages>    1244</Pages>
        <AuthorNotes>                    </AuthorNotes>
        <Abstract>It is a great book  </Abstract>
    </Header>
      <Header>
        <Title>Life and live longer     </Title>
        <Author>Bill Griffin   </Author>
        <Year>2001</Year>
        <Edition>  1</Edition>
        <Price>           2.99</Price>
        <Pages>      44</Pages>
        <AuthorNotes>Yeah, right         </AuthorNotes>
        <Abstract>Wishfull thinking    </Abstract>
    </Header>
</Document>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="text"/>
    <xsl:template match="//Document">
        <xsl:for-each select="./Header">
            <xsl:value-of select="./Title"/>
            <xsl:value-of select="./Author"/>
            <xsl:value-of select="./Year"/>
            <xsl:value-of select="./Edition"/>
            <xsl:value-of select="./Price"/>        
            <xsl:value-of select="./Pages"/>
            <xsl:value-of select="./AuthorNotes"/>
            <xsl:value-of select="./Abstract"/>
            <xsl:text>&#13;&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输出:

Long life to the queen   Sam Catnip     1996  1          12.99    1244It is a great book  
Life and live longer     Bill Griffin   2001  1           2.99      44Yeah, right         Wishfull thinking    

什么时候应该:

Long life to the queen   Sam Catnip     1996  1          12.99    1244                    It is a great book  
Life and live longer     Bill Griffin   2001  1           2.99      44Yeah, right         Wishfull thinking    

我将非常感谢有关如何解决此问题的任何想法。

谢谢,

艺术

【问题讨论】:

  • 您的模板产生所需的输出 (MSXML 6.0)

标签: xml xslt text


【解决方案1】:

即使您的 XSLT 包含保留空白的指令,当您的输入 XML 文档在被传递到您的 XSLT 之前被解析时,它也可能没有被保留。

您没有指定您使用的语言/平台或任何内容,因此很难提供具体的解决方案,但如果您阅读这样的 XML 文档,我知道在 C# 中:

string xmlSource = @"<Document>etc..</Document>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlSource);

它已经将任何只有空格的元素视为空元素,并且在您尝试应用样式表之前,doc 已经删除了该空间。

在 C# 中,您需要这样做:

XmlDocument doc = new XmlDocument { PreserveWhitespace = true };

当你在加载它之前实例化它。如果您使用的是不同的平台,则需要研究您的平台是如何做到这一点的。

更通用的解决方案(虽然有点麻烦)是像这样修改您的输入 XML:

...
  <AuthorNotes xml:space="preserve">                    </AuthorNotes>
...

我认为您可以将其应用于根元素,但我不能 100% 确定这一点。

【讨论】:

  • 感谢弗林,它确实与根元素中的 一起工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多