带有 xsl 样式表的 XML 到固定宽度的文本文件 [英] XML to Fixed width text file with xsl style sheet

查看:21
本文介绍了带有 xsl 样式表的 XML 到固定宽度的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助使用 xsl 样式表将此 xml 格式化为固定宽度的文本文件.我对 xsl 知之甚少,在网上找到的关于如何做到这一点的信息也很少.

I need help formatting this xml to a fixed width text file using a xsl style sheet. I know very little about xsl and have found very little information online on how this can be done.

基本上我需要这个 xml

Basically I need this xml

<?xml version="1.0" encoding="UTF-8"?>
<Report>
   <table1>
      <Detail_Collection>
         <Detail>
            <SSN>*********</SSN>
            <DOB>1980/11/11</DOB>
            <LastName>user</LastName>
            <FirstName>test</FirstName>
            <Date>2013/02/26</Date>
            <Time>14233325</Time>
            <CurrentStreetAddress1>53 MAIN STREET</CurrentStreetAddress1>
            <CurrentCity>san diego</CurrentCity>
            <CurrentState>CA</CurrentState>
      </Detail_Collection>
   </table1>
</Report>

在这种格式中,都在同一行

In this format, all on the same line

*********19801111user         test       201302261423332553 MAIN STREET                                    san diego          CA

这些是固定宽度

FR TO
1   9     SSN
10  17    DOB
18  33    LastName
34  46    FirstName
47  54    Date
55  62    Time
63  90    CurrentStreetAddress1 
91  115   CurrentCity
116 131   CurrentStat

非常感谢所有帮助!提前致谢!

All help is much appreciated! Thanks in advance!

推荐答案

在 XSLT 1.0 中执行此操作的秘诀是认识到您可以将填充策略"与子字符串策略"结合起来,以填充或截断一段文本到所需的宽度.特别是这种形式的 XSLT 指令:

The secret to doing this in XSLT 1.0 is to realize that you can combine a "padding strategy" with a "substring strategy" to either pad or cut off a piece of text to a desired width. In particular, XSLT instructions of this form:

substring(concat('value to pad or cut', '       '), 1, 5)

...其中 concat 用于向字符串添加多个填充字符,substring 用于限制整体宽度,这很有帮助.话虽如此,这里有一个 XSLT 1.0 解决方案,可以完成您想要的.

...where concat is used to add a number of padding characters to a string and substring is used to limit the overall width, are helpful. With that said, here's an XSLT 1.0 solution that accomplishes what you want.

请注意,在您的预期输出中,某些字符宽度与您的要求不符;例如,根据要求, 的大小应为 16 个字符,而您的输出似乎在 13 处将其切断.也就是说,我相信我下面的解决方案会输出您期望的内容.

Please note that in your expected output, some of the character widths do not match your requirements; for example, according to the requirements, <LastName> should be sized to 16 characters, whereas your output appears to cut it off at 13. That said, I believe my solution below outputs what you expect.

当这个 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Detail">
    <xsl:apply-templates />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="SSN">
    <xsl:value-of
      select="substring(concat(., '         '), 1, 9)"/>
  </xsl:template>

  <xsl:template match="DOB">
    <xsl:value-of
      select="substring(concat(translate(., '/', ''), '        '), 1, 8)"/>
  </xsl:template>

  <xsl:template match="LastName">
    <xsl:value-of
      select="substring(concat(., '                '), 1, 16)"/>
  </xsl:template>

  <xsl:template match="FirstName">
    <xsl:value-of
      select="substring(concat(., '             '), 1, 13)"/>
  </xsl:template>

  <xsl:template match="Date">
    <xsl:value-of
      select="substring(concat(translate(., '/', ''), '        '), 1, 8)"/>
  </xsl:template>

  <xsl:template match="Time">
    <xsl:value-of
      select="substring(concat(., ' '), 1, 8)"/>
  </xsl:template>

  <xsl:template match="CurrentStreetAddress1">
    <xsl:value-of
      select="substring(concat(., '                            '), 1, 28)"/>
  </xsl:template>

  <xsl:template match="CurrentCity">
    <xsl:value-of
      select="substring(concat(., '                         '), 1, 25)"/>
  </xsl:template>

  <xsl:template match="CurrentStat">
    <xsl:value-of
      select="substring(concat(., '               '), 1, 15)"/>
  </xsl:template>

</xsl:stylesheet>

...针对提供的 XML 运行(添加了 </Detail> 以使文档格式良好):

...is run against the provided XML (with a </Detail> added to make the document well-formed):

<Report>
  <table1>
    <Detail_Collection>
      <Detail>
        <SSN>*********</SSN>
        <DOB>1980/11/11</DOB>
        <LastName>user</LastName>
        <FirstName>test</FirstName>
        <Date>2013/02/26</Date>
        <Time>14233325</Time>
        <CurrentStreetAddress1>53 MAIN STREET</CurrentStreetAddress1>
        <CurrentCity>san diego</CurrentCity>
        <CurrentState>CA</CurrentState>
      </Detail>
    </Detail_Collection>
  </table1>
</Report>

...产生了想要的结果:

*********19801111user            test         201302261423332553 MAIN STREET              san diego                CA

这篇关于带有 xsl 样式表的 XML 到固定宽度的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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