我正在使用xsl转换组记录以显示为多条记录,其余记录显示一次并将结果输出到文本文件。
XML:
<?xml version="1.0" encoding="utf-8" ?>
<ed:Fin_Data xmlns:ed="urn:com.ad.report/psrpt">
<ed:Data_Entry>
<ed:Name>Ione Sys</ed:Name>
<ed:EAddress>111 Old Blvd. Ste 1
CITY, ST 12345
</ed:EAddress>
<ed:Business_Phone>+1 (111) 9999999</ed:Business_Phone>
<ed:Pay_Rate>Temporary</ed:Pay_Rate>
<ed:HAddress>1111 SW Old TER
CITY, ST 12345
</ed:HAddress>
<ed:Name>Idle Bain</ed:Name>
<ed:ID>X23675</ed:ID>
<ed:Location>New Blvd.</ed:Location>
<ed:Start_Date>2018-02-01</ed:Start_Date>
<ed:End_Date>2018-02-15</ed:End_Date>
<ed:Pay_Date>2018-02-28</ed:Pay_Date>
<ed:Prs_Lss>
<ed:Type>Prs</ed:Type>
<ed:Amt>3244.09</ed:Amt>
<ed:YTD>12785.37</ed:YTD>
</ed:Prs_Lss>
<ed:Prs_Lss>
<ed:Type>Lss</ed:Type>
<ed:Amt>1301.94</ed:Amt>
<ed:YTD>2864.17</ed:YTD>
</ed:Prs_Lss>
</ed:Data_Entry>
</ed:Fin_Data>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ed="urn:com.ad.report/psrpt">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="linefeed" select="'
'"></xsl:variable>
<xsl:template match="/ed:Fin_Data">
<xsl:text>"Name"|"EAddress"|"BusinessPhone"|"PayRate"|"HAddress"|"Name"|"ID"|"Location"|"StartDate"|"EndDate"|"PayDate"|"PrsType"|"PrsAmt"|"PrsYTD"|"LssType"|"LssAmt"|"LssYTD"</xsl:text>
<xsl:apply-templates select="ed:Data_Entry/ed:Prs_Lss"/>
</xsl:template>
<xsl:template match="ed:Data_Entry">
<xsl:value-of select="$linefeed"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="ed:Name"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:EAddress"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:EAddress"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Business_Phone"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Pay_Rate"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:HAddress"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Name"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:ID"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Location"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Start_Date"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:End_Date"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Pay_Date"/>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="ed:Prs_Lss">
<xsl:value-of select="$linefeed"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="ed:Type"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Amt"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:YTD"/>
<xsl:text>"</xsl:text>
</xsl:template>
</xsl:stylesheet>
结果:
"Name"|"EAddress"|"BusinessPhone"|"PayRate"|"HAddress"|"Name"|"ID"|"Location"|"StartDate"|"EndDate"|"PayDate"|"PrsType"|"PrsAmt"|"PrsYTD"|"LssType"|"LssAmt"|"LssYTD"
"Prs"|"3244.09"|"12785.37"
"Lss"|"1301.94"|"2864.17"
预期结果:
"Name"|"EAddress"|"BusinessPhone"|"PayRate"|"HAddress"|"Name"|"ID"|"Location"|"StartDate"|"EndDate"|"PayDate"|"PrsType"|"PrsAmt"|"PrsYTD"|"LssType"|"LssAmt"|"LssYTD"
"Ione Sys"|"111 Old Blvd. Ste 1 CITY, ST 12345"|"+1 (111) 9999999"|"Temporary"|"1111 SW Old TER CITY, ST 12345"|"Idle Bain"|"X23675"|"New Blvd."|"2018-02-01"|"2018-02-15"|"2018-02-28"|"Prs"|"3244.09"|"12785.37"
""|""|""|""|""|""|""|""|""|""|""|"Lss"|"1301.94"|"2864.17"
Data_Entry标记下的所有元素都应显示在第一条记录上,并且取决于Prs_Lss标记中的记录数,它应该与所有记录一起显示。在此示例中,由于Prs_Lss标记具有2条记录,因此第一个记录将具有所有数据,而第二个记录将具有空白,直到Pay_Date和Prs_Lss标记中的其余数据。
答案 0 :(得分:2)
我建议您这样尝试?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ed="urn:com.ad.report/psrpt">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/ed:Fin_Data">
<!-- HEADER -->
<xsl:text>"Name"|"EAddress"|"BusinessPhone"|"PayRate"|"HAddress"|"Name"|"ID"|"Location"|"StartDate"|"EndDate"|"PayDate"|"PrsType"|"PrsAmt"|"PrsYTD"|"LssType"|"LssAmt"|"LssYTD" </xsl:text>
<!-- ENTRY DATA -->
<xsl:for-each select="ed:Data_Entry">
<xsl:text>"</xsl:text>
<xsl:value-of select="ed:Name"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="translate(ed:EAddress, '
', ' ')"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Business_Phone"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Pay_Rate"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="translate(ed:HAddress, '
', ' ')"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Name"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:ID"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Location"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Start_Date"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:End_Date"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Pay_Date"/>
<xsl:text>"|"</xsl:text>
<!-- FIRST PRS/LSS -->
<xsl:variable name="first-Prs_Lss" select="ed:Prs_Lss[1]" />
<xsl:value-of select="$first-Prs_Lss/ed:Type"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="$first-Prs_Lss/ed:Amt"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="$first-Prs_Lss/ed:YTD"/>
<xsl:text>" </xsl:text>
<!-- OTHER PRS/LSSs -->
<xsl:for-each select="ed:Prs_Lss[position() > 1]">
<xsl:text>""|""|""|""|""|""|""|""|""|""|""|"</xsl:text>
<xsl:value-of select="ed:Type"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:Amt"/>
<xsl:text>"|"</xsl:text>
<xsl:value-of select="ed:YTD"/>
<xsl:text>" </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:2)
这是 XSLT-2.0 解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ed="urn:com.ad.report/psrpt">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="linefeed" select="'
'"></xsl:variable>
<xsl:template match="/ed:Fin_Data">
<xsl:text>"Name"|"EAddress"|"BusinessPhone"|"PayRate"|"HAddress"|"Name"|"ID"|"Location"|"StartDate"|"EndDate"|"PayDate"|"PrsType"|"PrsAmt"|"PrsYTD"|"LssType"|"LssAmt"|"LssYTD"</xsl:text>
<xsl:apply-templates select="ed:Data_Entry/ed:Prs_Lss[1]"/>
<xsl:apply-templates select="ed:Data_Entry/ed:Prs_Lss[position() > 1]"/>
</xsl:template>
<xsl:template match="ed:Data_Entry">
<xsl:text>"</xsl:text>
<xsl:value-of select="ed:Name, replace(ed:EAddress,'
',' '), ed:Business_Phone, ed:Pay_Rate, replace(ed:HAddress,'
',' '), ed:Name, ed:ID, ed:Location, ed:Start_Date, ed:End_Date, ed:Pay_Date" separator='"|"' />
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="ed:Prs_Lss[position() = 1]">
<xsl:value-of select="$linefeed"/>
<xsl:apply-templates select="../../ed:Data_Entry"/>
<xsl:text>|"</xsl:text>
<xsl:value-of select="ed:Type, ed:Amt, ed:YTD" separator='"|"' />
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="ed:Prs_Lss[position() > 1]">
<xsl:value-of select="$linefeed"/>
<xsl:value-of select="for $x in ../../ed:Data_Entry/*[not(self::ed:Prs_Lss)] return '""'" separator="|" />
<xsl:text>|"</xsl:text>
<xsl:value-of select="ed:Type, ed:Amt, ed:YTD" separator='"|"' />
<xsl:text>"</xsl:text>
</xsl:template>
</xsl:stylesheet>
其输出是:
"Name"|"EAddress"|"BusinessPhone"|"PayRate"|"HAddress"|"Name"|"ID"|"Location"|"StartDate"|"EndDate"|"PayDate"|"PrsType"|"PrsAmt"|"PrsYTD"|"LssType"|"LssAmt"|"LssYTD"
"Ione Sys"|"Idle Bain"|"111 Old Blvd. Ste 1 CITY, ST 12345 "|"+1 (111) 9999999"|"Temporary"|"1111 SW Old TER CITY, ST 12345 "|"Ione Sys"|"Idle Bain"|"X23675"|"New Blvd."|"2018-02-01"|"2018-02-15"|"2018-02-28"|"Prs"|"3244.09"|"12785.37"
""|""|""|""|""|""|""|""|""|""|""|"Lss"|"1301.94"|"2864.17"