使用XSLT将xml平面转换为分层XML转换

时间:2015-06-23 01:23:43

标签: xslt

我正在尝试创建一个将平面结构(带有父/子id)映射到层次结构的转换。我在下面列出了一个简单的请求和响应示例。实际数据有500多个元素。任何帮助将非常感激!谢谢!

验证规则:

  1. 优惠可以在顶级
  2. 优惠可以打包
  3. 优惠可以在
  4. 请求:

    <!-- offer with no parents(group or package) if No package_id or group ID in    ffer element-->
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>offer</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>OFR1</value>
        </namevaluepair>
    </offer>
    <!-- type package P1 & P2 representation-->
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>package</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>P1</value>
        </namevaluepair>
    </offer>
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>package</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>P2</value>
        </namevaluepair>
    </offer>
    <!-- offer under package but not in group-->
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>offer</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>OFRP1</value>
        </namevaluepair>
        <namevaluepair>
            <name>package_id</name>
            <value>P1</value>
        </namevaluepair>
    </offer>
    <!-- offers under package but not in group-->
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>offer</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>OFRP2</value>
        </namevaluepair>
        <namevaluepair>
            <name>package_id</name>
            <value>P2</value>
        </namevaluepair>
    </offer>
    <!-- type groups G1P1 and G1P2 representations, 
                group G1P1 is part of package P1 and this group G1P2 is part of package P2-->
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>group</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>G1P1</value>
        </namevaluepair>
        <namevaluepair>
            <name>package_id</name>
            <value>P1</value>
        </namevaluepair>
    </offer>
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>group</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>G2P2</value>
        </namevaluepair>
        <namevaluepair>
            <name>package_id</name>
            <value>P2</value>
        </namevaluepair>
    </offer>
    <!-- offer OFRG1P1 under group G1P1 as parent AND G1P1 is under P1 package-->
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>offer</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>OFRG1P1</value>
        </namevaluepair>
        <namevaluepair>
            <name>group_id</name>
            <value>G1P1</value>
        </namevaluepair>
    
    </offer>
    <!-- offer OFRG1P1 under group G2P2 as parent AND grp G2P2 is under P2 package-->
    <offer>
        <namevaluepair>
            <name>type</name>
            <value>offer</value>
        </namevaluepair>
        <namevaluepair>
            <name>id</name>
            <value>OFRG2P2</value>
        </namevaluepair>
        <namevaluepair>
            <name>group_id</name>
            <value>G2P2</value>
        </namevaluepair>
    
    </offer>
    

    输出:

    <offers>
        <!-- offer with no parents(group or package)-->
        <offer ID="OFR1"/>
        <packages>
            <package ID="P1">
                <groups>
                    <group ID="G1P1">
                        <offer ID="OFRG1P1"/>
                    </group>
                </groups>
                <offer ID="OFR1P1"/>
            </package>
            <package ID="P2">
                <groups>
                    <group ID="G2P2">
                        <offer ID="OFRG2P2"/>
                    </group>
                </groups>
                <offer ID="OFR1P2"/>
            </package>
        </packages>
    </offers>
    

1 个答案:

答案 0 :(得分:0)

这是一个使用keys的XSLT 1.0解决方案。

样式表声明了四个用于查找不同对象的键:

  1. 顶级对象的类型(包或商品),
  2. 按包ID分组
  3. 根据包ID和
  4. 提供属于包的内容
  5. 按群组ID提供属于某个群组。
  6. 在顶层,样式表处理顶级商品和包。在包级别,样式表处理属于该包的组和商品。在组级别,系统处理属于该组的商品。

    app.directive('sideNav', function() {
    return{
        restrict: 'E',
        templateUrl: 'side_nav.html',
        link: function(scope, element, attrs){
           // execute metisMenu plugin
            setTimeout(function(){
                $(element).metisMenu();
            }, 1);
        },
        controller: function(){
            this.selectedNav = {};
    
            this.selectTab = function(setTab){
                this.tab = setTab;
            };
            this.isSelected = function(checkTab){
                return checkTab === this.tab;
            };
    
            this.navItems = [{
                name: 'Dashboard',
                icon: 'fa-dashboard'
            },{
                name: 'Logs',
                icon: 'fa-code'
            },{
                name: 'Firm Diagnostics',
                icon: 'fa-stethoscope',
                navItems: [
                    {
                        name: 'Disk I/O'
                    },{
                        name: 'Disk Space'
                    },{
                        name: 'Processor Information'
                    },{
                        name: 'Processor Activity'
                    },{
                        name: 'Memory Information'
                    },{
                        name: 'Memory Usage'
                    },{
                        name: 'Network Configuration'
                    },{
                        name: 'Processes'
                    },{
                        name: 'Services'
                    },{
                        name: 'System Information'
                    }
                ]
            }];
    
        },
    
        controllerAs: 'sideNav'
    };});