How to turn flat xml array into tree? [closed]

柔情痞子 提交于 2019-12-13 10:27:59

问题


can someone help with with the following?

I need to convert

<categories type="array">
      <category type="array">
        <category-name><![CDATA[Categories]]></category-name>
        <category-name><![CDATA[BAGS & TRIPODS]]></category-name>
        <category-name><![CDATA[Bags & Cases]]></category-name>
        <category-name><![CDATA[soft cases]]></category-name>
        <category-name><![CDATA[camera]]></category-name>
      </category>
    </categories>

into

<Category>
        <Name>BAGS &amp; TRIPODS</Name>
        <Category>
          <Name>Bags &amp; Cases</Name>
          <Category>
            <Name>soft cases</Name>
            <Category>
              <Name>camera</Name>
            </Category>
          </Category>
        </Category>
      </Category>

This has to be in XSLT 1.0. Thanks!


回答1:


You mean that you want to turn a flat sequence into a tree in which each parent has exactly one child?

In the template for the parent element, apply templates not to all children but only to the first child:

  <xsl:template match="category[@type='array']">
    <xsl:apply-templates select="*[1]"/>    
  </xsl:template>

Then in the template for each of the children, handle that child by writing out a new Category element and its Name, and then apply templates to the immediately following sibling:

  <xsl:template match="category-name">
    <Category>
      <Name>
        <xsl:apply-templates/>
      </Name>
      <xsl:apply-templates select="following-sibling::*[1]"/>
    </Category>
  </xsl:template>

In your example, the initial item in the array seems to be dropped; we'll need special code for that:

  <xsl:template match="category-name
                       [normalize-space = 'Categories']">
    <xsl:apply-templates select="following-sibling::*[1]"/>
  </xsl:template>  

All together:

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

   <xsl:output method="xml" indent="yes"/>

  <xsl:template match="category[@type='array']">
    <xsl:apply-templates select="*[1]"/>    
  </xsl:template>

  <xsl:template match="category-name[normalize-space = 'Categories']">
    <xsl:apply-templates select="following-sibling::*[1]"/>
  </xsl:template>  

  <xsl:template match="category-name">
    <Category>
      <Name>
        <xsl:apply-templates/>
      </Name>
      <xsl:apply-templates select="following-sibling::*[1]"/>
    </Category>
  </xsl:template>

</xsl:stylesheet>

From the input you give, this produces the following:

<Category>
  <Name>Categories</Name>
  <Category>
    <Name>BAGS &amp; TRIPODS</Name>
    <Category>
      <Name>Bags &amp; Cases</Name>
      <Category>
        <Name>soft cases</Name>
        <Category>
          <Name>camera</Name>
        </Category>
      </Category>
    </Category>
  </Category>
</Category>


来源:https://stackoverflow.com/questions/14780330/how-to-turn-flat-xml-array-into-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!