Bí quyết để thực hiện điều này trong XSLT 1.0 là nhận ra rằng bạn có thể kết hợp "chiến lược đệm" với "chiến lược chuỗi con" cho một trong hai hoặc cắt một đoạn văn bản thành chiều rộng mong muốn. Đặc biệt, hướng dẫn XSLT của mẫu này:
substring(concat('value to pad or cut', ' '), 1, 5)
... nơi concat
được sử dụng để thêm một số ký tự đệm để một chuỗi và substring
được sử dụng để hạn chế chiều rộng tổng thể, rất hữu ích. Như đã nói, đây là giải pháp XSLT 1.0 hoàn thành những gì bạn muốn.
Xin lưu ý rằng trong kết quả mong đợi của bạn, một số chiều rộng ký tự không phù hợp với yêu cầu của bạn; ví dụ, theo yêu cầu, <LastName>
phải được kích thước thành 16 ký tự, trong khi đầu ra của bạn dường như cắt nó ở 13. Điều đó nói rằng, tôi tin rằng giải pháp của tôi dưới đây xuất ra những gì bạn mong đợi.
Khi XSLT này:
<?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(., ' '), 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>
... được điều hành so với XML cung cấp (với một </Detail>
thêm để làm cho tài liệu cũng như hình thành):
<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>
... kết quả mong muốn được tạo ra:
*********19801111user test 201302261423332553 MAIN STREET san diego CA
Bạn có thể sử dụng tiện ích mở rộng như nút-set() hoặc tài liệu/tệp xml phụ để giữ chiều rộng và thứ tự của đầu ra không? –