开发者

Help on converting XML to positional text file without stripping spaces

I'm trying to create a positional file out of an XML. I created the XSLT and work fine unless the field is filled with spaces. In that case the XSL returns only one space. I'm using MSXML (6.0).

I have tried the following with no luck:

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

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

Here is the XML input, the XSLT and the output.

<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开发者_C百科="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>

Output:

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    

When it should be:

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    

I will really appreciate any idea on how to solve this.

Thanks,

Arty


Even if you XSLT contains an instruction to preserve the whitespace, it might not have been preserved when your input XML document was parsed before being passed to your XSLT.

You didn't specify what language/platform or anything you're using, so it's difficult to offer a specific solution, but I know in C# if you read an XML document like this:

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

It will already have treated any elements with just space as empty elements, and doc already has that space stripped out before you even try applying a stylesheet.

In C#, you need to do this:

XmlDocument doc = new XmlDocument { PreserveWhitespace = true };

when you instantiate it before even loading it. If you're using a different platform, you'll need to research how your platform does this.

A more general solution (albeit a tad cumbersome) is to modify your input XML like this:

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

I think you can apply this to the root element, but I'm not 100% certain of that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