基于元素属性的XSLT分组
我的XML文件看起来像基于元素属性的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
<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>