将父节点添加到XML中的子节点

时间:2015-10-16 16:03:06

标签: xml recursion

我有一个非常简单的问题,但是时间很短,所以如果有人能提供快速答案,我将非常感激。基本上我在下面给出了一个XML:

 <Root>
<Delhi>
    <Population>1000</Population>`enter code here`
</Delhi>
<Mumbai>
    <Population>1000</Population>
    <District>
        <Name>Colaba</Name>
    </District>
    <District>
        <Name>navi Mumbai</Name>
    </District>
    <District></District>
</Mumbai>
<Mumbai>
    <Population>1000</Population>
</Mumbai>
<Mumbai>
    <Population>1000</Population>
</Mumbai>
<Chennai>
    <Population>1000</Population>
    <District>
        <Name>Chan</Name>
    </District>
</Chennai>
<Kolkata>
    <Population>1000</Population>
</Kolkata>

我想将此XML传递给方法,并希望得到如下所示的响应:

<Root>
<Delhi>
    <Population>1000</Population>
</Delhi>
<Mumbai>
    <Mumbai>
        <Population>1000</Population>
        <District>
            <District>
                <Name>Colaba</Name>
            </District>
            <District>
                <Name>navi Mumbai</Name>
            </District>
            <District></District>
        </District>
    </Mumbai>
    <Mumbai>
        <Population>1000</Population>
    </Mumbai>
    <Mumbai>
        <Population>1000</Population>
    </Mumbai>
</Mumbai>
<Chennai>
    <Chennai>
        <Population>1000</Population>
        <District>
            <District>
                <Name>Chan</Name>
            </District>
        </District>
    </Chennai>
</Chennai>
<Kolkata>
    <Population>1000</Population>
</Kolkata>

所以逻辑很简单:我想遍历根元素的节点,然后检查我的节点是Mumbai,District还是Chennai。如果是,我将该名称中的所有标记括在具有相同名称的标记内。 XML可以有任何级别,所以我认为它需要通过递归有效地完成。

1 个答案:

答案 0 :(得分:0)

考虑一种XSLT解决方案,大多数通用语言都支持包含Perl,PHP,Python,VB,Java和C#的库(还有其他)。作为信息,XSLT是声明性的,专用编程语言(与SQL相同,但与数据库相同)专门用于转换,样式化,重新格式化或重构XML文档。

请注意:此XSLT脚本会转换您的XML,但不会处理仅复制与您的文档无关的两个无关的 Mumbai节点

XSLT使用Muenchian Method将区项目组合在一起。

<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="MumbaiDistrict" match="Mumbai[1]" use="District" />
  <xsl:key name="ChennaiDistrict" match="Chennai" use="District" />

    <!-- Identity Transform -->
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>   

    <xsl:template match="Mumbai[1]">
      <xsl:element name="Mumbai">
        <xsl:element name="Mumbai">        
            <xsl:copy-of select="Population"/>
            <xsl:element name="District">
              <xsl:for-each select="key('MumbaiDistrict', District)">          
                <xsl:copy-of select="District"/>          
             </xsl:for-each>
           </xsl:element>          
         </xsl:element>
      </xsl:element>
    </xsl:template>

    <xsl:template match="Chennai">
      <xsl:element name="Chennai">
        <xsl:element name="Chennai">
           <xsl:copy-of select="Population"/>
           <xsl:element name="District">
             <xsl:for-each select="key('ChennaiDistrict', District)">          
              <xsl:copy-of select="District"/>          
             </xsl:for-each>
          </xsl:element>
        </xsl:element>
      </xsl:element>        
    </xsl:template>

</xsl:stylesheet>

<强>输出

<?xml version="1.0"?>
<Root>
  <Delhi><Population>1000</Population>`enter code here`
</Delhi>
  <Mumbai>
    <Mumbai>
      <Population>1000</Population>
      <District>
        <District>
          <Name>Colaba</Name>
        </District>
        <District>
          <Name>navi Mumbai</Name>
        </District>
        <District/>
      </District>
    </Mumbai>
  </Mumbai>
  <Mumbai>
    <Population>1000</Population>
  </Mumbai>
  <Mumbai>
    <Population>1000</Population>
  </Mumbai>
  <Chennai>
    <Chennai>
      <Population>1000</Population>
      <District>
        <District>
          <Name>Chan</Name>
        </District>
      </District>
    </Chennai>
  </Chennai>
  <Kolkata>
    <Population>1000</Population>
  </Kolkata>
</Root>