gpt4 book ai didi

xml - 基于不同 XSLT 2.0 的枚举值

转载 作者:数据小太阳 更新时间:2023-10-29 02:09:47 26 4
gpt4 key购买 nike

我有一长串带有命名标识符的 XML 值。我需要为组合在一起并唯一命名的每个不同标识符制作单独的输出文件。

例如,假设我有:

<List>
<Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
Hello World!
</Item>
<Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
Goodbye World!
</Item>
<Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
This example text should be in the first file
</Item>
<Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
This example text should be in the second file
</Item>
<Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
Hello World!
</Item>
</List>

我如何编写一个转换 (XSLT 2.0) 来将这些分组输出到生成的文件名中并具有唯一值?例如:将第一个 @group 映射到 file1.xml,将第二个 @group 映射到 file2.xml

最佳答案

这是一个使用 XSLT 2.0 中的一些优秀新功能的解决方案:

这个转换:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- -->
<xsl:template match="/*">
<xsl:variable name="vTop" select="."/>
<!-- -->
<xsl:for-each-group select="Item" group-by="@group">
<xsl:result-document href="file:///C:/Temp/file{position()}.xml">
<xsl:element name="{name($vTop)}">
<xsl:copy-of select="current-group()"/>
</xsl:element>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>

应用于 OP 提供的 Xml 文档时(更正为格式正确!):

<List>
<Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
Hello World!
</Item>
<Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
Goodbye World!
</Item>
<Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
This example text should be in the first file
</Item>
<Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
This example text should be in the second file
</Item>
<Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
Hello World!
</Item>
</List>

生成所需的两个文件:file1.xml 和 file2.xml

关于xml - 基于不同 XSLT 2.0 的枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/639690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com