Sekretem tego w XSLT 1.0 jest uświadomienie sobie, że można połączyć "strategię wypełniania" ze "strategią podłańcuchową", aby wydzielić lub odciąć fragment tekstu na pożądaną szerokość. W szczególności, instrukcje XSLT tej formy:
substring(concat('value to pad or cut', ' '), 1, 5)
... concat
gdzie stosuje się, aby dodać liczbę znaków wypełniających do łańcucha i substring
służy do ograniczenia całkowitej szerokości, są pomocne. Mając to na uwadze, oto rozwiązanie XSLT 1.0, które zapewnia to, czego oczekujesz.
Należy pamiętać, że w oczekiwanym wyniku niektóre szerokości znaków nie odpowiadają Twoim wymaganiom; na przykład, zgodnie z wymaganiami, <LastName>
powinien mieć rozmiar 16 znaków, podczas gdy twój wynik wydaje się być wyłączony o godzinie 13. Powiedziałbym, że moje rozwiązanie poniżej daje oczekiwane rezultaty.
Gdy 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(., ' '), 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>
... prowadzony jest przeciwko przewidzianym XML (z </Detail>
dodanej, aby dokument dobrze uformowane):
<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>
... wymagany jest wynik:
*********19801111user test 201302261423332553 MAIN STREET san diego CA
Czy można użyć rozszerzenia takiego jak node-set() lub dodatkowego dokumentu/pliku xml, aby zachować szerokość i kolejność danych wyjściowych? –