1

我正在尝试基于level表示路径的元素从平面 XML 创建嵌套层次结构。每个level元素及其所属的兄弟姐妹(名称和编号不同)都应该被包装在一个“记录”元素中,从而创建一个树结构。

从这个来源(简化):

<?xml version="1.0" encoding="UTF-8"?>
    
<record>
    
    <level>first</level>
    
    <unitid>0001</unitid>
    <a-few-more-siblings/>
    
    <level>first/second</level>
    
    <unitid>0002</unitid>
    
    <many-more-siblings/>
    <level>first/second/third</level>
    
    <unitid>0003a</unitid>
    <some-more-siblings/>
    
    <level>first/second/third</level>
    
&lt;unitid>0003b</unitid>
    <many-more-siblings/>
    <level>first/second/third</level>
    
    <unitid>0003c</unitid>
    <some-more-siblings/>
 
    <level>first</level>
    
    <unitid>0004</unitid>

    <again-more-siblings/>
     
</record>

我想生成以下所需的输出

<Record level="first">

    <level>first</level>
    <unitid>001</unitid>
    <a-few-more-siblings/>
    <Record level="second">

        <level>second</level>
        <unitid>002</unitid>
        <many-more-siblings/>
        <Record level="third">
            <level>third</level>
            <unitid>003a</unitid>
            <some-more-siblings/>
        </Record>
        <Record level="third">

            <level>third</level>
            <unitid>003b</unitid>
            <many-more-siblings/>
        </Record>
        <Record level="third">

            <level>third</level>
            <unitid>003c</unitid>
            <some-more-siblings/>
        </Record>
    </Record>    
</Record>
<Record level="first">
    <level>first</level>
    <unitid>0004</unitid>
    <again-more-siblings/>
</Record>

到目前为止,我能生产的最接近的是:

<record level="first">
   <level>first</level>
   <unitid>0001</unitid>
   <some-other-siblings/>
   <record level="second">
      <level>first/second</level>
      <unitid>0002</unitid>
      <some-other-siblings/>
      <record level="third">
             <level>first/second</level>
             <unitid>0002</unitid>
             <some-other-siblings/>
         <level>first/second/third</level>
         <unitid>0003a</unitid>
         <some-other-siblings/>
      </record>
      <record level="third">
             <level>first/second</level>
             <unitid>0002</unitid>
             <some-other-siblings/>
             <level>first/second/third</level>
             <unitid>0003a</unitid>
             <some-other-siblings/>
         <level>first/second/third</level>
         <unitid>0003b</unitid>
         <some-other-siblings/>
      </record>
      <record level="third">
         <level>first/second/third</level>
         <unitid>0003c</unitid>
         <some-other-siblings/>
      </Record>
   </record>
</record>

(第三层不受欢迎的兄弟姐妹额外缩进;0004第一层没有出现)

我尝试了针对类似问题提出的不同方法变体(“从平面到分层”、“跟随兄弟姐妹直到”等),但最终要么在某个位置打印了太多兄弟姐妹,要么只输出第一条记录在第三层。

任何帮助是极大的赞赏。

4

1 回答 1

2

一种方法是使用密钥。为了开始获取level元素的兄弟姐妹,您可以定义一个键来按第一个最前面的level元素对元素进行分组(即,该组将是所有兄弟姐妹)。

<xsl:key name="siblings" 
     match="*[not(self::level)]" 
     use="generate-id(preceding-sibling::level[1])" />

您还可以定义一个键来获取level元素的直接“后代”(即,对于每个级别,将它们按第一个最前面的级别和一个短名称分组)。

<xsl:key name="nextlevel" 
     match="level" 
     use="generate-id(preceding-sibling::level[starts-with(current(), concat(., '/'))][1])" />

然后,在您的 XSLT 中,您只需选择“第一”级元素即可

<xsl:apply-templates select="level[. = 'first']" />

然后,您将拥有一个匹配level元素的通用模板,您可以在其中利用键来输出兄弟元素和下一级元素

<xsl:template match="level">
    <Record level="{.}">
        <xsl:copy-of select="." />
        <xsl:apply-templates select="key('siblings', generate-id())" />
        <xsl:apply-templates select="key('nextlevel', generate-id())" />
    </Record>
</xsl:template>

尝试以下 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:key name="siblings" match="*[not(self::level)]" use="generate-id(preceding-sibling::level[1])" />

    <xsl:key name="nextlevel" match="level" use="generate-id(preceding-sibling::level[starts-with(current(), concat(., '/'))][1])" />

    <xsl:template match="record">
        <xsl:apply-templates select="level[. = 'first']" />
    </xsl:template>

    <xsl:template match="level">
        <Record level="{.}">
            <xsl:copy-of select="." />
            <xsl:apply-templates select="key('siblings', generate-id())" />
            <xsl:apply-templates select="key('nextlevel', generate-id())" />
        </Record>
    </xsl:template>

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

当应用于您的 XML 时,将输出以下内容

<Record level="first">
    <level>first</level>
    <unitid>0001</unitid>
    <a-few-more-siblings/>
    <Record level="first/second">
        <level>first/second</level>
        <unitid>0002</unitid>
        <many-more-siblings/>
        <Record level="first/second/third">
            <level>first/second/third</level>
            <unitid>0003a</unitid>
            <some-more-siblings/>
        </Record>
        <Record level="first/second/third">
            <level>first/second/third</level>
            <unitid>0003b</unitid>
            <many-more-siblings/>
        </Record>
        <Record level="first/second/third">
            <level>first/second/third</level>
            <unitid>0003c</unitid>
            <some-more-siblings/>
        </Record>
    </Record>
</Record>
<Record level="first">
    <level>first</level>
    <unitid>0004</unitid>
    <again-more-siblings/>
</Record>

这与您当前显示的预期输出不太一样,因为您的预期输出有两个“第一个”level元素包裹在一个Record元素中(与Record“第三个”level元素的单独元素相比)。如果您的预期输出确实是您所期望的,请尝试替换record与这两个模板匹配的模板:

<xsl:template match="record">
    <Record level="first">
        <xsl:apply-templates select="level[. = 'first']" />
     </Record>
</xsl:template>

<xsl:template match="level[. = 'first']">
    <xsl:copy-of select="." />
    <xsl:apply-templates select="key('siblings', generate-id())" />
    <xsl:apply-templates select="key('nextlevel', generate-id())" />
</xsl:template>
于 2013-06-09T08:56:34.473 回答