XSL Transformation Solution

时间:2017-08-19 12:46:58

标签: xslt transformation altova map-force

In my mapping project, I have a xsd based on this xml structure for source. Note the capitalized node names and attribute values:

<ROOT>
    <INPUT>     
        <NODEA
            ATT1="xxx"
            ATT2="xxx" />
        <NODEB
            ATT1="xxx" 
            ATT2="xxx" />
    </INPUT>
</ROOT>

And other xsd based on this xml for target. Note the lowercase names and element values:

<operation> 
    <input>
        <nodea>
            <att1>xxx</att1>
            <att2>xxx</att2>
        </nodea>
        <nodeb>
            <att1>xxx</att1>
            <att2>xxx</att2>
        </nodeb>
    </input>
</operation>

How can I get an XSL transformation, grouping by tree nodes, similar like this, with Altova MapForce 2017? Note the node comments and the name of variables:

<xsl:template match="/ROOT">
    <operation>         
        <input>                     
            <xsl:for-each select="INPUT">           
                <!-- NODEA -->
                <xsl:for-each select="NODEA">                   
                    <!-- ATT1 -->
                    <xsl:for-each select="@ATT1">
                        <xsl:variable name="v_ATT1" select="."/>
                        <att1>
                            <xsl:value-of select="string($v_ATT1)"/>
                        </att1>
                    </xsl:for-each>                 
                    <!-- ATT2 -->
                    <xsl:for-each select="@ATT2">
                        <xsl:variable name="v_ATT2" select="."/>
                        <att2>
                            <xsl:value-of select="string($v_ATT2)"/>
                        </att2>
                    </xsl:for-each>                 
                </xsl:for-each>             
                <!-- NODEB -->
                <xsl:for-each select="NODEB">
                    <!-- ATT1 -->
                    ...
                    <!-- ATT2 -->
                    ...
                </xsl:for-each>             
            </xsl:for-each>
        </input>
    </operation>        
</xsl:template>

The Altova MapForce creates this different solution:

<xsl:stylesheet ... >
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="var1_initial" select="."/>
        <operation xmlns="...">
            <xsl:attribute ...>
            <input>
                <xsl:for-each select="*[local-name()='ROOT' and namespace-uri()='']">
                    <xsl:variable name="var2_cur" select="."/>
                    <att1>
                        <xsl:value-of select="*[local-name()='INPUT' and namespace-uri()='']/*[local-name()='NODEA' and namespace-uri()='']/@ATT1"/>
                    </att1>
                    ...
                </xsl:for-each>
            </input>
        </operation>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

如果您的目的是将第一个XML转换为第二个XML,那么为什么不简单地执行:

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:param name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:param name="lower-case" select="'abcdefghijklmnopqrstuvwxyz'"/>

<xsl:template match="/ROOT">
    <operation> 
        <xsl:apply-templates/>
    </operation> 
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{translate(name(), $upper-case, $lower-case)}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:element name="{translate(name(), $upper-case, $lower-case)}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

此处不涉及分组 - 只需重命名节点并将属性转换为元素。