2017-10-05 133 views
0

我想用一個模板來替換一個字符串'REPORT_BLANK'與默認值「」。它似乎是'狀態'屬性(在xsl輸出內)的工作,但是,'PayGroup'attirbute我得到意想不到的輸出包含'&#x'字符和元素的數據,我不需要。請幫助這個新手。意外的'&#x'出現在XSL輸出

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="&#xA;      18511&#xA;      &#xA;       &#xA;        &#xA;         41356&#xA;         2017-10-04T00:00:00.000Z&#xA;         Y&#xA;        &#xA;        &#xA;         &#xA;          &#xA;           300000015238875&#xA;           &#xA;          &#xA;         &#xA;        &#xA;        &#xA;        &#xA;         &#xA;          &#xA;           &#xA;           &#xA;           &#xA;           &#xA;           300000015238875&#xA;          &#xA;         &#xA;        &#xA;       &#xA;      &#xA;      &#xA;       &#xA;        &#xA;         &#xA;          &#xA;           300000015239345&#xA;          &#xA;         &#xA;        &#xA;       &#xA;      &#xA;      &#xA;       &#xA;        &#xA;         &#xA;          &#xA;          &#xA;           300000015239345&#xA;           &#xA;          &#xA;         &#xA;        &#xA;       &#xA;      &#xA;      &#xA;       &#xA;        &#xA;         &#xA;          &#xA;           Apprenticeship&#xA;          &#xA;         &#xA;        &#xA;       &#xA;      &#xA;      &#xA;      &#xA;     " 
      ></Employee> 
     </Payroll> 
    </Data> 
</company> 
+0

這是調用'CGIelementToAttribute'值爲''REPORT_BLANK''的結果...您必須調試您的'string-replace-all'函數 –

+0

我是全新的,調試的最佳方式是什麼?我有氧氣的XML開發人員,有沒有一個很好的鏈接/視頻,我可以遵循調試XSL? – murfy92

+0

https://www.freeformatter.com/xsl-transformer.html#ad-output您可以製作一個最簡單的xml文件,並開始移除您的xslt中的相關部分,直到您只獲得有問題的部分,然後您修復它然後合併回原來的xslt –

回答

0

發生了什麼事是InterfaceData所有text()後代是在你的屬性輸出。這包括空白和換行符。

如果您添加<xsl:strip-space elements="*"/>作爲xsl:stylesheet的子項,您將看到新行的十六進制引用以及空格將被刪除。但是,該值仍然不正確。

我認爲這是因爲您在撥打模板string-replace-all時使用的current()位於名爲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()函數而不是命名模板?