2012-11-06 36 views
-1

这是我之前询问的问题的扩展 - XSLT 1.0 Grouping with multiple elements with same name 输出格式已更改并因此重新发布。对同一名称的多个元素进行分组

我有一个看起来像一个XML -

<resultset> 
    <hit> 
     <content> 
      <ITEM> 
       <TITLE>Office Cleaning1</TITLE> 
       <DESCRIPTION>blah blah blah</DESCRIPTION> 
       <Hierarchy>level1A~level2A~level3A</Hierarchy> 
       <Hierarchy>level1B~level2B~level3B</Hierarchy> 
      </ITEM> 
     </content> 
    </hit> 
    <hit> 
     <content> 
      <ITEM> 
       <TITLE>Office Cleaning2</TITLE> 
       <DESCRIPTION>blah blah blah</DESCRIPTION> 
       <Hierarchy>level1A~level2A~level3B</Hierarchy> 
      </ITEM> 
     </content> 
    </hit> 
    <hit> 
     <content> 
      <ITEM> 
       <TITLE>Office Cleaning3</TITLE> 
       <DESCRIPTION>blah blah blah</DESCRIPTION> 
       <Hierarchy>level1A~level2B~level3C</Hierarchy> 
      </ITEM> 
     </content> 
    </hit> 
    <hit> 
     <content> 
      <ITEM> 
       <TITLE>Office Cleaning4</TITLE> 
       <DESCRIPTION>blah blah blah</DESCRIPTION> 
       <Hierarchy>level1A~level2B~level3B</Hierarchy> 
       <Hierarchy>level1A~level2B~level3C</Hierarchy> 
      </ITEM> 
     </content> 
    </hit> 
    <hit> 
     <content> 
      <ITEM> 
       <TITLE>Office Cleaning5</TITLE> 
       <DESCRIPTION>blah blah blah</DESCRIPTION> 
       <Hierarchy>level1B~level2B~level3B</Hierarchy> 
      </ITEM> 
     </content> 
    </hit> 
</resultset> 

注意,有多个层次的元素是1级的连接字符串〜level2的〜3级,我希望将它变换成这样像这样 -

<TREE> 
<LEVELS> 
<LEVEL1 name="level1A"> 
<LEVEL2 name="level2A"> 
    <LEVEL3 name="level3A"> 
     <ITEM Name="Office Cleaning1"/> 
    </LEVEL3> 
    <LEVEL3 name="level3B"> 
     <ITEM Name="Office Cleaning2"/> 
    </LEVEL3> 
</LEVEL2> 
<LEVEL2 name="level2B"> 
    <LEVEL3 name="level3B"> 
     <ITEM Name="Office Cleaning4"/> 
    </LEVEL3> 
    <LEVEL3 name="level3C"> 
     <ITEM Name="Office Cleaning3"/> 
     <ITEM Name="Office Cleaning4"/> 
    </LEVEL3> 
</LEVEL2> 
</LEVEL1> 
<LEVEL1 name="level1B"> 
    <LEVEL2 name="level2B"> 
    <LEVEL3 name="level3B"> 
     <ITEM Name="Office Cleaning1"/> 
     <ITEM Name="Office Cleaning5"/> 
    </LEVEL3> 
    </LEVEL2> 
</LEVEL1> 
</TREE> 

基本上每个项目都有多个与它关联的层次结构。我需要将它们组合在一起,并将每个级别组合在一起。

我实际上可以将输入XML中的HIERARCHY元素中的值更改为可能更容易提取的任何格式。对于例如我可以使它看起来像LEVEL1:level1A〜LEVEL2:level2A〜LEVEL3:level3A 但我不能添加新的元素。

+0

您是否努力了解Dimitre提供的解决方案并对其进行修改?你需要付出一些努力,而不是一直要求人们做你的工作。 –

+0

是的,我一直在试图看到如何获得匹配的水平组合在一起。实际上,我的xml包含的数据远远多于示例中的数据,但基本的目的是了解如何将level2元素置于正确的level1-> level2下的正确的level1和level3元素下。以为有人会对如何实现这一目标有所了解。 – user1766784

