移动元素并按子

时间:2018-06-07 08:26:32

标签: xslt

我试图仅在子记录中移动元素。当我对一个Employee运行我的xsl时,结果是所希望的。但是,针对两个或更多员工运行xsl的结果并不理想。

XML

<?xml version='1.0' encoding='UTF-8'?>
<xx:Payroll_Extract_Employees xmlns:xx="urn:com.me/a">
<xx:PayGroup>
<xx:Header>
<xx:recs>2</xx:recs>
</xx:Header>
<xx:Employee>
<xx:Summary>
<xx:Employee_ID>0000000aa</xx:Employee_ID>
</xx:Summary>
<xx:Position>
<xx:Position_ID xx:PriorValue="">Pos1</xx:Position_ID>
<xx:Organization_One xx:PriorValue="">bad1</xx:Organization_One>
<xx:Organization_Three xx:PriorValue="">good1</xx:Organization_Three>
</xx:Position>
<xx:Additional_Information>
<xx:Payroll_ID xx:PriorValue="">a001</xx:Payroll_ID>
<xx:Organization_One xx:PriorValue="a">aa1</xx:Organization_One>
<xx:Organization_Two xx:PriorValue="a">aa2</xx:Organization_Two>
<xx:Organization_Four xx:PriorValue="a">aa3</xx:Organization_Four>
</xx:Additional_Information>
</xx:Employee>
<xx:Employee>
<xx:Summary>
<xx:Employee_ID>0000000bb</xx:Employee_ID>
</xx:Summary>
<xx:Position>
<xx:Position_ID xx:PriorValue="">Pos2</xx:Position_ID>
<xx:Organization_One xx:PriorValue="">bad2</xx:Organization_One>
<xx:Organization_Three xx:PriorValue="">good2</xx:Organization_Three>
</xx:Position>
<xx:Additional_Information>
<xx:Payroll_ID xx:PriorValue="">b001</xx:Payroll_ID>
<xx:Organization_One xx:PriorValue="b">bb1</xx:Organization_One>
<xx:Organization_Two xx:PriorValue="b">bb2</xx:Organization_Two>
<xx:Organization_Four xx:PriorValue="b">bb3</xx:Organization_Four>
</xx:Additional_Information>
</xx:Employee>
</xx:PayGroup>
</xx:Payroll_Extract_Employees>

的xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 version="1.0" xmlns:xx="urn:com.me/a">
<!-- Identity transform -->
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="xx:Employee_ID">
<xsl:copy-of select="."/>
<xsl:copy-of      
Select="/xx:Payroll_Extract_Employees/xx:PayGroup/xx:Employee  
/xx:Additional_Information/xx:Payroll_ID"/>
</xsl:template>

<xsl:template match="xx:Position/xx:Organization_One"></xsl:template>

<xsl:template match="xx:Position/xx:Position_ID">
<xsl:copy-of select="."/>
<xsl:copy-of select="//xx:Additional_Information/xx:Organization_One"/>
<xsl:copy-of select="//xx:Additional_Information/xx:Organization_Two"/>
<xsl:copy-of select="//xx:Additional_Information/xx:Organization_Four"/>
</xsl:template>
</xsl:stylesheet>

输出:

    <?xml version="1.0" encoding="UTF-8"?>
    <xx:Payroll_Extract_Employees xmlns:xx="urn:com.me/a">
    <xx:PayGroup>
    <xx:Header>
    <xx:recs>2</xx:recs>
    </xx:Header>
    <xx:Employee>
    <xx:Summary>
    <xx:Employee_ID>0000000aa</xx:Employee_ID>
    <xx:Payroll_ID xx:PriorValue="">a001</xx:Payroll_ID>
    <xx:Payroll_ID xx:PriorValue="">b001</xx:Payroll_ID>
    </xx:Summary>
    <xx:Position>
    <xx:Position_ID xx:PriorValue="">Pos1</xx:Position_ID>
    <xx:Organization_One xx:PriorValue="a">aa1</xx:Organization_One>
    <xx:Organization_One xx:PriorValue="b">bb1</xx:Organization_One>
    <xx:Organization_Two xx:PriorValue="a">aa2</xx:Organization_Two>
    <xx:Organization_Two xx:PriorValue="b">bb2</xx:Organization_Two>
    <xx:Organization_Four xx:PriorValue="a">aa3</xx:Organization_Four>
    <xx:Organization_Four xx:PriorValue="b">bb3</xx:Organization_Four>
    <xx:Organization_Three xx:PriorValue="">good1</xx:Organization_Three>
    </xx:Position>
    <xx:Additional_Information>
    <xx:Payroll_ID xx:PriorValue="">a001</xx:Payroll_ID>
    <xx:Organization_One xx:PriorValue="a">aa1</xx:Organization_One>
    <xx:Organization_Two xx:PriorValue="a">aa2</xx:Organization_Two>
    <xx:Organization_Four xx:PriorValue="a">aa3</xx:Organization_Four>
    </xx:Additional_Information>
    </xx:Employee>
    <xx:Employee>
    <xx:Summary>
    <xx:Employee_ID>0000000bb</xx:Employee_ID>
    <xx:Payroll_ID xx:PriorValue="">a001</xx:Payroll_ID>
    <xx:Payroll_ID xx:PriorValue="">b001</xx:Payroll_ID>
    </xx:Summary>
    <xx:Position>
    <xx:Position_ID xx:PriorValue="">Pos2</xx:Position_ID>
    <xx:Organization_One xx:PriorValue="a">aa1</xx:Organization_One>
    <xx:Organization_One xx:PriorValue="b">bb1</xx:Organization_One>
    <xx:Organization_Two xx:PriorValue="a">aa2</xx:Organization_Two>
    <xx:Organization_Two xx:PriorValue="b">bb2</xx:Organization_Two>
    <xx:Organization_Four xx:PriorValue="a">aa3</xx:Organization_Four>
    <xx:Organization_Four xx:PriorValue="b">bb3</xx:Organization_Four>
    <xx:Organization_Three xx:PriorValue="">good2</xx:Organization_Three>
    </xx:Position>
    <xx:Additional_Information>
    <xx:Payroll_ID xx:PriorValue="">b001</xx:Payroll_ID>
    <xx:Organization_One xx:PriorValue="b">bb1</xx:Organization_One>
    <xx:Organization_Two xx:PriorValue="b">bb2</xx:Organization_Two>
    <xx:Organization_Four xx:PriorValue="b">bb3</xx:Organization_Four>
    </xx:Additional_Information>
    </xx:Employee>
    </xx:PayGroup>
    </xx:Payroll_Extract_Employees>

对于员工1,我不想要任何以&#34; b&#34;开头的价值。对于员工2,我不希望任何以&#34; a&#34;开头的价值。

是否可以添加一个简单的parm来限制员工将模板应用到员工身上?

1 个答案:

答案 0 :(得分:1)

问题在于表达......

<xsl:copy-of select="/xx:Payroll_Extract_Employees/xx:PayGroup/xx:Employee/xx:Additional_Information/xx:Payroll_ID"/>

通过使用/启动表达式,您正在从文档节点中搜索,而不是您所在的当前节点,因此您最终将选择文档中的所有Payroll_ID个节点。

由于您希望将其限制为当前正在处理的Employee,您应该这样做...

<xsl:copy-of select="ancestor::xx:Employee/xx:Additional_Information/xx:Payroll_ID"/>

同样适用于Organization_???选择

<xsl:copy-of select="//xx:Additional_Information/xx:Organization_One"/>

这应该成为这个......

<xsl:copy-of select="ancestor::xx:Employee/xx:Additional_Information/xx:Organization_One"/>