使用xsl样式表的XML到固定宽度的文本文件
发布时间:2020-12-16 05:36:38 所属栏目:百科 来源:网络整理
导读:我需要帮助使用xsl样式表将此xml格式化为固定宽度的文本文件.我对xsl知之甚少,并且在网上找到的关于如何做到这一点的信息非常少. 基本上我需要这个xml ?xml version="1.0" encoding="UTF-8"?Report table1 Detail_Collection Detail SSN*********/SSN DOB198
我需要帮助使用xsl样式表将此xml格式化为固定宽度的文本文件.我对xsl知之甚少,并且在网上找到的关于如何做到这一点的信息非常少.
基本上我需要这个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> 在这种格式中,所有都在同一行 *********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 非常感谢所有帮助!
在XSLT 1.0中执行此操作的秘诀在于,您可以将“填充策略”与“子字符串策略”结合起来,将一段文本填充或剪切到所需的宽度.特别是,这种形式的XSLT指令:
substring(concat('value to pad or cut',' '),1,5) …其中concat用于向字符串添加多个填充字符,子字符串用于限制整体宽度,这是有帮助的.话虽如此,这是一个XSLT 1.0解决方案,可以实现您的需求. 请注意,在您的预期输出中,某些字符宽度与您的要求不符;例如,根据要求,< LastName>应该大小为16个字符,而你的输出似乎在13处切断它.那说,我相信我的解决方案输出你期望的. 当这个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> </xsl:text> </xsl:template> <xsl:template match="SSN"> <xsl:value-of select="substring(concat(.,' '),9)"/> </xsl:template> <xsl:template match="DOB"> <xsl:value-of select="substring(concat(translate(.,'/',''),' '),8)"/> </xsl:template> <xsl:template match="LastName"> <xsl:value-of select="substring(concat(.,' '),16)"/> </xsl:template> <xsl:template match="FirstName"> <xsl:value-of select="substring(concat(.,' '),13)"/> </xsl:template> <xsl:template match="Date"> <xsl:value-of select="substring(concat(translate(.,8)"/> </xsl:template> <xsl:template match="Time"> <xsl:value-of select="substring(concat(.,' '),8)"/> </xsl:template> <xsl:template match="CurrentStreetAddress1"> <xsl:value-of select="substring(concat(.,' '),28)"/> </xsl:template> <xsl:template match="CurrentCity"> <xsl:value-of select="substring(concat(.,' '),25)"/> </xsl:template> <xsl:template match="CurrentStat"> <xsl:value-of select="substring(concat(.,' '),15)"/> </xsl:template> </xsl:stylesheet> …针对提供的XML运行(添加< / Detail>以使文档格式正确): <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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |