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分组_Xslt_Xpath_Xslt 2.0 - Fatal编程技术网

基于元素属性的XSLT分组

基于元素属性的XSLT分组,xslt,xpath,xslt-2.0,Xslt,Xpath,Xslt 2.0,我的XML文件看起来像 <templates> <template type="ORC"> <field/> </template> <template type="OBR"> <field/> </template> <template type="OBX"> <field/> </te

我的XML文件看起来像

<templates>
    <template type="ORC">
        <field/>
    </template>
    <template type="OBR">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="SPM">
        <field/>
    </template>
    <template type="ORC">
        <field/>
    </template>
    <template type="OBR">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="SPM">
        <field/>
    </template>
</templates>

我想将订单详细信息(template/@type='ORC')分组,并使用XSLT2.0将上面的示例XML转换为下面的格式

<templates>
    <order-details>
        <template type="ORC">
            <field/>
        </template>
        <template type="OBR">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="SPM">
            <field/>
        </template>
    </order-details>
    <order-details>
        <template type="ORC">
            <field/>
        </template>
        <template type="OBR">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="SPM">
            <field/>
        </template>
    </order-details>
</templates>

您可以使用以的属性开头的
组来按您想要的方式进行分组

在这里:


当应用于输入文档时,会产生指定的预期输出:

<templates>
   <order-details>
      <template type="ORC">
         <field/>
      </template>
      <template type="OBR">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="SPM">
         <field/>
      </template>
   </order-details>
   <order-details>
      <template type="ORC">
         <field/>
      </template>
      <template type="OBR">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="SPM">
         <field/>
      </template>
   </order-details>
</templates>


分组需要一个公共值,您想在
@type=“ORC”
@Tommy上拆分-在这种情况下,分组只需要以
@type=“ORC”
开头。请参阅Pavel的答案,了解如何在XSLT2.0中实现这一点。在1.0中也很简单;如果您愿意,我可以添加一个带有1.0解决方案的答案。
<templates>
   <order-details>
      <template type="ORC">
         <field/>
      </template>
      <template type="OBR">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="SPM">
         <field/>
      </template>
   </order-details>
   <order-details>
      <template type="ORC">
         <field/>
      </template>
      <template type="OBR">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="SPM">
         <field/>
      </template>
   </order-details>
</templates>