我正在转换下面的XML以生成文本输出。我能够成功转换XML。但是,还有一个新的打印线在我的情况下不需要。我尝试了不同的方法来避免打印不必要的新行。但是,我无法成功。基本上我是根据XSLT中的条件打印数据,如果XSLT中的最后一条记录未通过条件,那么新行仍然是在前一循环中创建的。
以下是我的XML:
<?xml version='1.0' encoding='UTF-8'?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<wd:Get_EFW2_Year_End_Worker_Filing_Data_Response
xmlns:wd="urn:com.workday/bsvc"
wd:version="v28.0">
<wd:Request_Criteria>
<wd:Response_Data>
<wd:EFW2_Year_End_Worker_Filing>
<wd:W-2_Instance_ID>3707$113701</wd:W-2_Instance_ID>
<wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
<wd:EFW2_Year_End_Worker_Filing_Data>
<wd:Employee_First_Name>Rowyna</wd:Employee_First_Name>
<wd:Employee_Middle_Initial>A</wd:Employee_Middle_Initial>
<wd:Employee_Last_Name>Brewer</wd:Employee_Last_Name>
<wd:EFW2_Additional_Data>
<wd:Statutory_Employee>0</wd:Statutory_Employee>
<wd:Retirement_Plan>0</wd:Retirement_Plan>
<wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
</wd:EFW2_Additional_Data>
</wd:EFW2_Year_End_Worker_Filing_Data>
</wd:EFW2_Year_End_Worker_Filing>
<wd:EFW2_Year_End_Worker_Filing>
<wd:W-2_Instance_ID>3707$113702</wd:W-2_Instance_ID>
<wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
<wd:EFW2_Year_End_Worker_Filing_Data>
<wd:Employee_First_Name>George</wd:Employee_First_Name>
<wd:Employee_Middle_Initial>E</wd:Employee_Middle_Initial>
<wd:Employee_Last_Name>Denbow</wd:Employee_Last_Name>
<wd:EFW2_Additional_Data>
<wd:Statutory_Employee>0</wd:Statutory_Employee>
<wd:Retirement_Plan>0</wd:Retirement_Plan>
<wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
</wd:EFW2_Additional_Data>
<wd:EFW2_Year_End_Worker_State_Data>
<wd:State_EIN>74-6000203-000</wd:State_EIN>
<wd:State>IL</wd:State>
<wd:State_FIPS_Code>17</wd:State_FIPS_Code>
<wd:State_Taxable_Wages>100.11</wd:State_Taxable_Wages>
<wd:State_Tax_Withheld>2004.34</wd:State_Tax_Withheld>
</wd:EFW2_Year_End_Worker_State_Data>
</wd:EFW2_Year_End_Worker_Filing_Data>
</wd:EFW2_Year_End_Worker_Filing>
<wd:EFW2_Year_End_Worker_Filing>
<wd:W-2_Instance_ID>3707$113703</wd:W-2_Instance_ID>
<wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
<wd:EFW2_Year_End_Worker_Filing_Data>
<wd:Employee_First_Name>Thomas</wd:Employee_First_Name>
<wd:Employee_Middle_Initial>B</wd:Employee_Middle_Initial>
<wd:Employee_Last_Name>Morris</wd:Employee_Last_Name>
<wd:EFW2_Additional_Data>
<wd:Statutory_Employee>0</wd:Statutory_Employee>
<wd:Retirement_Plan>0</wd:Retirement_Plan>
<wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
</wd:EFW2_Additional_Data>
</wd:EFW2_Year_End_Worker_Filing_Data>
</wd:EFW2_Year_End_Worker_Filing>
<wd:EFW2_Year_End_Worker_Filing>
<wd:W-2_Instance_ID>3707$113705</wd:W-2_Instance_ID>
<wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
<wd:EFW2_Year_End_Worker_Filing_Data>
<wd:Employee_First_Name>Gertrude</wd:Employee_First_Name>
<wd:Employee_Last_Name>Patton</wd:Employee_Last_Name>
<wd:EFW2_Additional_Data>
<wd:Statutory_Employee>0</wd:Statutory_Employee>
<wd:Retirement_Plan>0</wd:Retirement_Plan>
<wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
</wd:EFW2_Additional_Data>
<wd:EFW2_Year_End_Worker_State_Data>
<wd:State_EIN>74-6000203-000</wd:State_EIN>
<wd:State>IL</wd:State>
<wd:State_FIPS_Code>17</wd:State_FIPS_Code>
<wd:State_Taxable_Wages>100.11</wd:State_Taxable_Wages>
<wd:State_Tax_Withheld>2004.34</wd:State_Tax_Withheld>
</wd:EFW2_Year_End_Worker_State_Data>
</wd:EFW2_Year_End_Worker_Filing_Data>
</wd:EFW2_Year_End_Worker_Filing>
<wd:EFW2_Year_End_Worker_Filing>
<wd:W-2_Instance_ID>3707$113704</wd:W-2_Instance_ID>
<wd:Completed_Moment>2017-02-22T15:15:25.329-08:00</wd:Completed_Moment>
<wd:EFW2_Year_End_Worker_Filing_Data>
<wd:Employee_First_Name>Roy</wd:Employee_First_Name>
<wd:Employee_Middle_Initial>L</wd:Employee_Middle_Initial>
<wd:Employee_Last_Name>Briseno</wd:Employee_Last_Name>
<wd:EFW2_Additional_Data>
<wd:Statutory_Employee>0</wd:Statutory_Employee>
<wd:Retirement_Plan>0</wd:Retirement_Plan>
<wd:Third_Party_Sick_Pay>0</wd:Third_Party_Sick_Pay>
</wd:EFW2_Additional_Data>
</wd:EFW2_Year_End_Worker_Filing_Data>
</wd:EFW2_Year_End_Worker_Filing>
</wd:Response_Data>
</wd:Get_EFW2_Year_End_Worker_Filing_Data_Response>
</env:Body>
</env:Envelope>
&#13;
和我的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wd="urn:com.workday/bsvc"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
xmlns:xtt="urn:com.workday/xtt" xmlns:etv="urn.com.workday/etv" version="2.0"
xmlns:this="urn:BuildWorkerWageRecord_RW.xsl">
<xsl:strip-space elements="*"/>
<xsl:output method="text" indent="yes"/>
<xsl:variable name="NEWLINE" select="'
'"/>
<xsl:variable name="delimeter">|</xsl:variable>
<xsl:param name="prop.No.of.RS.Records"/>
<xsl:variable name="Direction_Left">
<xsl:text>LEFT</xsl:text>
</xsl:variable>
<xsl:variable name="Direction_Right">
<xsl:text>RIGHT</xsl:text>
</xsl:variable>
<xsl:variable name="Record_Identifier">
<xsl:text>RW</xsl:text>
</xsl:variable>
<xsl:template match="env:Envelope/env:Body/wd:Get_EFW2_Year_End_Worker_Filing_Data_Response">
<xsl:for-each select="wd:Response_Data/wd:EFW2_Year_End_Worker_Filing">
<!-- Get the state name to filter out the states other than Illinois -->
<xsl:variable name="State" select="wd:EFW2_Year_End_Worker_Filing_Data/wd:EFW2_Year_End_Worker_State_Data[wd:State = 'IL']/wd:State"/>
<xsl:if test="$State = 'IL'">
<!-- Record Identifier(RW) -->
<xsl:value-of select="$Record_Identifier"/>
<xsl:value-of select="$delimeter"/>
<!-- Social Security Number (SSN) -->
<xsl:variable name="SSN"
select="translate(wd:EFW2_Year_End_Worker_Filing_Data/wd:SSN, '-', '')"/>
<xsl:choose>
<xsl:when test="$SSN = ''">
<xsl:value-of select="'000000000'"/>
</xsl:when>
<xsl:when test="substring($SSN, 1, 3) = '666'">
<xsl:value-of select="'000000000'"/>
</xsl:when>
<xsl:when test="substring($SSN, 1, 1) = '9'">
<xsl:value-of select="'000000000'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$SSN"/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$delimeter"/>
<!-- Employee First Name -->
<xsl:variable name="FirstName"
select="wd:EFW2_Year_End_Worker_Filing_Data/wd:Employee_First_Name"/>
<xsl:value-of select="this:pad($Direction_Right, 15, ' ', $FirstName)"/>
<xsl:value-of select="$delimeter"/>
<!-- Employee Middle Name or Initial -->
<xsl:variable name="MiddleName"
select="wd:EFW2_Year_End_Worker_Filing_Data/wd:Employee_Middle_Initial"/>
<xsl:value-of select="this:pad($Direction_Right, 15, ' ', $MiddleName)"/>
<xsl:value-of select="$delimeter"/>
<!-- Employee Last Name -->
<xsl:variable name="LastName"
select="wd:EFW2_Year_End_Worker_Filing_Data/wd:Employee_Last_Name"/>
<xsl:value-of select="this:pad($Direction_Right, 20, ' ', $LastName)"/>
<xsl:value-of select="$delimeter"/>
<!-- Wages, Tips and Other Compensation - BOX-1 -->
<xsl:variable name="Box-1"
select="wd:EFW2_Year_End_Worker_Filing_Data/wd:EFW2_Year_End_Worker_Box_Data[wd:Box_Description = 'Wages, tips, and other compensation']/wd:Amount"/>
<xsl:choose>
<xsl:when test="$Box-1 != ''">
<xsl:value-of
select="this:pad($Direction_Left, 11, '0', this:impliedDecimal(xs:string(format-number($Box-1, '0.00'))))"
/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'00000000000'"/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$delimeter"/>
<xsl:if test="position() != last()">
<xsl:value-of select="$NEWLINE"/>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:function name="this:pad" as="xs:string">
<xsl:param name="direction" as="xs:string"/>
<xsl:param name="length" as="xs:integer"/>
<xsl:param name="padChar" as="xs:string"/>
<xsl:param name="value"/>
<xsl:variable name="padding_unreplaced">
<xsl:text> </xsl:text>
</xsl:variable>
<xsl:variable name="padding" select="replace($padding_unreplaced, ' ', $padChar)"/>
<xsl:choose>
<xsl:when test="string-length($value) = $length">
<xsl:sequence select="$value"/>
</xsl:when>
<xsl:when test="string-length($value) > $length">
<xsl:sequence select="substring($value, 1, $length)"/>
<!-- <xsl:choose>
<xsl:when test="$direction = $Direction_Left">
<xsl:sequence select="substring($value, 1, $length)"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence
select="substring($value, string-length($value) - $length + 1, $length)"
/>
</xsl:otherwise>
</xsl:choose> -->
</xsl:when>
<xsl:when test="$direction = $Direction_Left">
<xsl:sequence
select="concat(substring($padding, string-length($padding) - ($length - string-length($value)) + 1), $value)"
/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence
select="concat($value, substring($padding, string-length($padding) - ($length - string-length($value)) + 1))"
/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="this:impliedDecimal">
<xsl:param name="dollarAmount"/>
<xsl:if test="$dollarAmount">
<xsl:if test="not(contains($dollarAmount, '.'))">
<xsl:value-of select="concat($dollarAmount, '00')"/>
</xsl:if>
<xsl:if test="contains($dollarAmount, '.')">
<xsl:value-of
select="
if (string-length(substring-after($dollarAmount, '.')) = 1) then
(translate(concat($dollarAmount, '0'), '.', ''))
else
(translate($dollarAmount, '.', ''))"
/>
</xsl:if>
<xsl:if test="contains($dollarAmount, '-')">
<xsl:if test="contains($dollarAmount, '+')">
<xsl:value-of select="translate(translate($dollarAmount, '-', '0'), '+', '-')"/>
</xsl:if>
</xsl:if>
</xsl:if>
</xsl:function>
</xsl:stylesheet>
&#13;
打印状态代码=&#39; IL&#39;在wd:EFW2_Year_End_Worker_State_Data节点中。
当前输出:
Line是不必要的换行符。
预期出局:
我知道由于Postion()!= Last()条件,新行正在打印。我正在寻找替代这种逻辑,我可以避免印刷不必要的新线。
提前致谢!
答案 0 :(得分:1)
我可以通过两种方式来解决您的问题。
一。
你可以在xsl-foreach循环下移动Position()!= Last()条件吗?这将确保它不会打印该号码。但是,会有一条额外的新线可能没有害处。
两个
使用xsl if-else块。您需要将整个打印代码包装在if-else块中。伪代码就是这样。
if(Position() = Last())
Print all content without a new line
else
Print all content with a new line
答案 1 :(得分:1)
我知道新行正在打印,因为Postion()!= Last() 条件。
新行正在打印,因为您已选择要处理的所有记录,而不仅仅是来自伊利诺伊州的记录 - 并且处理的最后一条记录来自伊利诺伊州不。因此,条件 - 仅应用 来自伊利诺伊州的记录 - 将永远不会返回true。
解决方案是仅选择来自伊利诺伊州的记录 - 即更改:
<xsl:for-each select="wd:Response_Data/wd:EFW2_Year_End_Worker_Filing">
为:
<xsl:for-each select="wd:Response_Data/wd:EFW2_Year_End_Worker_Filing[wd:EFW2_Year_End_Worker_Filing_Data/wd:EFW2_Year_End_Worker_State_Data/wd:State = 'IL']">
并删除xsl:if
指令检查状态及其相关变量。