【问题标题】:XML to XML using XSL ProblemXML 到 XML 使用 XSL 问题
【发布时间】:2010-01-25 15:51:10
【问题描述】:

我想使用 XSL 将一个 XML 文件转换为另一个具有不同结构的 XML 文件,而且我对 XSL 还很陌生。 XML 的输入部分如下所示:

<set>
    <object> Value </object>
        <name> Value </name>
        <tag>
            <event> Value </event>
            <group> Value </group>
            <other> Value </other>
        </tag>
    <object>
    <object>...</object>
</set>

我想要的输出是:

<set>
    <event>
        <group>
            <object name="value">
                <tag>
                    <other> Value </other>
                </tag>
            </object>
        <group>
        <group>...</group>
    <event>...</event>
</set>

意思是说我想搜索输入 xml 并根据“事件”节点和具有相同值的“组”节点对对象进行分组。帮忙?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    如果您的 xslt 处理器支持 exslt,您可以使用 set:distinct 循环遍历不同的事件和具有该事件的不同组。我用 xsltproc 测试了以下内容,但它们应该可以在任何支持 exslt:function 的处理器中工作。见:http://www.exslt.org/func/elements/function/index.html

    请注意,在 set:distinct 的实现中存在一个错误,我已在此处为您修复。

    test.xslt:

    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:func="http://exslt.org/functions"
                    xmlns:set="http://exslt.org/sets"
                    extension-element-prefixes="func set">
    
    <xsl:import href="set.distinct.function.xsl" />
    
    <xsl:template match="set">
      <set>
      <xsl:variable name="set" select="."/>
      <xsl:value-of select="count(object/tag/event)"/><xsl:text> </xsl:text>
      <xsl:value-of select="count(set:distinct(object/tag/event))"/>
      </xsl:for-each>
      <xsl:for-each select="set:distinct(object/tag/event)">
        <event>
        <xsl:attribute name="name">
          <xsl:value-of select="."/>
        </xsl:attribute>
        <xsl:variable name="event" select="." />
        <xsl:for-each select="set:distinct($set/object/tag[event=$event]/group)">
          <group>
          <xsl:attribute name="name">
            <xsl:value-of select="."/>
          </xsl:attribute>
          <xsl:variable name="group" select="." />
          <xsl:apply-templates select="$set/object[tag/event=$event][tag/group=$group]" />
          </group>
        </xsl:for-each>
        </event>
      </xsl:for-each>
      </set>
    </xsl:template>
    <xsl:template match="object">
      <object name="{name}"/>
    </xsl:template>
    </xsl:stylesheet>
    

    set.distinct.function.xsl:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:exsl="http://exslt.org/functions"
                    xmlns:set="http://exslt.org/sets"
                    extension-element-prefixes="exsl"
                    exclude-result-prefixes="set">
    
    <exsl:function name="set:distinct">
       <xsl:param name="nodes" select="/.." />
       <xsl:choose>
          <xsl:when test="not($nodes)">
             <exsl:result select="/.." />
          </xsl:when>
          <xsl:otherwise>
             <xsl:variable name="distinct" 
                           select="set:distinct($nodes[position() > 1])" />
             <exsl:result select="$distinct | $nodes[1][not(. = $distinct)]" />
          </xsl:otherwise>
       </xsl:choose>
    </exsl:function>
    
    </xsl:stylesheet>
    

    test.xml:

    <set>
        <object>
            <name> Value1 </name>
            <tag>
                <event> Value </event>
                <group> Value </group>
                <other> Value </other>
            </tag>
        </object>
        <object>
            <name> Value2 </name>
            <tag>
                <event> Value </event>
                <group> Value2 </group>
                <other> Value </other>
            </tag>
        </object>
        <object>
            <name> Value3 </name>
            <tag>
                <event> Value </event>
                <group> Value2 </group>
                <other> Value </other>
            </tag>
        </object>
        <object>
            <name> Value4 </name>
            <tag>
                <event> Value </event>
                <group> Value </group>
                <other> Value </other>
            </tag>
        </object>
        <object>
            <name> Value5 </name>
            <tag>
                <event> Value2 </event>
                <group> Value </group>
                <other> Value </other>
            </tag>
        </object>
        <object>
            <name> Value6 </name>
            <tag>
                <event> Value2 </event>
                <group> Value </group>
                <other> Value </other>
            </tag>
        </object>
    </set>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-13
      • 2014-08-11
      • 2016-04-15
      • 2013-06-14
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多