gpt4 book ai didi

xml - XSLT - 当值存在于列表或数组中时匹配

转载 作者:行者123 更新时间:2023-12-03 07:56:58 25 4
gpt4 key购买 nike

我有几本巨著,我想根据 ID 值列表提取特定项目。值在数组中吗?如果是,则复制,如果不是则忽略。

看起来很基本,应该在某个地方有一个答案,但每篇文章似乎都更加复杂。我尝试了各种不同的方法,但似乎无法让它工作:参数、变量、使用 exsl:node-set (除了对类似但更复杂的问题的答案之外,我不熟悉它)。

输入 XML

<?xml version="1.0" encoding="UTF-8"?>
<sec xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Section Title</title>
<fig id="img01">
<caption><title>Image 1</title></caption>
<graphic xlink:href="img01.eps"/>
</fig>

<fig id="img02">
<caption><title>Image 2</title></caption>
<graphic xlink:href="img02.eps"/>
</fig>
</sec>

输入图像列表img02、img12、img23、img34 等...

所需输出 XML

<?xml version="1.0" encoding="UTF-8"?>
<sec xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Section Title</title>
<fig id="img02">
<caption><title>Image 2</title></caption>
<graphic xlink:href="img02.eps"/>
</fig>
</sec>

XSLT 示例

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="exsl" version="2">

<xsl:param name="ListOfImages" as="element()">
<item>img02</item>
<item>hundreds-of-image-IDs</item>
</xsl:param>

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

</xsl:template>

<xsl:template match="fig[exsl:node-set($ListOfImages)/item = exsl:node-set(@id)]">
<p>Image Name: <xsl:value-of select="@id"/></p>
<xsl:copy-of select="."/>
</xsl:template>

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

</xsl:stylesheet>

最佳答案

我会使用 ids 列表作为参数,例如

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">

<xsl:param name="id-list" as="xs:string*" select="'img02', 'img12', 'img23'"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="fig[not(@id = $id-list)]"/>

</xsl:stylesheet>

Saxon 9.8 支持 XSLT 3。

关于xml - XSLT - 当值存在于列表或数组中时匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75835422/

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