我正在尝试将垂直数据集转换为水平数据集。
数据集:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Export>
<Record>
<D_EmployeeNumber>1</D_EmployeeNumber>
<D_LastName>A</D_LastName>
<D_FirstName>Scottie</D_FirstName>
<D_MiddleName>E</D_MiddleName>
<D_SocSecYTDWHAmt>306.83</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>71.76</D_MedicareYTDWHAmt>
<D_WHType>Main</D_WHType>
<D_WHName>Main</D_WHName>
<D_WHAmt>0.00</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>2</D_EmployeeNumber>
<D_LastName>B</D_LastName>
<D_FirstName>Michael</D_FirstName>
<D_MiddleName>L</D_MiddleName>
<D_SocSecYTDWHAmt>311.78</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>72.92</D_MedicareYTDWHAmt>
<D_WHType>Main</D_WHType>
<D_WHName>Main</D_WHName>
<D_WHAmt>0.00</D_WHAmt>
<D_EquityAward></D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>3</D_EmployeeNumber>
<D_LastName>C</D_LastName>
<D_FirstName>Kelly</D_FirstName>
<D_MiddleName>D</D_MiddleName>
<D_SocSecYTDWHAmt>353.03</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>82.56</D_MedicareYTDWHAmt>
<D_WHType>Main</D_WHType>
<D_WHName>Main</D_WHName>
<D_WHAmt>0.00</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>4</D_EmployeeNumber>
<D_LastName>D</D_LastName>
<D_FirstName>Janet</D_FirstName>
<D_MiddleName>A</D_MiddleName>
<D_SocSecYTDWHAmt>0.00</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>0.00</D_MedicareYTDWHAmt>
<D_WHType>Main</D_WHType>
<D_WHName>Main</D_WHName>
<D_WHAmt>0.00</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>4</D_EmployeeNumber>
<D_LastName>D</D_LastName>
<D_FirstName>Janet</D_FirstName>
<D_MiddleName>A</D_MiddleName>
<D_SocSecYTDWHAmt>0.00</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>0.00</D_MedicareYTDWHAmt>
<D_WHType>County</D_WHType>
<D_WHName>FrdrckCoW/H</D_WHName>
<D_WHAmt>27.42</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>5</D_EmployeeNumber>
<D_LastName>E</D_LastName>
<D_FirstName>John</D_FirstName>
<D_MiddleName>E</D_MiddleName>
<D_SocSecYTDWHAmt>0.00</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>0.00</D_MedicareYTDWHAmt>
<D_WHType>Main</D_WHType>
<D_WHName>Main</D_WHName>
<D_WHAmt>0.00</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>5</D_EmployeeNumber>
<D_LastName>E</D_LastName>
<D_FirstName>John</D_FirstName>
<D_MiddleName>E</D_MiddleName>
<D_SocSecYTDWHAmt>0.00</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>0.00</D_MedicareYTDWHAmt>
<D_WHType>County</D_WHType>
<D_WHName>WashCoW/H-MD</D_WHName>
<D_WHAmt>163.18</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>6</D_EmployeeNumber>
<D_LastName>F</D_LastName>
<D_FirstName>Todd</D_FirstName>
<D_MiddleName>E</D_MiddleName>
<D_SocSecYTDWHAmt>709.69</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>165.98</D_MedicareYTDWHAmt>
<D_WHType>Main</D_WHType>
<D_WHName>Main</D_WHName>
<D_WHAmt>0.00</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>6</D_EmployeeNumber>
<D_LastName>F</D_LastName>
<D_FirstName>Todd</D_FirstName>
<D_MiddleName>E</D_MiddleName>
<D_SocSecYTDWHAmt>709.69</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>165.98</D_MedicareYTDWHAmt>
<D_WHType>Township</D_WHType>
<D_WHName>BtlrTwpW/H-L</D_WHName>
<D_WHAmt>120.00</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>7</D_EmployeeNumber>
<D_LastName>G</D_LastName>
<D_FirstName>Melissa</D_FirstName>
<D_MiddleName>L</D_MiddleName>
<D_SocSecYTDWHAmt>0.00</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>0.00</D_MedicareYTDWHAmt>
<D_WHType>Main</D_WHType>
<D_WHName>Main</D_WHName>
<D_WHAmt>0.00</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
<Record>
<D_EmployeeNumber>7</D_EmployeeNumber>
<D_LastName>G</D_LastName>
<D_FirstName>Melissa</D_FirstName>
<D_MiddleName>L</D_MiddleName>
<D_SocSecYTDWHAmt>0.00</D_SocSecYTDWHAmt>
<D_MedicareYTDWHAmt>0.00</D_MedicareYTDWHAmt>
<D_WHType>County</D_WHType>
<D_WHName>FrdrckCoW/H</D_WHName>
<D_WHAmt>122.74</D_WHAmt>
<D_EquityAward>True</D_EquityAward>
</Record>
</Export>
我试了很多东西,眼睛都快瞪大了。
这是我最新的XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:vb="urn:the-xml-files:xslt-vb"
xmlns:cs="urn:cs">
<msxsl:script language="VBScript" implements-prefix="vb" >
<msxsl:using namespace="System.IO"/>
<![CDATA[
]]>
</msxsl:script>
<xsl:output method="text" indent="no"/>
<xsl:template match="/">
<xsl:variable name="CountyRecords">
<Records>
<xsl:for-each select="Export/Record[(D_EquityAward = 'True' and D_WHType ='County' and D_WHAmt != 0.00)]"> <!-- Only County and Equity -->
<Record>
<xsl:element name="EmpNumber">
<xsl:copy-of select="D_EmployeeNumber"/>
</xsl:element>
<xsl:element name="CountyWHName">
<xsl:copy-of select="D_WHName"/>
</xsl:element>
</Record>
</xsl:for-each>
</Records>
</xsl:variable>
<xsl:variable name="CityRecords">
<Records>
<xsl:for-each select="Export/Record[(D_EquityAward = 'True' and D_WHType ='City' and D_WHAmt != 0.00)]"> <!-- Only County and Equity -->
<Record>
<xsl:element name="EmpNumber">
<xsl:value-of select="D_EmployeeNumber"/>
</xsl:element>
<xsl:element name="CityWHName">
<xsl:value-of select="D_WHName"/>
</xsl:element>
</Record>
</xsl:for-each>
</Records>
</xsl:variable>
<xsl:variable name="BoroughRecords">
<Records>
<xsl:for-each select="Export/Record[(D_EquityAward = 'True' and D_WHType ='Borough' and D_WHAmt != 0.00)]"> <!-- Only County and Equity -->
<Record>
<xsl:element name="EmpNumber">
<xsl:value-of select="D_EmployeeNumber"/>
</xsl:element>
<xsl:element name="BoroughWHName">
<xsl:value-of select="D_WHName"/>
</xsl:element>
</Record>
</xsl:for-each>
</Records>
</xsl:variable>
<xsl:variable name="TownshipRecords">
<Records>
<xsl:for-each select="Export/Record[(D_EquityAward = 'True' and D_WHType ='Township' and D_WHAmt != 0.00)]"> <!-- Only County and Equity -->
<Record>
<xsl:element name="EmpNumber">
<xsl:value-of select="D_EmployeeNumber"/>
</xsl:element>
<xsl:element name="TownshipWHName">
<xsl:value-of select="D_WHName"/>
</xsl:element>
</Record>
</xsl:for-each>
</Records>
</xsl:variable>
<xsl:for-each select="Export/Record[(D_EquityAward = 'True' and D_WHType ='Main')]">
<xsl:value-of select="D_EmployeeNumber"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="D_LastName"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="D_FirstName"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="D_MiddleName"/>
<xsl:text>,</xsl:text>
<xsl:if test="(./D_EmployeeNumber= msxsl:node-set($CountyRecords)/Records/Record/EmpNumber)">
<xsl:value-of select="msxsl:node-set($CountyRecords)/Records/Record/CountyWHName" />
</xsl:if>
<xsl:text>,</xsl:text>
<xsl:if test="(./D_EmployeeNumber = msxsl:node-set($CityRecords)/Records/Record/EmpNumber)">
<xsl:value-of select="msxsl:node-set($CityRecords)/Records/Record/CityWHName" />
</xsl:if>
<xsl:text>,</xsl:text>
<xsl:if test="(./D_EmployeeNumber = msxsl:node-set($TownshipRecords)/Records/Record/EmpNumber)">
<xsl:value-of select="msxsl:node-set($TownshipRecords)/Records/Record/TownshipWHName" />
</xsl:if>
<xsl:text>,</xsl:text>
<xsl:if test="(./D_EmployeeNumber = msxsl:node-set($BoroughRecords)/Records/Record/EmpNumber)">
<xsl:value-of select="msxsl:node-set($BoroughRecords)/Records/Record/BoroughWHName" />
</xsl:if>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
预期结果:
1,A,Scottie,E,,,,
3,C,Kelly,D,,,,
4,D,Janet,A,FrdrckCoW/H,,,
5,E,John,E,WashCoW/H-MD,,,
6,F,Todd,E,,,WashCoW/H-MD,
7,G,Melissa,L,FrdrckCoW/H,,,
我在这里所做的是基于每种税收类型创建节点集,然后在我的最终For each中将它们链接到特定的节点集(如果员工有这些税收类型之一(。
我这样做的问题是,我只得到节点集第一行的值。
有人能解决这个问题吗?
谢谢你-Steve
如果有一种不那么复杂的方法来实现这一点,我想知道。
如果没有对此处需要应用的逻辑的明确说明,很难从一个示例(甚至不返回预期结果(开始工作。我猜你想做这样的事情:
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="grp" match="Record[D_EquityAward = 'True' and D_WHAmt != 0]" use="D_EmployeeNumber" />
<xsl:template match="/Export">
<!-- FOR EACH MAIN RECORD -->
<xsl:for-each select="Record[D_EquityAward = 'True' and D_WHType ='Main']">
<!-- COMMON FIELDS -->
<xsl:value-of select="D_EmployeeNumber"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="D_LastName"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="D_FirstName"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="D_MiddleName"/>
<xsl:text>,</xsl:text>
<!-- DATA FROM OTHER RECORDS OF THIS EMPLOYEE: -->
<xsl:variable name="group" select="key('grp', D_EmployeeNumber)"/>
<!-- 1. COUNTY NAME -->
<xsl:value-of select="$group[D_WHType ='County']/D_WHName"/>
<xsl:text>,</xsl:text>
<!-- 2. CITY NAME -->
<xsl:value-of select="$group[D_WHType ='City']/D_WHName"/>
<xsl:text>,</xsl:text>
<!-- 3. TOWNSHIP NAME -->
<xsl:value-of select="$group[D_WHType ='Township']/D_WHName"/>
<xsl:text>,</xsl:text>
<!-- 4. BOROUGH NAME -->
<xsl:value-of select="$group[D_WHType ='Borough']/D_WHName"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
当应用于您的输入示例时,结果将是:
1,A,Scottie,E,,,,
3,C,Kelly,D,,,,
4,D,Janet,A,XFrdrckCoW/H,,,
5,E,John,E,WashCoW/H-MD,,,
6,F,Todd,E,,,BtlrTwpW/H-L,
7,G,Melissa,L,FrdrckCoW/H,,,
这与您当前的结果和问题中发布的预期结果都不同。
演示:https://xsltfiddle.liberty-development.net/ehVZvvN/4