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
在XSLT中插入一组节点的父节点_Xslt_Xslt Grouping - Fatal编程技术网

在XSLT中插入一组节点的父节点

在XSLT中插入一组节点的父节点,xslt,xslt-grouping,Xslt,Xslt Grouping,我需要使用XSLT转换XML输入: <CONTAINER>container1</CONTAINER> <STOP>stop1</STOP> <PO>po1</PO> <PO>po2</PO> <PO>po3</PO> <CONTAINER>container2</CONTAINER> <STOP>stop2</STOP> <

我需要使用XSLT转换XML输入:

<CONTAINER>container1</CONTAINER>
<STOP>stop1</STOP>
<PO>po1</PO>
<PO>po2</PO>
<PO>po3</PO>
<CONTAINER>container2</CONTAINER>
<STOP>stop2</STOP>
<PO>po4</PO>
<PO>po5</PO>
<PO>po6</PO>
<STOP>stop3</STOP>
<PO>po7</PO>
<PO>po8</PO>
container1
停止1
po1
po2
磷酸
集装箱2
停止2
po4
po5
po6
停止3
po7
po8
进入

container1
塌陷停止
po1
po2
磷酸
集装箱2
塌陷停止
po4
po5
po6
停止3
po7
po8
所以基本上我需要在一个站点中折叠所有POs标签,而不是有很多站点,每个站点都有一组POs ad子站点

有人能帮我吗?我对XSLT非常陌生,因此无法找到执行(如果可能)此转换的方法。

  • 您的输入应该是格式良好的文档。我把它包在一个元素中,使它成为这样
  • 您正在使用XSLT2。您没有说明版本
  • 文档中的每个容器都会立即停止
  • 根元素的第一个子元素是CONTAINER
  • 此XSLT 2转换

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="2.0">
    
    <xsl:output indent="yes" encoding="utf-8" omit-xml-declaration="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates />
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="t">
      <xsl:copy>
        <xsl:for-each-group select="*" group-starting-with="CONTAINER">
          <CONTAINER><xsl:value-of select="current-group()[self::CONTAINER]/text()" /></CONTAINER>
          <STOP>
            <new_tag>Collapsed STOP</new_tag>
            <xsl:apply-templates select="current-group()
              [not(self::CONTAINER)]      (: Excluded because we have already dealt with CONTAINER.  :)
              [position() ge 2     ]      (: Exclude the first STOP, because already dealt with.     :)
              " />
          </STOP>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    
    
    塌陷停止
    
    。。。将转换此输入文档

    <t>
        <CONTAINER>container1</CONTAINER>
        <STOP>stop1</STOP>
        <PO>po1</PO>
        <PO>po2</PO>
        <PO>po3</PO>
        <CONTAINER>container2</CONTAINER>
        <STOP>stop2</STOP>
        <PO>po4</PO>
        <PO>po5</PO>
        <PO>po6</PO>
        <STOP>stop3</STOP>
        <PO>po7</PO>
        <PO>po8</PO>
    </t>
    
    
    集装箱1
    停止1
    po1
    po2
    磷酸
    集装箱2
    停止2
    po4
    po5
    po6
    停止3
    po7
    po8
    
    。。。在这个输出中

    <t>
       <CONTAINER>container1</CONTAINER>
       <STOP>
          <new_tag>Collapsed STOP</new_tag>
          <PO>po1</PO>
          <PO>po2</PO>
          <PO>po3</PO>
       </STOP>
       <CONTAINER>container2</CONTAINER>
       <STOP>
          <new_tag>Collapsed STOP</new_tag>
          <PO>po4</PO>
          <PO>po5</PO>
          <PO>po6</PO>
          <STOP>stop3</STOP>
          <PO>po7</PO>
          <PO>po8</PO>
       </STOP>
    </t>
    
    
    集装箱1
    塌陷停止
    po1
    po2
    磷酸
    集装箱2
    塌陷停止
    po4
    po5
    po6
    停止3
    po7
    po8
    
    学习笔记 请参阅,以了解有关xsl:for each group指令的更多信息。

    XSLT 1.0解决方案:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    
        <xsl:template match="/root">
            <root>
                <xsl:apply-templates />
            </root>
    
        </xsl:template>
    
        <xsl:template match="/root/CONTAINER">
            <xsl:copy-of select="."/>
            <xsl:apply-templates />
        </xsl:template>
    
        <xsl:template match="/root/STOP">
            <STOP>
                <new_tag><xsl:value-of select="." /></new_tag>
                <xsl:variable name="stopnum">
                    <xsl:number level="single" count="STOP" />
                </xsl:variable>
                <xsl:for-each select="following-sibling::PO[count(preceding-sibling::STOP) = $stopnum]">
                    <xsl:copy-of select="." />                
                </xsl:for-each>
            </STOP>
        </xsl:template>
    
        <xsl:template match="text()" />
    
    </xsl:transform>
    
    输出:

    <root>
       <CONTAINER>container1</CONTAINER>
       <STOP>
          <new_tag>stop1</new_tag>
          <PO>po1</PO>
          <PO>po2</PO>
          <PO>po3</PO>
       </STOP>
       <CONTAINER>container2</CONTAINER>
       <STOP>
          <new_tag>stop2</new_tag>
          <PO>po4</PO>
          <PO>po5</PO>
          <PO>po6</PO>
       </STOP>
       <STOP>
          <new_tag>stop3</new_tag>
          <PO>po7</PO>
          <PO>po8</PO>
       </STOP>
    </root>
    
    
    集装箱1
    停止1
    po1
    po2
    磷酸
    集装箱2
    停止2
    po4
    po5
    po6
    停止3
    po7
    po8
    
    您已经试过了吗?你能发布你的样式表吗?XSLT的哪个版本?第三个
    站如何?您的输出没有将其作为
    PO
    s的父级。有什么原因吗?
                <xsl:variable name="stopnum">
                    <xsl:number level="single" count="STOP" />
                </xsl:variable>
                <xsl:for-each select="following-sibling::PO[count(preceding-sibling::STOP) = $stopnum]">
    
    <root>
    <CONTAINER>container1</CONTAINER>
    <STOP>stop1</STOP>
    <PO>po1</PO>
    <PO>po2</PO>
    <PO>po3</PO>
    <CONTAINER>container2</CONTAINER>
    <STOP>stop2</STOP>
    <PO>po4</PO>
    <PO>po5</PO>
    <PO>po6</PO>
    <STOP>stop3</STOP>
    <PO>po7</PO>
    <PO>po8</PO>
    </root>
    
    <root>
       <CONTAINER>container1</CONTAINER>
       <STOP>
          <new_tag>stop1</new_tag>
          <PO>po1</PO>
          <PO>po2</PO>
          <PO>po3</PO>
       </STOP>
       <CONTAINER>container2</CONTAINER>
       <STOP>
          <new_tag>stop2</new_tag>
          <PO>po4</PO>
          <PO>po5</PO>
          <PO>po6</PO>
       </STOP>
       <STOP>
          <new_tag>stop3</new_tag>
          <PO>po7</PO>
          <PO>po8</PO>
       </STOP>
    </root>