提前感谢&很抱歉,您可能需要一段时间才能理解我的逻辑(我不赞成使用XSLT)。
有人能帮助消除标签"PaySlip_Val"的重复并将所有"ListOfPaySlipData"子项分组到一个父项"ListOfPaySlipMonth"下吗。
下面是XML层次结构,
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<P>
<FstName>F1</FstName>
<LstName>L1</LstName>
<Type>
<Payslip>y</Payslip>
<Details>
<Year>2016</Year>
<Month>Jan</Month>
<Amount>$$$</Amount>
</Details>
<Details>
<Year>2016</Year>
<Month>Feb</Month>
<Amount>$$$</Amount>
</Details>
</Type>
<Type>
<Payslip>yes</Payslip>
<Details>
<Year>2016</Year>
<Month>Mar</Month>
<Amount>$$$</Amount>
</Details>
<Details>
<Year>2016</Year>
<Month>Apr</Month>
<Amount>$$$</Amount>
</Details>
</Type>
<Type>
<Payslip>n</Payslip>
<Details>
<Year>2016</Year>
<Month>May</Month>
<Amount>$$$</Amount>
</Details>
</Type>
</P>
<P>
<FstName>F2</FstName>
<LstName>L2</LstName>
<Type>
<Payslip>n</Payslip>
<Details>
<Year>2016</Year>
<Month>Feb</Month>
<Leaves>4</Leaves>
</Details>
</Type>
</P>
</Data>
我已经申请了以下XSLT、
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:template match="/">
<xsl:call-template name="Pay_Slip" />
</xsl:template>
<xsl:template name="Pay_Slip">
<xsl:element name="ListOfData">
<xsl:for-each select="Data/P">
<xsl:element name="First_Name">
<xsl:value-of select="FstName" />
</xsl:element>
<xsl:element name="Last_Name">
<xsl:value-of select="LstName" />
</xsl:element>
<xsl:element name="PaySlip">
<xsl:for-each select="Type">
<xsl:variable name="Type" select="Payslip" />
<xsl:if test="$Type ='y' or $Type ='yes'">
<xsl:element name="PaySlip_Val">
<xsl:value-of select="$Type" />
</xsl:element>
<xsl:element name="ListOfPaySlipMonth">
<xsl:for-each select="Details">
<xsl:element name="ListOfPaySlipData">
<xsl:element name="Month">
<xsl:value-of select="Month" />
</xsl:element>
<xsl:element name="Salary">
<xsl:value-of select="Amount" />
</xsl:element>
<xsl:element name="Year">
<xsl:value-of select="Year" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
结果是:
<?xml version="1.0" encoding="UTF-8"?>
<ListOfData>
<First_Name>F1</First_Name>
<Last_Name>L1</Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Jan</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Feb</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
<PaySlip_Val>yes</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Mar</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Apr</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
</PaySlip>
<First_Name>F2</First_Name>
<Last_Name>L2</Last_Name>
<PaySlip/>
</ListOfData>
期望结果:
<ListOfData>
<ListOfP>
<First_Name>F1</First_Name>
<Last_Name>L1</Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Jan</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Feb</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Mar</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Apr</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
</PaySlip>
</ListOfP>
</ListOfData>
AFAICT,返回预期结果:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Data">
<ListOfData>
<xsl:apply-templates/>
</ListOfData>
</xsl:template>
<xsl:template match="P">
<First_Name><xsl:value-of select="FstName"/></First_Name>
<Last_Name><xsl:value-of select="LstName"/></Last_Name>
<xsl:variable name="payslips" select="Type[Payslip='yes' or Payslip='y']" />
<PaySlip>
<xsl:if test="$payslips">
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<xsl:apply-templates select="Type[Payslip='yes' or Payslip='y']/Details"/>
</ListOfPaySlipMonth>
</xsl:if>
</PaySlip>
</xsl:template>
<xsl:template match="Details">
<ListOfPaySlipData>
<xsl:copy-of select="Month"/>
<Salary><xsl:value-of select="Amount"/></Salary>
<xsl:copy-of select="Year"/>
</ListOfPaySlipData>
</xsl:template>
</xsl:stylesheet>
编辑:
我已经更新了想要的结果。条件是我需要名字&仅当"Payslip"标记为y或yes时才显示姓氏。在里面在其他情况下,它不应该获取数据(例如F2和L2)。
试试这个方法,然后:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Data">
<ListOfData>
<xsl:apply-templates select="P[Type/Payslip='yes' or Type/Payslip='y']"/>
</ListOfData>
</xsl:template>
<xsl:template match="P">
<First_Name><xsl:value-of select="FstName"/></First_Name>
<Last_Name><xsl:value-of select="LstName"/></Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<xsl:apply-templates select="Type[Payslip='yes' or Payslip='y']/Details"/>
</ListOfPaySlipMonth>
</PaySlip>
</xsl:template>
<xsl:template match="Details">
<ListOfPaySlipData>
<xsl:copy-of select="Month"/>
<Salary><xsl:value-of select="Amount"/></Salary>
<xsl:copy-of select="Year"/>
</ListOfPaySlipData>
</xsl:template>
</xsl:stylesheet>