时间:2011-01-06 18:05:16

标签: xslt

1 个答案:

答案 0 :(得分:0)

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kElemByParentId" match="Employee/*" use="../@id"/>
    <xsl:param name="pNames" select="'Country department'"/>
    <xsl:variable name="vNames" select="concat(' ',$pNames,' ')"/>
    <xsl:variable name="vLookup" select="document('Lookup1.xml')|
                                         document('Lookup2.xml')"/>
    <xsl:variable name="vUpper" select="'QWERTYUIOPASDFGHJKLZXCVBNM'"/>
    <xsl:variable name="vLower" select="'qwertyuiopasdfghjklzxcvbnm'"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Employee/*[last()]">
        <xsl:variable name="vKey" select="../id"/>
        <xsl:call-template name="identity"/>
        <xsl:for-each select="$vLookup">
            <xsl:apply-templates select="key('kElemByParentId',$vKey)
                                          [contains($vNames,
                                                    concat(' ',name(),' '))]"
                                 mode="va-object"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="*" mode="va-object">
        <xsl:variable name="vName"
                      select="translate(name(),$vUpper,$vLower)"/>
        <va-object>
            <va-metadata>
                <related-content-ref area="decisions"
                                     cite="{@code}"
                                     relationship="{$vName}"
                                     relevance="100"/>
                <primary-class>
                    <super-class super-class="value-add"/>
                    <sub-class sub-class="{$vName}">
                        <xsl:value-of select="."/>
                    </sub-class>
                </primary-class>
            </va-metadata>
        </va-object>
    </xsl:template>
</xsl:stylesheet>

输出:

<Employees>
    <Employee>
        <id name="id">1</id>
        <firstname>ABC</firstname>
        <lastname>XYZ</lastname>
        <age>32</age>
        <va-object>
            <va-metadata>
                <related-content-ref area="decisions"
                                     cite="102"
                                     relationship="department"
                                     relevance="100" />
                <primary-class>
                    <super-class super-class="value-add" />
                    <sub-class sub-class="department">HR</sub-class>
                </primary-class>
            </va-metadata>
        </va-object>
        <va-object>
            <va-metadata>
                <related-content-ref area="decisions"
                                     cite="10"
                                     relationship="country"
                                     relevance="100" />
                <primary-class>
                    <super-class super-class="value-add" />
                    <sub-class sub-class="country">Canada</sub-class>
                </primary-class>
            </va-metadata>
        </va-object>
    </Employee>
</Employees>

注意:“按顺序”XPath 1.0测试:

contains(concat($separator,$sequence,$separator),
         concat($separator,$item,$separator))