XSLT将每个重复元素转换为唯一记录

时间:2014-11-25 04:05:47

标签: xml xslt

我需要为Project和Skill的每个重复元素创建一个唯一的记录。尝试使用我在XSLT中的已知选项,但我得不到正确的结果。

请帮我为下面的输入XML生成XSLT。

输入XML:

<root>
<Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>abc</Project>
 <Skill>java</Skill>
<Project>def</Project>
<Skill>unix</Skill>
<Project>efg</Project>
<Skill>xml</Skill>
<Project>pqr</Project>
<Skill>sql</Skill>
<Project>xyz</Project>
<Skill>Analytics</Skill>
</Record>
</root>

所需的输出XML:

 <root>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>abc</Project>
<Skill>java</Skill>
</Record>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>def</Project>
<Skill>unix</Skill>
</Record>
<Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>efg</Project>
<Skill>xml</Skill>
</Record>
 <Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>pqr</Project>
<Skill>sql</Skill>
</Record>
<Record>
<Emp_ID>288237</Emp_ID>
<Emp_Name>John</Emp_Name>
<Country>US</Country>
<Manager>Wills</Manager>
<Join_Date>5/12/2014</Join_Date>
<Experience>9 years</Experience>
<Project>xyz</Project>
<Skill>Analytics</Skill>
</Record>
</root>

2 个答案:

答案 0 :(得分:0)

您可以使用以下XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"  />
<xsl:template match="root">
    <root>
        <xsl:for-each select="Record/Project">
            <Record>
            <xsl:copy-of select="../Emp_ID"/>
            <xsl:copy-of select="../Emp_Name"/>
            <xsl:copy-of select="../Country"/>
            <xsl:copy-of select="../Join_Date"/>
            <xsl:copy-of select="../Experience"/>
            <xsl:copy-of select="."/>
            <xsl:copy-of select="following::Skill[1]"/>
            </Record>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

答案 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="/">
    <root>
        <xsl:for-each select="root/Record/Project">
            <Record>
                <xsl:copy-of select="../*[not(self::Project or self::Skill)]"/>
                <xsl:copy-of select=". | following-sibling::Skill[1]"/>
            </Record>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>