XSLT:使用父节点elemnts填充子节点内容

时间:2015-11-29 10:49:00

标签: xslt

我正在尝试使用XSLT将XML从一种形式转换为另一种形式。

我输入的XML为

<Employee>
<Emp>
    <EMPID>123</EMPID>
    <NAME>James</NAME>
    <Sequence>1</Sequence>
    <JOBS>
        <COMPANY>ABC</COMPANY>
        <DESIGNATION>Developer</DESIGNATION>
    </JOBS>
    <JOBS>
        <COMPANY>DEF</COMPANY>
        <DESIGNATION>Tester</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>456</EMPID>
    <NAME>Clark</NAME>
    <Sequence>2</Sequence>
    <JOBS>
        <COMPANY>GHT</COMPANY>
        <DESIGNATION>QA Lead</DESIGNATION>
    </JOBS>
    <JOBS>
        <COMPANY>Levor</COMPANY>
        <DESIGNATION>Project Lead</DESIGNATION>
    </JOBS>
</Emp>
</Employee>

我想转换成(期望输出)

<Employee>
<Emp>
    <EMPID>123</EMPID>
    <NAME>James</NAME>
    <Sequence>1</Sequence>
    <JOBS>
        <COMPANY>ABC</COMPANY>
        <DESIGNATION>Developer</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>123</EMPID>
    <NAME>James</NAME>
    <Sequence>1</Sequence>
    <JOBS>
        <COMPANY>DEF</COMPANY>
        <DESIGNATION>Tester</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>456</EMPID>
    <NAME>Clark</NAME>
    <Sequence>2</Sequence>
    <JOBS>
        <COMPANY>GHT</COMPANY>
        <DESIGNATION>QA Lead</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>456</EMPID>
    <NAME>Clark</NAME>
    <Sequence>2</Sequence>
    <JOBS>
        <COMPANY>Levor</COMPANY>
        <DESIGNATION>Project Lead</DESIGNATION>
    </JOBS>
</Emp>
</Employee>

我在XSLT以下尝试了这个并且它没有用的不同路径,你能帮我纠正这个XSLT以获得欲望输出

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Employee">
    <xsl:element name="Employee">
        <xsl:for-each select="Employee">
            <xsl:sort select="Sequence"/>
            <xsl:element name="Emp">
                <xsl:copy-of select="EMPID"/>
                <xsl:copy-of select="NAME"/>
                <xsl:copy-of select="Sequence"/>
                <xsl:for-each select="child::JOBS">
                    <xsl:copy-of select="COMPANY"/>
                    <xsl:copy-of select="DESIGNATION"/>
                </xsl:for-each>
            </xsl:element>
        </xsl:for-each>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

请帮帮我?

加了:

感谢您的快速回复,但我忘了添加一个案例,即JOBS聚合不适用于所有员工,如下所示

<Employee>
<Emp>
    <EMPID>123</EMPID>
    <NAME>James</NAME>
    <Sequence>1</Sequence>
    <JOBS>
        <COMPANY>ABC</COMPANY>
        <DESIGNATION>Developer</DESIGNATION>
    </JOBS>
    <JOBS>
        <COMPANY>DEF</COMPANY>
        <DESIGNATION>Tester</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>789</EMPID>
    <NAME>Anderson</NAME>
    <Sequence>3</Sequence>
</Emp>
<Emp>
    <EMPID>456</EMPID>
    <NAME>Clark</NAME>
    <Sequence>2</Sequence>
    <JOBS>
        <COMPANY>GHT</COMPANY>
        <DESIGNATION>QA Lead</DESIGNATION>
    </JOBS>
    <JOBS>
        <COMPANY>Levor</COMPANY>
        <DESIGNATION>Project Lead</DESIGNATION>
    </JOBS>
</Emp>
</Employee>

所以Anderson没有emp,结果应该是这样的

<Employee>
<Emp>
    <EMPID>123</EMPID>
    <NAME>James</NAME>
    <Sequence>1</Sequence>
    <JOBS>
        <COMPANY>ABC</COMPANY>
        <DESIGNATION>Developer</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>123</EMPID>
    <NAME>James</NAME>
    <Sequence>1</Sequence>
    <JOBS>
        <COMPANY>DEF</COMPANY>
        <DESIGNATION>Tester</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>456</EMPID>
    <NAME>Clark</NAME>
    <Sequence>2</Sequence>
    <JOBS>
        <COMPANY>GHT</COMPANY>
        <DESIGNATION>QA Lead</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>456</EMPID>
    <NAME>Clark</NAME>
    <Sequence>2</Sequence>
    <JOBS>
        <COMPANY>Levor</COMPANY>
        <DESIGNATION>Project Lead</DESIGNATION>
    </JOBS>
</Emp>
<Emp>
    <EMPID>789</EMPID>
    <NAME>Anderson</NAME>
    <Sequence>3</Sequence>
</Emp>
</Employee>

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

编辑以回应澄清:

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="*"/>

<xsl:template match="/Employee">
    <xsl:copy>
        <xsl:apply-templates select="Emp">
            <xsl:sort select="Sequence"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template> 

<xsl:template match="Emp[not(JOBS)]">
     <xsl:copy-of select="."/>
</xsl:template>     

<xsl:template match="Emp">
    <xsl:apply-templates select="JOBS"/>
</xsl:template>                 

<xsl:template match="JOBS">
    <Emp>
        <xsl:copy-of select="../EMPID | ../NAME | ../Sequence | ."/>
    </Emp>
</xsl:template>

</xsl:stylesheet>