使用过滤器将 XML 转换为 CSV

问题描述 投票:0回答:0

我有一个 xml 和一个将 xml 转换为 csv 的 XSLT。这里 headerFieldsMap 是 csv 的头部,psPositions 是数据。 XML 版本为 1.0 我需要对 xslt 进行更改,以便名为 PositionNbr 的标头(如果存在)应始终是 csv 的第一列。目前“业务单位代码”作为 CSV 中的第一列出现,但我希望“Position-Nbr”成为第一列,如果它存在于< headerFieldsMap>中。 这是 xml

 <root>
 <headerFieldsMap>
    <BusinessUnit>Business Unit Code</BusinessUnit>
    <CompanyName>Business Unit Desct</CompanyName>
    <DeptDescr>Department Description</DeptDescr>
    <DeptId>Department</DeptId>
    <DescrShort>Posting Job Title</DescrShort>
    <Description>Working Position Description</Description>
    <JobFamily>Category</JobFamily>
    <PositionNbr>Position-Nbr</PositionNbr>
    <StdHoursFreq>Standard hours Frequency</StdHoursFreq>
    <TotalWorkingHours>Standard Hours</TotalWorkingHours>
  </headerFieldsMap>
  <psPositions>
    <position>
      <LocationPostal>06511</LocationPostal>
      <ConfidentialFlag>N</ConfidentialFlag>
      <DeptDescr>New Haven EMU Shop</DeptDescr>
      <Description>Carman E Rate</Description>
      <RegTemp>R</RegTemp>
      <SalAdminPlan>TWUR</SalAdminPlan>
      <JobCodeSetid>SHARE</JobCodeSetid>
      <LocationCountry>US</LocationCountry>
      <Remote>N</Remote>
      <Step>0</Step>
      <SalaryRangeTo>0</SalaryRangeTo>
      <PositionNbr>01000333</PositionNbr>
      <SalaryRangeFrom>0</SalaryRangeFrom>
      <EffStatus>A</EffStatus>
      <FullPartTime>F</FullPartTime>
      <PositionStatus>Approved</PositionStatus>
      <LocationSetId>SHARE</LocationSetId>
      <CompanyName>Metro-North Railroad</CompanyName>
      <RemainingHeadCount>0</RemainingHeadCount>
      <PayFrequency>H</PayFrequency>
      <JobCode>26448E</JobCode>
      <RegRegion>USA</RegRegion>
      <Shift>2</Shift>
      <ReportsToPos>01000474</ReportsToPos>
      <DeptIdSetId>MNCRR</DeptIdSetId>
      <TotalWorkingHours>40</TotalWorkingHours>
      <DescrShort>Carman E R</DescrShort>
      <StdHoursFreq>W</StdHoursFreq>
      <LocationCode>NEW HAVEN</LocationCode>
      <Effdt>2023-02-21</Effdt>
      <LocationDescr>98 Union Street</LocationDescr>
      <LocationCity>New Haven</LocationCity>
      <Grade>004</Grade>
      <JobFamily>Transportation Operations</JobFamily>
      <DeptId>44302</DeptId>
      <LastUpdDtTm>2023-02-21T19:12:05Z</LastUpdDtTm>
      <LocationRegion>CT</LocationRegion>
      <ReportsToEmail>[email protected]</ReportsToEmail>
      <PayCurrency>USD</PayCurrency>
      <MaxHeadCount>1</MaxHeadCount>
      <BusinessUnit>MNCRR</BusinessUnit>
      <JobFamilyCode>TRNOPS</JobFamilyCode>
      <CompanyCode>MNR</CompanyCode>
      <CurrHeadCount>1</CurrHeadCount>
    </position>
 </psPositions>
</root>

这是xslt

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:req="http://peoplesoft.com/requestResponse"
                xmlns:csl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:variable name="delimiter" select="','"/>

    <xsl:template match="/root">
        <!-- Header row -->
        <xsl:for-each select="headerFieldsMap/*">
            <xsl:value-of select="."/>
            <xsl:if test="position() != last()">
                <xsl:value-of select="$delimiter"/>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>

        <!-- Data rows -->
         <xsl:for-each select="psPositions/position">
            <xsl:variable name="row" select="."/>
            <xsl:for-each select="/root/headerFieldsMap/*">
                <xsl:variable name="fieldName" select="local-name()"/>
                <xsl:variable name="fieldValue" select="."/>

                <!-- If the field value is StdHoursFreq and it's equal to "W", replace it with "Weekly" -->
                <xsl:choose>
                    <xsl:when test="$row/*[local-name()=$fieldName] = 'W' and $fieldName = 'StdHoursFreq'">
                        <xsl:text>Weekly</xsl:text>
                    </xsl:when>
                    <xsl:when test="$row/*[local-name()=$fieldName] = 'B' and $fieldName = 'StdHoursFreq'">
                        <xsl:text>Bi-Weekly</xsl:text>
                    </xsl:when>
                    <xsl:when test="$row/*[local-name()=$fieldName] = 'M' and $fieldName = 'StdHoursFreq'">
                        <xsl:text>Monthly</xsl:text>
                    </xsl:when>
                    <xsl:when test="$fieldName = 'DescrShort'">
                        <xsl:value-of select="$row/*[local-name()='Description']"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$row/*[local-name()=$fieldName]"/>
                    </xsl:otherwise>
                </xsl:choose>

                <xsl:if test="position() != last()">
                    <xsl:value-of select="$delimiter"/>
                </xsl:if>
            </xsl:for-each>
            <xsl:text>&#xA;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
xml csv xslt xslt-1.0
© www.soinside.com 2019 - 2024. All rights reserved.