XSL 转换解决方案 [英] XSL Transformation Solution
问题描述
在我的映射项目中,我有一个基于此 xml 结构的 xsd 作为源.注意大写的节点名称和属性值:
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>
和其他基于此 xml 的 xsd 为目标.请注意小写名称和元素值:
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>
如何使用 Altova MapForce 2017 进行 XSL 转换,按树节点分组,类似于这样?注意节点注释和变量名称:
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>
Altova MapForce 创建了这个不同的解决方案:
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>
推荐答案
如果您的目的是将第一个 XML 转换为第二个 XML,那么您为什么不简单地做:
If your purpose is to transform the first XML into the second one, then why don't you do simply:
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>
这里不涉及分组 - 只是重命名节点并将属性转换为元素.
There is no grouping involved here - just renaming nodes and converting attributes to elements.
这篇关于XSL 转换解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!