原标题:XSLT“包含”方法未按预期工作
我有这个xml:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<tag-one>
<tag-two>
<tag-three>
<tag-four>
<tag-five>
<tag-six>
<tag-seven>
<tag-eight>
<tag-nine>
<id-tag>valid_id</id-tag>
</tag-nine>
</tag-eight>
</tag-seven>
</tag-six>
</tag-five>
</tag-four>
</tag-three>
</tag-two>
<tag-two>
<tag-three>
<tag-four>
<tag-five>
<tag-six>
<tag-seven>
<tag-eight>
<tag-nine>
<id-tag>invalid_id-invalid_kg</id-tag>
</tag-nine>
</tag-eight>
</tag-seven>
</tag-six>
</tag-five>
</tag-four>
</tag-three>
</tag-two>
</tag-one>
</Document>
我正在尝试构建一个 XSLT 文件,如果相应的
tag-two
包含在列表中,则过滤掉 id-tag
元素
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="invalidIds"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="tag-two[contains($invalidIds, tag-three/tag-four/tag-five//*[local-name()='id-tag'])]">
</xsl:template>
</xsl:transform>
我使用 saxon 库将
invalidIds
作为 java 代码中的数组列表传递:
List<String> idList = Arrays.asList("invalid_id-invalid_kg");
// omitted rest of the code for brevity
// Map.of(new QName("invalidIds"), new XdmAtomicValue(idList.toString()))
当我在 xslt 中打印出
invalidIds
值时,我得到了:[invalid_id-invalid_kg]
。
问题是它过滤了两个
tag-two
,因为它看起来像contains
方法充当“字符串”包含,而不是“数组”包含,这意味着“valid_id”匹配条件...你有吗知道我该如何解决这个问题吗?
在您的 Java 列表上使用 https://www.saxonica.com/html/documentation12/javadoc/net/sf/saxon/s9api/XdmValue.html#makeSequence(java.lang.Iterable)
Map.of(new QName("invalidIds"), XdmValue.makeSequence(idList))
。
模板应该是
<xsl:template match="tag-two[$invalidIds = tag-three/tag-four/tag-five//id-tag]"/>