我正在尝试使用模板将字符串'REPORT_BLANK'替换为默认值“”。它似乎适用于'Status'属性(在xsl输出中),但是,对于'PayGroup'attirbute,我得到包含'& #x'字符和来自我不需要的元素的数据的意外输出。请帮助这个新手。
XML来源:
<?xml version = '1.0' encoding = 'UTF-8'?>
<DATA_DS>
<G_1>
<PAYROLL_ACTION_ID>5103</PAYROLL_ACTION_ID>
<G_2>
<FILE_FRAGMENT>
<PayrollInterface>
<REP_CATEGORY_NAME>HMLR ePayfact GIF Interface 1</REP_CATEGORY_NAME>
<parameters>
<RequestID>300000016822367</RequestID>
<FLOW_NAME>04102017-11:26</FLOW_NAME>
<LegislativeDataGroupID>300000001404188</LegislativeDataGroupID>
<EffectiveDate>2017-10-04</EffectiveDate>
<StartDate>2017-10-04</StartDate>
<ReportCategoryID>300000016773109</ReportCategoryID>
<action_parameter_group_id>300000000174069</action_parameter_group_id>
<ChangesOnly>N</ChangesOnly>
<PayrollName>300000001632050</PayrollName>
<PayrollPeriod/>
<SuppressZeroBalance/>
</parameters>
<InterfaceData>
<OBJECT_ACTION_ID>18527</OBJECT_ACTION_ID>
<Employee>
<Employee>
<EmployeeBasic>
<EmployeeNo>50120</EmployeeNo>
</EmployeeBasic>
</Employee>
</Employee>
<EELocation>
<EELocation_Employment>
<EELocation_Assignment_Data>
<EELocation_Assignment>
<EELocation_Rec>
<HRAssignmentID_Internal>300000016784341</HRAssignmentID_Internal>
</EELocation_Rec>
</EELocation_Assignment>
</EELocation_Assignment_Data>
</EELocation_Employment>
</EELocation>
<EEPSPayChange>
<EEPSPayChange_Employment>
<EEPSPayChange_Assignment_Data>
<EEPSPayChange_Assignment>
<GradeStepRateValue_Record/>
<EEPSPayChange_Rec>
<HRAssignmentID_Internal>300000016784341</HRAssignmentID_Internal>
<PersonLatestHireDate_Internal>2017-10-03T00:00:00.000Z</PersonLatestHireDate_Internal>
<EffectiveDate>2017-10-03T00:00:00.000Z</EffectiveDate>
<ChangeType>START</ChangeType>
<GradeId/>
<AssignmentCategory_Internal/>
<GradeSubCode>REPORT_BLANK</GradeSubCode>
<Salary_Internal/>
<Salary_2_Internal>TOBEDERIVED</Salary_2_Internal>
<AssignFullTimeHours_Internal>37</AssignFullTimeHours_Internal>
<MaxOTRateType>N</MaxOTRateType>
<FTE_Internal>1</FTE_Internal>
<PayGroupId>06</PayGroupId>
<PersonType_Internal>Permanent</PersonType_Internal>
<Classification>01</Classification>
<HoursPayable>37</HoursPayable>
<WorkingPatternId>501</WorkingPatternId>
<OSPSchemeNo>1</OSPSchemeNo>
<MilestoneDate_Internal>TOBEDERIVED</MilestoneDate_Internal>
</EEPSPayChange_Rec>
</EEPSPayChange_Assignment>
</EEPSPayChange_Assignment_Data>
</EEPSPayChange_Employment>
</EEPSPayChange>
<EEPSLocation>
<EEPSLocation_TermsDataGroup>
<EEPSLocation_TermsRecord>
<EEPSLocation_AssgnDataGroup>
<EEPSLocation_AssgnRecord>
<PersonType_Internal>Permanent</PersonType_Internal>
<PersonLatestHireDate_Internal>2017-10-03T00:00:00.000Z</PersonLatestHireDate_Internal>
<EffectiveDate>2017-10-03T00:00:00.000Z</EffectiveDate>
<DetachedDutyInd>N</DetachedDutyInd>
<LedgerHeading>LD</LedgerHeading>
<HRAssignmentID_Internal>300000016784341</HRAssignmentID_Internal>
</EEPSLocation_AssgnRecord>
</EEPSLocation_AssgnDataGroup>
</EEPSLocation_TermsRecord>
</EEPSLocation_TermsDataGroup>
</EEPSLocation>
<EEBankDetail/>
<EEPaymentElement/>
</InterfaceData>
<InterfaceData>
<OBJECT_ACTION_ID>18505</OBJECT_ACTION_ID>
<Employee>
<Employee>
<EmployeeBasic>
<EmployeeNo>50061</EmployeeNo>
<Status>REPORT_BLANK</Status>
<PensElig>Y</PensElig>
</EmployeeBasic>
<EmployeeBasic_DateMarriageCeased>
<PersonDetailsRecords>
<PersonDetails>
<PersonID_Internal>300000015958328</PersonID_Internal>
<DateMarriageCeased/>
</PersonDetails>
</PersonDetailsRecords>
</EmployeeBasic_DateMarriageCeased>
<EmployeeTermination>
<TerminationDetailsRecord>
<TerminationDetails>
<TerminationAction_Internal/>
<TerminationStatus_Internal>REPORT_BLANK</TerminationStatus_Internal>
<LeavingDate/>
<ReasonForLeaving/>
<PersonID_Internal>300000015958328</PersonID_Internal>
</TerminationDetails>
</TerminationDetailsRecord>
</EmployeeTermination>
</Employee>
</Employee>
<EELocation>
<EELocation_Employment>
<EELocation_Assignment_Data>
<EELocation_Assignment>
<EELocation_Rec>
<HRAssignmentID_Internal>300000015958405</HRAssignmentID_Internal>
</EELocation_Rec>
</EELocation_Assignment>
</EELocation_Assignment_Data>
</EELocation_Employment>
<EELocation_Employment>
<EELocation_Assignment_Data>
<EELocation_Assignment>
<EELocation_Rec>
<HRAssignmentID_Internal>300000016544638</HRAssignmentID_Internal>
</EELocation_Rec>
</EELocation_Assignment>
</EELocation_Assignment_Data>
</EELocation_Employment>
</EELocation>
<EEPSPayChange>
<EEPSPayChange_Employment>
<EEPSPayChange_Assignment_Data>
<EEPSPayChange_Assignment>
<GradeStepRateValue_Record>
<GradeStepRateValue>
<GradeStepRateValue_Record>
<AssgnID_Internal>300000015958405</AssgnID_Internal>
<GradeStepRateValue_Internal>49547</GradeStepRateValue_Internal>
</GradeStepRateValue_Record>
</GradeStepRateValue>
</GradeStepRateValue_Record>
<EEPSPayChange_Rec>
<HRAssignmentID_Internal>300000015958405</HRAssignmentID_Internal>
<PayGroupId>06</PayGroupId>
</EEPSPayChange_Rec>
</EEPSPayChange_Assignment>
</EEPSPayChange_Assignment_Data>
</EEPSPayChange_Employment>
<EEPSPayChange_Employment>
<EEPSPayChange_Assignment_Data>
<EEPSPayChange_Assignment>
<GradeStepRateValue_Record/>
<EEPSPayChange_Rec>
<PayGroupId>08</PayGroupId>
</EEPSPayChange_Rec>
</EEPSPayChange_Assignment>
</EEPSPayChange_Assignment_Data>
</EEPSPayChange_Employment>
</EEPSPayChange>
<EEPSLocation>
<EEPSLocation_TermsDataGroup>
<EEPSLocation_TermsRecord>
<EEPSLocation_AssgnDataGroup>
<EEPSLocation_AssgnRecord>
<PersonType_Internal>Permanent</PersonType_Internal>
</EEPSLocation_AssgnRecord>
</EEPSLocation_AssgnDataGroup>
</EEPSLocation_TermsRecord>
</EEPSLocation_TermsDataGroup>
<EEPSLocation_TermsDataGroup>
<EEPSLocation_TermsRecord>
<EEPSLocation_AssgnDataGroup>
<EEPSLocation_AssgnRecord>
<PersonType_Internal>Permanent</PersonType_Internal>
</EEPSLocation_AssgnRecord>
</EEPSLocation_AssgnDataGroup>
</EEPSLocation_TermsRecord>
</EEPSLocation_TermsDataGroup>
</EEPSLocation>
<EEBankDetail/>
<EEPaymentElement/>
</InterfaceData>
<InterfaceData>
<OBJECT_ACTION_ID>18511</OBJECT_ACTION_ID>
<Employee>
<Employee>
<EmployeeBasic>
<EmployeeNo>41356</EmployeeNo>
<EffectiveDate>2017-10-04T00:00:00.000Z</EffectiveDate>
<PensElig>Y</PensElig>
</EmployeeBasic>
<EmployeeBasic_DateMarriageCeased>
<PersonDetailsRecords>
<PersonDetails>
<PersonID_Internal>300000015238875</PersonID_Internal>
<DateMarriageCeased/>
</PersonDetails>
</PersonDetailsRecords>
</EmployeeBasic_DateMarriageCeased>
<EmployeeAddress/>
<EmployeeTermination>
<TerminationDetailsRecord>
<TerminationDetails>
<TerminationAction_Internal/>
<TerminationStatus_Internal>REPORT_BLANK</TerminationStatus_Internal>
<LeavingDate/>
<ReasonForLeaving/>
<PersonID_Internal>300000015238875</PersonID_Internal>
</TerminationDetails>
</TerminationDetailsRecord>
</EmployeeTermination>
</Employee>
</Employee>
<EELocation>
<EELocation_Employment>
<EELocation_Assignment_Data>
<EELocation_Assignment>
<EELocation_Rec>
<HRAssignmentID_Internal>300000015239345</HRAssignmentID_Internal>
</EELocation_Rec>
</EELocation_Assignment>
</EELocation_Assignment_Data>
</EELocation_Employment>
</EELocation>
<EEPSPayChange>
<EEPSPayChange_Employment>
<EEPSPayChange_Assignment_Data>
<EEPSPayChange_Assignment>
<GradeStepRateValue_Record/>
<EEPSPayChange_Rec>
<HRAssignmentID_Internal>300000015239345</HRAssignmentID_Internal>
<PayGroupId>REPORT_BLANK</PayGroupId>
</EEPSPayChange_Rec>
</EEPSPayChange_Assignment>
</EEPSPayChange_Assignment_Data>
</EEPSPayChange_Employment>
</EEPSPayChange>
<EEPSLocation>
<EEPSLocation_TermsDataGroup>
<EEPSLocation_TermsRecord>
<EEPSLocation_AssgnDataGroup>
<EEPSLocation_AssgnRecord>
<PersonType_Internal>Apprenticeship</PersonType_Internal>
</EEPSLocation_AssgnRecord>
</EEPSLocation_AssgnDataGroup>
</EEPSLocation_TermsRecord>
</EEPSLocation_TermsDataGroup>
</EEPSLocation>
<EEBankDetail/>
<EEPaymentElement/>
</InterfaceData>
</PayrollInterface>
</FILE_FRAGMENT>
</G_2>
</G_1>
</DATA_DS>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<company>
<Data>
<Payroll>
<xsl:for-each-group select="//InterfaceData" group-by="OBJECT_ACTION_ID">
<!-- Employee -->
<Employee>
<xsl:for-each select="current-group()//EmployeeBasic">
<xsl:for-each select="./*">
<xsl:call-template name="CGIelementToAttribute">
<xsl:with-param name="myVarElementName" select="name(.)"/>
<xsl:with-param name="myVarElementValue" select="current()"
/>
</xsl:call-template>
</xsl:for-each>
</xsl:for-each>
<xsl:call-template name="CGIelementToAttribute">
<xsl:with-param name="myVarElementName" select="'PayGroup'"/>
<xsl:with-param name="myVarElementValue" select="EEPSPayChange/EEPSPayChange_Employment[last()]/EEPSPayChange_Assignment_Data[1]/EEPSPayChange_Assignment[1]/EEPSPayChange_Rec[1]/PayGroupId[1]/text()"/>
</xsl:call-template>
</Employee>
</xsl:for-each-group>
</Payroll>
</Data>
</company>
</xsl:template>
<xsl:template name="CGIelementToAttribute">
<xsl:param name="myVarElementName"/>
<xsl:param name="myVarElementValue"/>
<!-- if the element value contains 'BLANK' or the element name ends with 'Internal' it will not be output -->
<xsl:if
test="not(contains('|BLANK|', concat('|', $myVarElementValue, '|'))) and not(ends-with(name(), 'Internal')) and not(contains('|Salary|', concat('|', $myVarElementName, '|')))">
<xsl:choose>
<!-- empty element -->
<xsl:when test="$myVarElementValue = ''">
<xsl:attribute name="{$myVarElementName}">
<xsl:value-of select="''"/>
</xsl:attribute>
</xsl:when>
<!-- output 'REPORT BLANK' as the attribute value -->
<xsl:when test="(contains('|REPORT_BLANK|', concat('|', $myVarElementValue, '|')))">
<xsl:attribute name="{$myVarElementName}">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="current()"/>
<xsl:with-param name="replace" select="'REPORT_BLANK'"/>
<xsl:with-param name="by" select="''"/>
</xsl:call-template>
</xsl:attribute>
</xsl:when>
<!-- format date -->
<xsl:when test="contains($myVarElementName, 'Date')">
<xsl:call-template name="formatDate">
<xsl:with-param name="myVarDateString" select="$myVarElementValue"/>
<xsl:with-param name="myVarDateName" select="$myVarElementName"/>
</xsl:call-template>
</xsl:when>
<!-- no transformation -->
<xsl:otherwise>
<xsl:attribute name="{$myVarElementName}">
<xsl:value-of select="$myVarElementValue"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name="formatDate">
<!-- CGIformatDate: converts dateto CGI format from CCYY-MM-DDThh:mm:ss:mm[Z] to dd/mm/ccyy -->
<xsl:param name="myVarDateString"/>
<xsl:param name="myVarDateName"/>
<xsl:attribute name="{$myVarDateName}">
<xsl:value-of select="concat(substring($myVarDateString, 9, 2), '/', substring($myVarDateString, 6, 2), '/', substring($myVarDateString, 1, 4))"/>
</xsl:attribute>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="by"/>
<xsl:variable name="str1">
<xsl:call-template name="string-replace">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="by" select="$by"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="str2">
<!-- 2nd... clean up -->
<xsl:call-template name="string-replace">
<xsl:with-param name="text" select="$str1"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="by" select="$by"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="str3">
<xsl:choose>
<xsl:when
test="substring($str2, string-length($str2) - string-length($by), string-length($str2)) = concat($by, ' ')">
<xsl:value-of
select="substring($str2, 1, string-length($str2) - string-length($by) - 1)"
/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="substring($str3, 1, string-length($by) + 1) = concat($by, ' ')">
<xsl:value-of select="substring-after($str3, concat($by, ' '))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str3"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="string-replace">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="by"/>
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text, $replace)"/>
<xsl:value-of select="$by"/>
<xsl:call-template name="string-replace">
<xsl:with-param name="text" select="substring-after($text, $replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="by" select="$by"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XSL输出:
<?xml version="1.0" encoding="UTF-8"?>
<company>
<Data>
<Payroll>
<Employee EmployeeNo="50120" PayGroup="06"></Employee>
<Employee EmployeeNo="50061" Status="TOBEDERIVED" PensElig="Y" PayGroup="08"></Employee>
<Employee EmployeeNo="41356" EffectiveDate="04/10/2017" PensElig="Y"
PayGroup="
 18511
 
 
 
 41356
 2017-10-04T00:00:00.000Z
 Y
 
 
 
 
 300000015238875
 
 
 
 
 
 
 
 
 
 
 
 
 300000015238875
 
 
 
 
 
 
 
 
 
 
 300000015239345
 
 
 
 
 
 
 
 
 
 
 
 300000015239345
 
 
 
 
 
 
 
 
 
 
 
 Apprenticeship
 
 
 
 
 
 
 
 "
