英文:
need to write an xslt to transform xml to csv with specific data
问题
以下是修改后的XSLT代码,它只生成与标题字段映射中的字段匹配的数据:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="delimiter" select="','"/>
<xsl:template match="/">
<!-- Header row -->
<xsl:for-each select="root/headerFieldsMap/*">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:value-of select="$delimiter"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<!-- Data rows -->
<xsl:for-each select="root/psPositions/position">
<xsl:for-each select="root/headerFieldsMap/*">
<xsl:variable name="fieldName" select="."/>
<xsl:variable name="fieldValue" select="ancestor::position/*[local-name() = $fieldName]"/>
<!-- If the field value is StdHoursFreq and it's equal to 'W', replace it with 'Weekly' -->
<xsl:choose>
<xsl:when test="$fieldName = 'StdHoursFreq' and $fieldValue = 'W'">
<xsl:text>Weekly</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$fieldValue"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:value-of select="$delimiter"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这个修改后的XSLT代码会根据标题字段映射仅生成相应的数据,就像您所期望的那样。
英文:
I'm trying to write an xslt which creates a csv from an xml. I need to create a header field mapping such that header matches the data.
Here is an example xml
<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>Burns@mnr.org</ReportsToEmail>
<PayCurrency>USD</PayCurrency>
<MaxHeadCount>1</MaxHeadCount>
<BusinessUnit>MNCRR</BusinessUnit>
<JobFamilyCode>TRNOPS</JobFamilyCode>
<CompanyCode>MNR</CompanyCode>
<CurrHeadCount>1</CurrHeadCount>
</position>
</psPositions>
</root>
The csv or the output should look like this
Business Unit Code,Business Unit Desct,Department Description,Department,Posting Job Title,Working Position Description,Category,Position-Nbr,Standard hours Frequency,Standard Hours
MNCRR,Metro-North Railroad,New Haven EMU Shop,44302,Carman E R,Carman E Rate,Transportation Operations,01000333,W,40
MTAHQ,MTA Headquarters,Division of Management /Budget,400300,SR FINANCI,Sr Financial Analyst - Budgets,Finance/Accounting,01055644,W,37.5
Note that the data only has the values for the fields which are mentioned in the <headerFieldsMap> field. The fields in the <headerFieldsMap> may vary, there could only be 2 fields that come in, the data should show up accordingly.
Current xslt i have is this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="delimiter" select="','"/>
<xsl:template match="/">
<!-- Header row -->
<xsl:for-each select="root/headerFieldsMap/*">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:value-of select="$delimiter"/>
</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
<!-- Data rows -->
<xsl:for-each select="root/psPositions/position">
<xsl:for-each select="*">
<xsl:variable name="fieldName" select="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="$fieldName = 'StdHoursFreq' and $fieldValue = 'W'">
<xsl:text>Weekly</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$fieldValue"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:value-of select="$delimiter"/>
</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
But this one prints out all the data from the xml. I only need the data that is mentioned in the header.
XSLT standards: 1.0
The above xslt generates the output that looks like the one below. It shouldn't have data for the fields that are not in the header, like "Burns@mnr.org" shouldn't be there because "ReportsToEmail" isn't there in the header.
Business Unit Code,Business Unit Desct,Department Description,Department,Posting Job Title,Working Position Description,Category,Position-Nbr,Standard hours Frequency,Standard Hours
06511,N,New Haven EMU Shop,Carman E Rate,R,TWUR,SHARE,US,N,0,0,01000333,0,A,F,Approved,SHARE,Metro-North Railroad,0,H,26448E,USA,2,01000474,MNCRR,40,Carman E R,Weekly,NEW HAVEN,2023-02-21,98 Union Street,New Haven,004,Transportation Operations,44302,2023-02-21T19:12:05Z,CT,Burns@mnr.org,USD,1,MNCRR,TRNOPS,MNR,1
10004,Y,Division of Management /Budget,Sr Financial Analyst - Budgets,R,HAY0,SHARE,US,N,0,0,01055644,0,A,F,Approved,SHARE,MTA Headquarters,1,B,1287,USA,1,01005823,MTAHQ,37.5,SR FINANCI,Weekly,2 BROADWAY,2023-02-21,2 Broadway,New York,438,Finance/Accounting,400300,2023-02-21T12:57:45Z,NY,USD,1,MTAHQ,FINANC,MTA,0
答案1
得分: 1
我只需要标题中提到的数据
尝试以下方式:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:key name="row-data" match="position/*" use="concat(name(), generate-id(..))" />
<xsl:template match="/root">
<xsl:variable name="cols" select="headerFieldsMap/*" />
<!-- 标题行 -->
<xsl:for-each select="$cols">
<xsl:value-of select="." />
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<!-- 数据行 -->
<xsl:for-each select="psPositions/position">
<xsl:variable name="row-id" select="generate-id()" />
<xsl:for-each select="$cols">
<xsl:value-of select="key('row-data', concat(name(), $row-id))" />
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
请注意,从您的输入XML中获得的结果与您显示的结果非常不同。然而,样式表符合您问题中提出的逻辑(至少是我理解的方式)。
要将W
更改为Weekly
,您可以执行以下操作:
<xsl:for-each select="$cols">
<xsl:variable name="value" select="key('row-data', concat(name(), $row-id))" />
<xsl:choose>
<xsl:when test="name() = 'StdHoursFreq' and $value = 'W'">Weekly</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value" />
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
英文:
> I only need the data that is mentioned in the header
Try it along the lines of:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:key name="row-data" match="position/*" use="concat(name(), generate-id(..))" />
<xsl:template match="/root">
<xsl:variable name="cols" select="headerFieldsMap/*"/>
<!-- header row-->
<xsl:for-each select="$cols">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
<!-- data rows-->
<xsl:for-each select="psPositions/position">
<xsl:variable name="row-id" select="generate-id()"/>
<xsl:for-each select="$cols">
<xsl:value-of select="key('row-data', concat(name(), $row-id))"/>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note that the result obtained from your input XML is very different from what you show. Nevertheless, the stylesheet conforms to the logic presented in your question (at least the way I understood it).
To change W
to Weekly
, you could do:
<xsl:for-each select="$cols">
<xsl:variable name="value" select="key('row-data', concat(name(), $row-id))" />
<xsl:choose>
<xsl:when test="name() = 'StdHoursFreq' and $value = 'W'">Weekly</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
答案2
得分: 1
请尝试以下的XSLT。
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="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>
</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 $row/*[local-name()='StdHoursFreq']">
<xsl:text>Weekly</xsl:text>
</xsl:when>
<xsl:when test="$fieldName = 'DescrShort'">
<xsl:value-of select="$row/*[local-name()='LocationPostal']"/>
</xsl:when>
<xsl:otherwise>
<xsl:valueof 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>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果你需要任何进一步的帮助,请随时告诉我。
英文:
Please try the following XSLT.
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="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 $row/*[local-name()='StdHoursFreq']">
<xsl:text>Weekly</xsl:text>
</xsl:when>
<xsl:when test="$fieldName = 'DescrShort'">
<xsl:value-of select="$row/*[local-name()='LocationPostal']"/>
</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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论