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
使用XSLT1.0组合XML_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

使用XSLT1.0组合XML

使用XSLT1.0组合XML,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我需要绘制以下地图,但很难,因为它有不同的名称: 123 基础知识 4556 AAA 123 结果应该是: 123 基础知识 4556 AAA 123 将索引为n>1的所有order节点复制到第一个节点中的一种方法是以下XSLT: <?xml version = "1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

我需要绘制以下地图,但很难,因为它有不同的名称:


123
基础知识
4556
AAA
123
结果应该是:


123
基础知识
4556
AAA
123

将索引为
n>1的所有
order
节点复制到第一个节点中的一种方法是以下XSLT:

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

    <xsl:template match="text()" />    <!-- suppresses copying of text() nodes -->

    <xsl:template match="main">        <!-- replicates the "main" node -->
      <main>
        <xsl:apply-templates />
      </main>
    </xsl:template>

    <xsl:template match="order[1]">    <!-- matches the first "order" node and copies all following ones -->
      <order>
        <xsl:copy-of select="*" />
        <xsl:copy-of select="following-sibling::order" />
      </order>   
    </xsl:template>    
</xsl:stylesheet>

输出如下所示:

<?xml version="1.0"?>
<main>
    <order>
        <ID>123</ID>
        <Name>ABC</Name>
        <order>
            <ID>4556</ID>
            <Name>AAA</Name>
            <ParentID>123</ParentID>
        </order>
    </order>
</main>

123
基础知识
4556
AAA
123

就像@michael.hor257k所说的,从概念上来说,这个问题和你之前的问题是一样的。我想你只是不理解这个概念。希望我的示例中的注释会有所帮助

如果我正确理解了这个问题,您希望基于
ParentID
order
元素嵌套在其“父级”
order
元素中。因为我们是基于ParentID的,所以我们将使用它作为密钥

XML输入

<main>
    <order>
        <ID>123</ID>
        <Name>ABC</Name>
    </order>

    <order>
        <ID>4556</ID>
        <Name>AAA</Name>
        <ParentID>123</ParentID>
    </order>
</main>
<main>
   <order>
      <ID>123</ID>
      <Name>ABC</Name>
      <order>
         <ID>4556</ID>
         <Name>AAA</Name>
      </order>
   </order>
</main>

123
基础知识
4556
AAA
123
XSLT1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!--Create a key containing order elements that contain a non-empty ParentID.-->
  <xsl:key name="orderByParentId" match="order[string(ParentID)]" use="ParentID"/>

  <!--Identity transform (https://www.w3.org/TR/xslt#copying)-->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/main">
    <xsl:copy>
      <!--To get the high level order elements, only apply-templates when order
      does not contain a non-empty ParentID child.-->
      <xsl:apply-templates 
        select="@*|order[not(string(ParentID))]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="order">
    <xsl:copy>
      <!--Don't apply-templates to ParentID; we don't want to keep those.
      Do apply-templates to the key 'orderByParentId' when the key matches
      the current order's ID child.-->
      <xsl:apply-templates 
        select="@*|*[not(self::ParentID)]|key('orderByParentId',ID)"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

输出

<main>
    <order>
        <ID>123</ID>
        <Name>ABC</Name>
    </order>

    <order>
        <ID>4556</ID>
        <Name>AAA</Name>
        <ParentID>123</ParentID>
    </order>
</main>
<main>
   <order>
      <ID>123</ID>
      <Name>ABC</Name>
      <order>
         <ID>4556</ID>
         <Name>AAA</Name>
      </order>
   </order>
</main>

123
基础知识
4556
AAA

我看不出这与您的另一个问题有什么不同:在这种情况下,我们将使用不同“使用”的ID和父ID比较订单在key@michael.hor257kI中,我相信这是一样的。如果有很多订单,我只想根据它们的ID对它们进行分组,其中具有相同父ID的订单与另一个订单的ID匹配,则必须合并。不是所有的订单都有@zx485@Tarun:对不起,我一点儿也不明白你的补充问题。上面的michael.hor257k告诉您,这个问题可能与您之前的一个问题非常相似。我还是回答了。但你的评论似乎表明了一套全新的参数,你最好把这些参数放在一个新问题中,并给出一个精确的预期结果。