Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt XSL分组与1.0_Xslt_Xpath_Key_Grouping - Fatal编程技术网

Xslt XSL分组与1.0

Xslt XSL分组与1.0,xslt,xpath,key,grouping,Xslt,Xpath,Key,Grouping,我有一个如下所示的XML <Doc> <row> <Col1>13820PKS-</Col1> <Col6>01</Col6> <Col9>1507462800</Col9> <Col12>15074628</Col12> <Col14>4</Col14> </row> <row> <C

我有一个如下所示的XML

<Doc>
<row>
  <Col1>13820PKS-</Col1> 
  <Col6>01</Col6> 
  <Col9>1507462800</Col9> 
  <Col12>15074628</Col12> 
  <Col14>4</Col14> 
  </row>
<row>
  <Col1>13820PKS-</Col1> 
  <Col6>01</Col6> 
  <Col9>1507462800</Col9> 
  <Col12>15074629</Col12> 
  <Col14>5</Col14> 
  </row>
 <row>
  <Col1>13820PKS-</Col1> 
  <Col6>01</Col6> 
  <Col9>1808502801</Col9> 
  <Col12>18085021</Col12> 
  <Col14>1</Col14> 
  </row>
 <row>
  <Col1>13820PKS-</Col1> 
  <Col6>02</Col6> 
  <Col9>2710004100</Col9> 
  <Col12>2710004100</Col12> 
  <Col14>1</Col14> 
  </row>
</Doc>

13820PKS-
01
1507462800
15074628
4.
13820PKS-
01
1507462800
15074629
5.
13820PKS-
01
1808502801
18085021
1.
13820PKS-
02
2710004100
2710004100
1.
文档实际上有2000多行。 最终结果应该是

<Doc>
    <ListID id="01">
        <MainArt>
            <ItemCode>13820PKS-</ItemCode>
            <List>
                <SubArt>
                    <ItemCode>1507462800</ItemCode>
                    <SubArtList>
                        <row>
                            <ItemCode>15074628</ItemCode>
                            <Quantity>4</Quantity>
                        </row>
                        <row>
                            <ItemCode>15074629</ItemCode>
                            <Quantity>5</Quantity>
                        </row>
                    </SubArtList>
                </SubArt>
                <SubArt>
                    <ItemCode>1808502801</ItemCode>
                    <SubArtList>
                        <row>
                            <ItemCode>18085021</ItemCode>
                            <Quantity>1</Quantity>
                        </row>
                    </SubArtList>
                </SubArt>
            </List>
        </MainArt>
    </ListID>
    <ListID id="02">
        <MainArt>
            <ItemCode>13820PKS-</ItemCode>
            <List>
                <SubArt>
                    <ItemCode>2710004100</ItemCode>
                    <SubArtList>
                        <row>
                            <ItemCode>2710004100</ItemCode>
                            <Quantity>1</Quantity>
                        </row>
                    </SubArtList>
                </SubArt>
            </List>
        </MainArt>
    </ListID>
</Doc>

13820磅-
1507462800
15074628
4.
15074629
5.
1808502801
18085021
1.
13820磅-
2710004100
2710004100
1.
我仍处于学习阶段,没有使用模板的技能。我尝试使用递归for each循环来实现它,但也不起作用。
任何帮助都将不胜感激。谢谢

您拥有三个级别的多重分组并不是一件容易的事!但这就是你要做的

首先,您在最顶层按Col6元素分组,因此您定义了一个键,以按Col6值查找元素

<xsl:key name="Col6" match="row" use="Col6"/>
最后,对于Col9元素,您将有一个三重连接键

<xsl:key name="Col9" match="row" use="concat(Col6, '|', Col1, '|', Col9)"/>

这可以以类似的方式用于获取当前Col1中的所有Col9元素

总而言之,给出了以下XSLT

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

  <xsl:key name="Col6" match="row" use="Col6"/>
  <xsl:key name="Col1" match="row" use="concat(Col6, '|', Col1)"/>
  <xsl:key name="Col9" match="row" use="concat(Col6, '|', Col1, '|', Col9)"/>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="row[generate-id() = generate-id(key('Col6', Col6)[1])]" mode="Col6" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="row" mode="Col6">
    <ListID id="{Col6}">
      <xsl:apply-templates select="key('Col6', Col6)[generate-id() = generate-id(key('Col1', concat(Col6, '|', Col1))[1])]" mode="Col1" />
    </ListID>
  </xsl:template>

  <xsl:template match="row" mode="Col1">
    <MainArt>
      <ItemCode>
        <xsl:value-of select="Col1"/>
      </ItemCode>
      <List>
        <xsl:apply-templates select="key('Col1', concat(Col6, '|', Col1))[generate-id() = generate-id(key('Col9', concat(Col6, '|', Col1, '|', Col9))[1])]" mode="Col9" />
      </List>
    </MainArt>
  </xsl:template>

  <xsl:template match="row" mode="Col9">
    <SubArt>
      <ItemCode>
        <xsl:value-of select="Col9"/>
      </ItemCode>
      <SubArtList>
        <xsl:apply-templates select="key('Col9', concat(Col6, '|', Col1, '|', Col9))" mode="Col12" />
      </SubArtList>
    </SubArt>
  </xsl:template>

  <xsl:template match="row" mode="Col12">
    <row>
      <ItemCode>
        <xsl:value-of select="Col12"/>
      </ItemCode>
      <Quantity>
        <xsl:value-of select="Col14"/>
      </Quantity>
    </row>
  </xsl:template>
