gpt4 book ai didi

xslt - 相对于每个元素在 XSLT 中选择不同的

转载 作者:行者123 更新时间:2023-12-02 02:16:57 25 4
gpt4 key购买 nike

我尝试检索一个元素的子元素的属性值列表,但我希望这些值只出现一次。

例如,我有以下 XML

<root>
<sec>
<nom-title>
<nom-chapter>
<nom-article><data att="1.1"/></nom-article>
<nom-article>
<nom-item><data att="1.1"/></nom-item>
<nom-item><data att="1.2"/></nom-item>
</nom-article>
</nom-chapter>
<nom-chapter>
<nom-article><data att="2.1"/></nom-article>
<nom-article><data att="1.1"/></nom-article>
</nom-chapter>
</nom-title>
<nom-title>
<nom-chapter>
<nom-article><data att="1.1"/></nom-article>
</nom-chapter>
</nom-title>
</sec>
</root>

我想要这样的结果:

<root>
<nom-title>
<att>1.1</att>
<att>1.2</att>
<att>2.1</att>
<nom-chapter>
<att>1.1</att>
<att>1.2</att>
<nom-article>
<att>1.1</att>
</nom-article>
<nom-article>
<att>1.1</att>
<att>1.2</att>
<nom-item><att>1.1</att></nom-item>
<nom-item><att>1.2</att></nom-item>
</nom-article>
</nom-chapter>
</nom-title>
<nom-title>
<att>1.1</att>
<nom-chapter>
<att>1.1</att>
<nom-article>
<att>1.1</att>
</nom-article>
</nom-chapter>
</nom-title>
</root>

我尝试使用 xsl:key 元素,但它只返回一个元素的值。在这个例子中,它只为第一个标题返回 1.1 而不是第二个。我用过的xsl:

 <xsl:key name="allAtt"
match="//*[starts-with(name(.),'nom-')]/data"
use="@att"/>
<xsl:template match="nom-title|nom-chapter|nom-article|nom-item">
<xsl:element name="name(.)">
<xsl:apply-templates select=".//*[starts-with(name(.),'nom-')]/data
</xsl:element>
</xsl:template>
<xsl:template match="data">
<xsl:variable name="att" select="@att"/>
<xsl:if test="generate-id(.)=generate-id(key('allAtt',$att)[1]">
<xsl:element name="att"><xsl:value-of select="$att"></xsl:element>
</xsl:if>
</xsl:template>

最佳答案

这个转换:

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

<xsl:key name="kData-nom-article" match="data" use=
"concat(generate-id(ancestor::nom-article[1]),
'+', @att)"/>
<xsl:key name="kData-nom-chapter" match="data" use=
"concat(generate-id(ancestor::nom-chapter[1]),
'+', @att)"/>
<xsl:key name="kData-nom-title" match="data" use=
"concat(generate-id(ancestor::nom-title[1]),
'+', @att)"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="sec"><xsl:apply-templates/></xsl:template>

<xsl:template match="nom-title|nom-article|nom-chapter">
<xsl:copy>
<xsl:apply-templates mode="list" select=
".//data[generate-id()
=
generate-id(key(concat('kData-', name(current())),
concat(generate-id(current()),
'+', @att
)
)
[1]
)
]"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="data" mode="list">
<att><xsl:value-of select="@att"/></att>
</xsl:template>

<xsl:template match="non-item/data">
<att><xsl:value-of select="@att"/></att>
</xsl:template>

<xsl:template match="*[not(self::nom-item)]/data"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<root>
<sec>
<nom-title>
<nom-chapter>
<nom-article>
<data att="1.1"/>
</nom-article>
<nom-article>
<nom-item>
<data att="1.1"/>
</nom-item>
<nom-item>
<data att="1.2"/>
</nom-item>
</nom-article>
</nom-chapter>
<nom-chapter>
<nom-article>
<data att="2.1"/>
</nom-article>
<nom-article>
<data att="1.1"/>
</nom-article>
</nom-chapter>
</nom-title>
<nom-title>
<nom-chapter>
<nom-article>
<data att="1.1"/>
</nom-article>
</nom-chapter>
</nom-title>
</sec>
</root>

产生想要的、正确的结果:

<root>
<nom-title>
<att>1.1</att>
<att>1.2</att>
<att>2.1</att>
<nom-chapter>
<att>1.1</att>
<att>1.2</att>
<nom-article>
<att>1.1</att>
</nom-article>
<nom-article>
<att>1.1</att>
<att>1.2</att>
<nom-item>
<data att="1.1"/>
</nom-item>
<nom-item>
<data att="1.2"/>
</nom-item>
</nom-article>
</nom-chapter>
<nom-chapter>
<att>2.1</att>
<att>1.1</att>
<nom-article>
<att>2.1</att>
</nom-article>
<nom-article>
<att>1.1</att>
</nom-article>
</nom-chapter>
</nom-title>
<nom-title>
<att>1.1</att>
<nom-chapter>
<att>1.1</att>
<nom-article>
<att>1.1</att>
</nom-article>
</nom-chapter>
</nom-title>
</root>

说明:通过动态构造要执行的实际分组的键名称,将三个不同的 Muenchian 分组表示为一个。

记住:键名是一个字符串,必要时(如本例),可以动态构造该名称,或将其作为参数传递。 p>

关于xslt - 相对于每个元素在 XSLT 中选择不同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10009452/

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