+0

我无法确定如何更改dmitri代码以现在执行4个级别的分组。任何帮助表示赞赏。 – user1766784

回答

0

我明白了我的工作。忽略额外的命名空间。我编辑了层次结构字段,使其具有level1的格式:value〜level2:value〜level3:为便于子字符串的值。

我相信有更好的方法,但这对我很有用。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:autn="http://schemas.autonomy.com/aci/"> 
<xsl:output method="xml" omit-xml-declaration="yes"/> 

<xsl:strip-space elements="*"/> 

    <xsl:key name="TOPLEVEL" match="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY" use="substring-before(substring-after(.,'LEVEL1:'),'~')"/> 
    <xsl:key name="MIDLEVEL" match="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY" use="substring-before(substring-after(.,'LEVEL2:'),'~')"/> 
    <xsl:key name="BOTTOMLEVEL" match="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY" use="substring-before(substring-after(.,'LEVEL3:'),'~')"/> 


<xsl:template match="/"> 
<TREE> 
    <xsl:for-each select="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY[generate-id() = generate-id(key('TOPLEVEL',substring-before(substring-after(.,'LEVEL1:'),'~'))[1])]"> 
    <xsl:variable name="TOP" select="substring-before(substring-after(.,'LEVEL1:'),'~')"/> 
    <LEVEL1> 
     <xsl:attribute name="name"><xsl:value-of select="substring-after($TOP,'+')"/></xsl:attribute> 
     <xsl:attribute name="id"><xsl:value-of select="substring-before($TOP,'+')"/></xsl:attribute> 
      <xsl:for-each select="//autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY[substring-before(substring-after(.,'LEVEL1:'),'~')=$TOP and generate-id() = generate-id(key('MIDLEVEL',substring-before(substring-after(.,'LEVEL2:'),'~'))[1])]"> 
      <xsl:variable name="MID" select="substring-before(substring-after(.,'LEVEL2:'),'~')"/> 
       <LEVEL2> 
        <xsl:attribute name="name"><xsl:value-of select="substring-after($MID,'+')"/></xsl:attribute> 
        <xsl:attribute name="id"><xsl:value-of select="substring-before($MID,'+')"/></xsl:attribute> 
        <xsl:for-each select="//autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY[substring-before(substring-after(.,'LEVEL1:'),'~')=$TOP and substring-before(substring-after(.,'LEVEL2:'),'~')=$MID and generate-id() = generate-id(key('BOTTOMLEVEL',substring-before(substring-after(.,'LEVEL3:'),'~'))[1])]"> 
        <xsl:variable name="BOTTOM" select="substring-before(substring-after(.,'LEVEL3:'),'~')"/> 
        <LEVEL3> 
         <xsl:attribute name="name"><xsl:value-of select="substring-after($BOTTOM,'+')"/></xsl:attribute> 
         <xsl:attribute name="id"><xsl:value-of select="substring-before($BOTTOM,'+')"/></xsl:attribute> 
         <xsl:apply-templates select="//HIERARCHY[substring-before(substring-after(.,'LEVEL1:'),'~')=$TOP and substring-before(substring-after(.,'LEVEL2:'),'~')=$MID and substring-before(substring-after(.,'LEVEL3:'),'~')=$BOTTOM]"/> 
        </LEVEL3> 
        </xsl:for-each> 
       </LEVEL2> 
      </xsl:for-each> 
    </LEVEL1> 
    </xsl:for-each> 
</TREE> 
</xsl:template> 
    <xsl:template match="HIERARCHY"> 
    <ITEM> 
     <xsl:attribute name="name"><xsl:value-of select="../DRETITLE"/></xsl:attribute> 
     <xsl:attribute name="id"><xsl:value-of select="../ID"/></xsl:attribute> 
    </ITEM> 
    </xsl:template> 

</xsl:stylesheet> 
相关问题