XSLT, Renaming Elements using mapping table based on Attribute's value

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 15:37:30

Here is an XSLT 3.0 stylesheet (executable with Saxon 9.8 any edition or Altova XMLSpy/Raptor 2017 or 2018) that transforms the mapping into an XSLT 3.0 stylesheet and then executes it with the transform function from XPath 3.1:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:axsl="http://www.w3.org/1999/XSL/Transform-alias"
    exclude-result-prefixes="xs math axsl"
    version="3.0">

    <xsl:param name="mapping">
        <mapping type="1" from="record" to="first-record">
            <map number="1" from="field" to="great-field"/>
            <map number="2" from="field" to="good-field"/>
        </mapping>

        <mapping type="14" from="record" to="real-record">
            <map number="1" from="field" to="my-field"/>
            <map number="2" from="field" to="other-field"/>
        </mapping>  
    </xsl:param>

    <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

    <xsl:variable name="stylesheet">
        <axsl:stylesheet version="3.0">
            <axsl:mode name="transform" on-no-match="shallow-copy"/>
            <xsl:apply-templates select="$mapping/mapping" mode="xslt-modes"/>
            <xsl:apply-templates select="$mapping/mapping" mode="xslt-code"/>
        </axsl:stylesheet>
    </xsl:variable>

    <xsl:template match="mapping" mode="xslt-modes">
        <axsl:mode name="transform-{position()}" on-no-match="shallow-copy"/>
    </xsl:template>

    <xsl:template match="mapping" mode="xslt-code">
        <axsl:template match="{@from}[@type = '{@type}']" mode="transform">
            <axsl:element name="{@to}">
                <axsl:apply-templates select="@* | node()" mode="transform-{position()}"/>
            </axsl:element>
        </axsl:template>
        <xsl:apply-templates mode="xslt-code">
            <xsl:with-param name="pos" select="position()"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="map" mode="xslt-code">
        <xsl:param name="pos"/>
        <axsl:template match="{@from}[@number = '{@number}']" mode="transform-{$pos}">
            <axsl:element name="{@to}">
                <axsl:apply-templates select="@* | node()" mode="#current"/>
            </axsl:element>
        </axsl:template>
    </xsl:template> 

    <xsl:template match="/">
        <xsl:message select="$stylesheet"></xsl:message>
        <xsl:sequence select="transform(map { 'source-node' : ., 'stylesheet-node' : $stylesheet , 'initial-mode' : xs:QName('transform') })?output"/>
    </xsl:template>

</xsl:stylesheet>

Remove or comment out the <xsl:message select="$stylesheet"></xsl:message> which is only in there to show the produced stylesheet code.

Of course the approach to transform the mapping into an XSLT stylesheet can also be used with XSLT 2.0 but then you need to run the created stylesheet from outside XSLT with e.g. Java or C# or by hand in your editor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!