gpt4 book ai didi

xslt - 在 XSL 中如何防止重复?

转载 作者:行者123 更新时间:2023-12-01 22:20:40 25 4
gpt4 key购买 nike

如何防止重复条目进入列表,然后理想情况下对该列表进行排序?我正在做的是,当一个级别的信息丢失时,从低于它的级别获取信息,在上面的级别中构建丢失的列表。目前,我有与此类似的 XML:

<c03 id="ref6488" level="file">
<did>
<unittitle>Clinic Building</unittitle>
<unitdate era="ce" calendar="gregorian">1947</unitdate>
</did>
<c04 id="ref34582" level="file">
<did>
<container label="Box" type="Box">156</container>
<container label="Folder" type="Folder">3</container>
</did>
</c04>
<c04 id="ref6540" level="file">
<did>
<container label="Box" type="Box">156</container>
<unittitle>Contact prints</unittitle>
</did>
</c04>
<c04 id="ref6606" level="file">
<did>
<container label="Box" type="Box">154</container>
<unittitle>Negatives</unittitle>
</did>
</c04>
</c03>

然后我应用以下 XSL:

<xsl:template match="c03/did">
<xsl:choose>
<xsl:when test="not(container)">
<did>
<!-- If no c03 container item is found, look in the c04 level for one -->
<xsl:if test="../c04/did/container">

<!-- If a c04 container item is found, use the info to build a c03 version -->
<!-- Skip c03 container item, if still no c04 items found -->
<container label="Box" type="Box">

<!-- Build container list -->
<!-- Test for more than one item, and if so, list them, -->
<!-- separated by commas and a space -->
<xsl:for-each select="../c04/did">
<xsl:if test="position() &gt; 1">, </xsl:if>
<xsl:value-of select="container"/>
</xsl:for-each>
</container>
</did>
</xsl:when>

<!-- If there is a c03 container item(s), list it normally -->
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

但是我得到了“容器”结果

<container label="Box" type="Box">156, 156, 154</container>

当我想要的是

<container label="Box" type="Box">154, 156</container>

以下是我想要获得的完整结果:

<c03 id="ref6488" level="file">
<did>
<container label="Box" type="Box">154, 156</container>
<unittitle>Clinic Building</unittitle>
<unitdate era="ce" calendar="gregorian">1947</unitdate>
</did>
<c04 id="ref34582" level="file">
<did>
<container label="Box" type="Box">156</container>
<container label="Folder" type="Folder">3</container>
</did>
</c04>
<c04 id="ref6540" level="file">
<did>
<container label="Box" type="Box">156</container>
<unittitle>Contact prints</unittitle>
</did>
</c04>
<c04 id="ref6606" level="file">
<did>
<container label="Box" type="Box">154</container>
<unittitle>Negatives</unittitle>
</did>
</c04>
</c03>

预先感谢您的帮助!

最佳答案

这个问题不需要 XSLT 2.0 解决方案

这是一个 XSLT 1.0 解决方案,它比当前选择的 XSLT 2.0 解决方案更紧凑(35 行与 43 行):

<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="kBoxContainerByVal"
match="container[@type='Box']" use="."/>

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

<xsl:template match="c03/did[not(container)]">
<xsl:copy>

<xsl:variable name="vContDistinctValues" select=
"/*/*/*/container[@type='Box']
[generate-id()
=
generate-id(key('kBoxContainerByVal', .)[1])
]
"/>

<container label="Box" type="Box">
<xsl:for-each select="$vContDistinctValues">
<xsl:sort data-type="number"/>

<xsl:value-of select=
"concat(., substring(', ', 1 + 2*(position() = last())))"/>
</xsl:for-each>
</container>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

当此转换应用于最初提供的 XML 文档时,将生成正确的所需结果:

<c03 id="ref6488" level="file">
<did>
<container label="Box" type="Box">156, 154</container>
<unittitle>Clinic Building</unittitle>
<unitdate era="ce" calendar="gregorian">1947</unitdate>
</did>
<c04 id="ref34582" level="file">
<did>
<container label="Box" type="Box">156</container>
<container label="Folder" type="Folder">3</container>
</did>
</c04>
<c04 id="ref6540" level="file">
<did>
<container label="Box" type="Box">156</container>
<unittitle>Contact prints</unittitle>
</did>
</c04>
<c04 id="ref6606" level="file">
<did>
<container label="Box" type="Box">154</container>
<unittitle>Negatives</unittitle>
</did>
</c04>
</c03>

更新:

我没有注意到容器编号必须按顺序排列的要求。现在的解决方案反射(reflect)了这一点。

关于xslt - 在 XSL 中如何防止重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2669813/

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