</xsl:stylesheet>

当应用于示例XML时,将输出以下内容

<Doc>
  <ListID id="01">
    <MainArt>
      <ItemCode>13820PKS-</ItemCode>
      <List>
        <SubArt>
          <ItemCode>1507462800</ItemCode>
          <SubArtList>
            <row>
              <ItemCode>15074628</ItemCode>
              <Quantity>4</Quantity>
            </row>
            <row>
              <ItemCode>15074629</ItemCode>
              <Quantity>5</Quantity>
            </row>
          </SubArtList>
        </SubArt>
        <SubArt>
          <ItemCode>1808502801</ItemCode>
          <SubArtList>
            <row>
              <ItemCode>18085021</ItemCode>
              <Quantity>1</Quantity>
            </row>
          </SubArtList>
        </SubArt>
      </List>
    </MainArt>
  </ListID>
  <ListID id="02">
    <MainArt>
      <ItemCode>13820PKS-</ItemCode>
      <List>
        <SubArt>
          <ItemCode>2710004100</ItemCode>
          <SubArtList>
            <row>
              <ItemCode>2710004100</ItemCode>
              <Quantity>1</Quantity>
            </row>
          </SubArtList>
        </SubArt>
      </List>
    </MainArt>
  </ListID>
</Doc>

13820磅-
1507462800
15074628
4.
15074629
5.
1808502801
18085021
1.
13820磅-
2710004100
2710004100
1.

您是否试图按Col6对行进行分组?在XSLT 1.0中,您需要使用一种称为Muenchian Grouping()的技术,这可能会在您第一次看到它时伤到您的大脑,但这是最好的方法!您是否可以修改您的问题,以显示给定输入的实际预期输出,因为您可能需要在此处进行多次分组。谢谢谢谢你的回复。我知道Muenchian分组方法(我读过know means),但正如你所说,它让我的大脑受伤:)。我仍在学习如何实现模板,我认为如果不理解模板实现,我将无法理解“Muenchian分组”。我必须做多重分组。Col12和Col14属于Col9,属于Col1,属于Col6。我知道这有点复杂。第二部分是预期产出。第二部分显示了预期产出的结构,但我相信这不是你字面上预期的实际产出?因此,如果您可以显示当前输入的实际输出,这将非常有用。谢谢非常感谢。这是我想要的,谢谢你的详细解释。我真的很感激。
<xsl:key name="Col9" match="row" use="concat(Col6, '|', Col1, '|', Col9)"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="Col6" match="row" use="Col6"/>
  <xsl:key name="Col1" match="row" use="concat(Col6, '|', Col1)"/>
  <xsl:key name="Col9" match="row" use="concat(Col6, '|', Col1, '|', Col9)"/>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="row[generate-id() = generate-id(key('Col6', Col6)[1])]" mode="Col6" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="row" mode="Col6">
    <ListID id="{Col6}">
      <xsl:apply-templates select="key('Col6', Col6)[generate-id() = generate-id(key('Col1', concat(Col6, '|', Col1))[1])]" mode="Col1" />
    </ListID>
  </xsl:template>

  <xsl:template match="row" mode="Col1">
    <MainArt>
      <ItemCode>
        <xsl:value-of select="Col1"/>
      </ItemCode>
      <List>
        <xsl:apply-templates select="key('Col1', concat(Col6, '|', Col1))[generate-id() = generate-id(key('Col9', concat(Col6, '|', Col1, '|', Col9))[1])]" mode="Col9" />
      </List>
    </MainArt>
  </xsl:template>

  <xsl:template match="row" mode="Col9">
    <SubArt>
      <ItemCode>
        <xsl:value-of select="Col9"/>
      </ItemCode>
      <SubArtList>
        <xsl:apply-templates select="key('Col9', concat(Col6, '|', Col1, '|', Col9))" mode="Col12" />
      </SubArtList>
    </SubArt>
  </xsl:template>

  <xsl:template match="row" mode="Col12">
    <row>
      <ItemCode>
        <xsl:value-of select="Col12"/>
      </ItemCode>
      <Quantity>
        <xsl:value-of select="Col14"/>
      </Quantity>
    </row>
  </xsl:template>
</xsl:stylesheet>
<Doc>
  <ListID id="01">
    <MainArt>
      <ItemCode>13820PKS-</ItemCode>
      <List>
        <SubArt>
          <ItemCode>1507462800</ItemCode>
          <SubArtList>
            <row>
              <ItemCode>15074628</ItemCode>
              <Quantity>4</Quantity>
            </row>
            <row>
              <ItemCode>15074629</ItemCode>
              <Quantity>5</Quantity>
            </row>
          </SubArtList>
        </SubArt>
        <SubArt>
          <ItemCode>1808502801</ItemCode>
          <SubArtList>
            <row>
              <ItemCode>18085021</ItemCode>
              <Quantity>1</Quantity>
            </row>
          </SubArtList>
        </SubArt>
      </List>
    </MainArt>
  </ListID>
  <ListID id="02">
    <MainArt>
      <ItemCode>13820PKS-</ItemCode>
      <List>
        <SubArt>
          <ItemCode>2710004100</ItemCode>
          <SubArtList>
            <row>
              <ItemCode>2710004100</ItemCode>
              <Quantity>1</Quantity>
            </row>
          </SubArtList>
        </SubArt>
      </List>
    </MainArt>
  </ListID>
</Doc>