消除 XSLT 1.0 中节点数据的重复
Eliminating duplication of node data in XSLT 1.0
提前致谢,抱歉,您可能需要一段时间才能理解我的逻辑(我不是使用 XSLT 的专家)。
任何人都可以帮助消除标签 "PaySlip_Val" 的重复并将所有 "ListOfPaySlipData" 个孩子分组在一个 parent "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,这 returns 预期结果:
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>
编辑:
I have updated the desired result. The condition is that I need to get
the First Name & Last name only if the "Payslip" tag has y or yes. In
other cases, it should not fetch the data (F2 & L2 as per example).
这样试试,然后:
<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>
提前致谢,抱歉,您可能需要一段时间才能理解我的逻辑(我不是使用 XSLT 的专家)。
任何人都可以帮助消除标签 "PaySlip_Val" 的重复并将所有 "ListOfPaySlipData" 个孩子分组在一个 parent "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,这 returns 预期结果:
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>
编辑:
I have updated the desired result. The condition is that I need to get the First Name & Last name only if the "Payslip" tag has y or yes. In other cases, it should not fetch the data (F2 & L2 as per example).
这样试试,然后:
<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>