【问题标题】:XSLT, Renaming Elements using mapping table based on Attribute's valueXSLT,使用基于属性值的映射表重命名元素
【发布时间】:2017-10-12 11:29:39
【问题描述】:

如何根据元素属性的映射表重命名元素?

任何建议将不胜感激。 也许是 XSLT 2.0 模板并修改映射表以获得更好的解决方案?

非常感谢提前 托马斯

原始 XML:

<transaction>
  <records type="1" >
      <record type="1" >
        <field number="1" >
            <item>223</item>
        </field>
        <field number="2" >
            <item>456</item>
        </field>
      </record>
  </records>

  <records type="14" >
      <record type="14" >
        <field number="1" >
            <item>777</item>
        </field>
        <field number="2" >
            <item>678</item>
        </field>
      </record>

      <record type="14" >
        <field number="1" >
            <item>555</item>
        </field>
      </record>
  </records>
</transaction>

映射表:

<xsl:stylesheet>
  <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:stylesheet>

改造后的结果:

<transaction>
  <records type="1" >
      <first-record type="1" >
        <great-field number="1" >
            <item >223</item>
        </great-field>
        <good-field number="2" >
            <item >456</item>
        </good-field>
      </first-record>
  </records>

  <records type="14" >
      <real-record type="14" >
        <my-field number="1" >
            <item >777</item>
        </my-field>
        <other-field number="2" >
            <item >678</item>
        </other-field>
      </real-record>

      <real-record type="14" >
        <my-field number="1" >
            <item >555</item>
        </my-field>
      </real-record>
  </records>
</transaction>

【问题讨论】:

    标签: xml xslt transformation


    【解决方案1】:

    这是一个 XSLT 3.0 样式表(可与 Saxon 9.8 任何版本或 Altova XMLSpy/Raptor 2017 或 2018 一起执行),它将映射转换为 XSLT 3.0 样式表,然后使用 XPath 3.1 中的 transform 函数执行它:

    <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>
    

    删除或注释掉&lt;xsl:message select="$stylesheet"&gt;&lt;/xsl:message&gt;,它只是在那里显示生成的样式表代码。

    当然,将映射转换为 XSLT 样式表的方法也可以用于 XSLT 2.0,但是您需要从外部 XSLT 运行创建的样式表,例如Java 或 C# 或在您的编辑器中手动操作。

    【讨论】:

    • 嗨,马丁,干得好。非常感谢您的快速回复。事实上,我忘了提及,我正在使用用 Java 8 编写的 RESTful WebService 来转换 XML。很抱歉缺少信息。我使用了您出色的模板,使用命令行在一个小型 Java 应用程序中转换了一些 XML 文件。它就像一个魅力:-) 再次感谢!托马斯
    • 如果我知道在这里得到解决方案的速度有多快,我会尝试立即描述整个任务。以下 XML 是我的起点。 - 我必须先删除所有命名空间。 - 将类型 {1, 2, 3, 4, .. 14} 分组。 (stackoverflow.com/questions/46693291/…) 其他类型,如 200、201 等将保留在根元素中(不需要它们的组)。 - 随后您的模板将根据映射表重命名元素。经过一些处理任务后,我将得到一个 XML,然后必须将其转换回原始格式!!!
    • ..com/ab" > 2 ab:it> 7 ab:field> 5记录> P交易>
    • 我应该为此创建一个新问题吗?请问有可能把我引向正确的方向吗?非常感谢您的努力
    • 我认为最好创建一个新问题,如果您想链接样式表,那么使用 XSLT 3.0 您可以选择在 XSLT 中使用transform 执行此操作,请参阅stackoverflow.com/a/40866026/252228。
    猜你喜欢
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多