XSLT 扁平到分层 [英] XSLT flat to hierarchical

查看:23
本文介绍了XSLT 扁平到分层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 xml 数据的平面文件,其中包含父子信息,我需要一个 xslt 来转换

I have a flat file with xml data that contains parent and child information i need a xslt to transform

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <CRStructure>
    <objid>CA0D2594-183B-4E80-B2CA-4F915A1E2D32</objid>
    <cr_id>87</cr_id>
    <cr_parent>14</cr_parent>
  </CRStructure>
  <CRStructure>
    <objid>23BD80FA-7ACE-4111-9607-8AC0857868AF</objid>
    <cr_id>172</cr_id>
    <cr_parent>128</cr_parent>
  </CRStructure>
  <CRStructure>
    <objid>E381DE99-AD82-428E-A82B-63EB965BA2F4</objid>
    <cr_id>247</cr_id>
    <cr_parent>138</cr_parent>
      </CRStructure>
  <CRStructure> ............

<CR  xmlns="http://ait.com/cr/">

  <CRItems>

    <CRItem ObjectId="69230491-BCB8-4CD5-9FC3-2113FFE832EC">
     <CrId>1</CrId> 

        <CRItems>

        <CRItem ObjectId="2D425940-B3B1-432E-BDA4-6778C9AE8391">
            <CrId>2</CrId>
             <CRItems>

            <CRItem ObjectId="3F2DF482-0485-42C9-A1D2-FCFE0EF6B4E3">
                <CrId>22</CrId>
              </CRItem> .........

层次结构总是三层,任何人都可以给我指明正确的方向.

The hierarchical structure is always three levels , anyone that can point me in the right direction.

编辑

试过了,好像可以,

  <xsl:template match="//NewDataSet">
     <CR>
       <CRItems>
     <xsl:for-each select="CRStructure[cr_parent='0']">
       <CRItem>
         <xsl:attribute name="ObjectId">
           <xsl:value-of select="objid" />
         </xsl:attribute>
         <CRItems>
         <xsl:variable name="cridequ" select="cr_id" />
         <xsl:for-each select="//NewDataSet/CRStructure[cr_parent=$cridequ]">
           <CRItem>
             <xsl:attribute name="ObjectId">
               <xsl:value-of select="objid" />
             </xsl:attribute>
             <CRItems>
               <xsl:variable name="cridsub" select="cr_id" />
               <xsl:for-each select="//NewDataSet/CRStructure[cr_parent=$cridsub]">
                 <CRItem>
                   <xsl:attribute name="ObjectId">
                     <xsl:value-of select="objid" />
                   </xsl:attribute>
                 </CRItem>
               </xsl:for-each>
             </CRItems>                          
           </CRItem>
         </xsl:for-each>
         </CRItems>
       </CRItem>   
    </xsl:for-each>
         </CRItems>
     </CR>
  </xsl:template>

不确定这是否是解决问题的正确方法,似乎我发现的大多数建议是使用 <xsl:template match.... 的某些组合,但不知道该怎么做这个

Not sure if this is the correct way of solving it , seems like most suggestions i find is to use some combinations of <xsl:template match.... but not sure how to do this

推荐答案

您的提议是正确的,但可以概括和简化为:

What you are proposing is correct but could be generalized and simplified as:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://ait.com/cr/" version="1.0">

    <xsl:output indent="yes"/>

    <xsl:key name="sons" match="CRStructure" use="cr_parent"/>
    <xsl:param name="maxLevels">3</xsl:param>

    <xsl:template match="NewDataSet">
        <CR>
            <CRItems>
                <xsl:apply-templates select="key('sons', 0)"/>
            </CRItems>
        </CR>
    </xsl:template>

    <xsl:template match="CRStructure">
        <xsl:param name="level">1</xsl:param>
        <xsl:if test="$level &lt;= $maxLevels">
            <CRItem ObjectId="{objid}">
                <CrId>
                    <xsl:value-of select="cr_id"/>
                </CrId>
                <CRItems>
                    <xsl:apply-templates select="key('sons', cr_id)">
                        <xsl:with-param name="level" select="$level  + 1"/>
                    </xsl:apply-templates>
                </CRItems>
            </CRItem>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

引入变量 $index 是为了避免在输入文档中出现错误时永远循环;) ...

The variable $index has been introduced to avoid to loop forever if there is an error in your input document ;) ...

这篇关于XSLT 扁平到分层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