使用XSLT组合基于多个标签的多个值的记录

时间:2017-06-27 08:39:48

标签: xslt

我对我试过的一个文件感到困惑 使用 XSLT。

创建

我想要这种类型的输出:

EmpID|Languages|LanguageAbility|LanguagePro|Certificate-Issuer|Certification|Certificate-ID
000626390|English|Overall||C1|C2|

000626390|Turkish|Overall||||

XML:这是我的代码的示例XML

    <wd:Report_Data xmlns:wd="urn:com.workday.report/Languages">
                <wd:Report_Entry>
                    <wd:EmpID>000626390</wd:EmpID>
                    <wd:Languages>
                        <wd:Languages>English</wd:Languages>
                        <wd:LanguageAbility>Overall</wd:LanguageAbility>
                    </wd:Languages>
                    <wd:Languages>
                        <wd:Languages>Turkish</wd:Languages>
                        <wd:LanguageAbility>Overall</wd:LanguageAbility>
                    </wd:Languages>
                    <wd:Certifications>               
                        <wd:Certificate-Issuer>C1</wd:Certificate-Issuer>
                        <wd:Certification>C2</wd:Certification>
                    </wd:Certifications>
                </wd:Report_Entry>
            </wd:Report_Data>

1 个答案:

答案 0 :(得分:0)

由于您的示例x​​ml不包含任何LanguagePro和Certficate-ID,因此我们假设它是语言的兄弟,

XSLT 2.0

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wd="urn:com.workday.report/Languages"
    version="2.0">
    <xsl:template match="wd:Report_Entry">
        <xsl:for-each select="wd:Languages">
            <xsl:variable name="position" select="position()"/>
            <xsl:if test="position() != 1"><xsl:text>&#xa;</xsl:text></xsl:if>
            <xsl:value-of select="parent::wd:Report_Entry/wd:EmpID
                |wd:Languages
                |wd:LanguageAbility
                " separator="|"></xsl:value-of>
            <xsl:text>|</xsl:text>
            <xsl:value-of select="parent::wd:Report_Entry/LanguagePro"/>
            <xsl:text>|</xsl:text>
            <xsl:value-of select="parent::wd:Report_Entry/wd:Certifications[$position]/wd:Certificate-Issuer"/>
            <xsl:text>|</xsl:text>
            <xsl:value-of select="parent::wd:Report_Entry/wd:Certifications[$position]/wd:Certification"/>
            <xsl:text>|</xsl:text>
            <xsl:value-of select="parent::wd:Report_Entry/Certificate-ID"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>