></Employee>
</Payroll>
</Data>
</company>
答案 0 :(得分:0)
正在发生的事情是text()
的所有InterfaceData
个后代都在您的属性中输出。这包括空格和换行符。
如果您将<xsl:strip-space elements="*"/>
添加为xsl:stylesheet
的子项,则会看到新行的十六进制引用以及空格被删除。但是,该值似乎仍然不正确。
我认为这是因为您在名为current()
的模板中对模板string-replace-all
的调用中使用了CGIelementToAttribute
。
尝试更改此内容:
<!-- output 'REPORT BLANK' as the attribute value -->
<xsl:when test="(contains('|REPORT_BLANK|', concat('|', $myVarElementValue, '|')))">
<xsl:attribute name="{$myVarElementName}">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="current()"/> <!-- THIS LINE -->
<xsl:with-param name="replace" select="'REPORT_BLANK'"/>
<xsl:with-param name="by" select="''"/>
</xsl:call-template>
</xsl:attribute>
</xsl:when>
到此:
<!-- output 'REPORT BLANK' as the attribute value -->
<xsl:when test="(contains('|REPORT_BLANK|', concat('|', $myVarElementValue, '|')))">
<xsl:attribute name="{$myVarElementName}">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$myVarElementValue"/> <!-- THIS LINE -->
<xsl:with-param name="replace" select="'REPORT_BLANK'"/>
<xsl:with-param name="by" select="''"/>
</xsl:call-template>
</xsl:attribute>
</xsl:when>
您可能还需要替换:
<xsl:with-param name="by" select="''"/>
使用:
<xsl:with-param name="by" select="'REPORT BLANK'"/>
否则您的属性值将为空。这是基于xsl:when
之前的评论。 (<!-- output 'REPORT BLANK' as the attribute value -->
)
另外,如果您真的使用的是XSLT 2.0处理器,为什么不使用replace()
函数而不是命名模板?