我正在 XSLT 中编写一些产品导出,并且我有一些相当大的选择语句,因为我有这些嵌套的排序/标题
给定一个选择语句,例如...
/objects/object/items/item[
not(
custom_options/custom_option[bracelet_piece='engraving_style']/value = preceding::item[
(
custom_options/custom_option[bracelet_piece='engraving_style']/value = 'Black Engraving'
or custom_options/custom_option[bracelet_piece='engraving_style']/value = 'Laser Engraved'
)
and not(product_type='configurable')
and (
product_attributes/product_type='Dog Tag'
or product_attributes/product_type='Other Engraveable'
)
]/custom_options/custom_option[bracelet_piece='engraving_style']/value
)
and (
custom_options/custom_option[bracelet_piece='engraving_style']/value = 'Black Engraving'
or custom_options/custom_option[bracelet_piece='engraving_style']/value = 'Laser Engraved'
)
and not(product_type='configurable')
and (product_attributes/product_type='Dog Tag' or product_attributes/product_type='Other Engraveable')
是否可以将语句的各个部分分解为将在运行时评估的可重用字符串?
听起来好像我想要属性值模板,但我能找到的那些说它们不能在选择语句中使用
为了从上面提取最简单且可能不太有用的示例,假设我希望能够包含
not(product_type='configurable')
在 select 语句中没有每次都复制文本,有没有办法做到这一点?备注 :我无法在此处存储该部分的结果,因为这是 for-each 指令中的选择。
最佳答案
好吧,当您提到属性值模板时,我猜您是在谈论 select
属性,那么您可能想在 XSLT 3(自今年 6 月以来的 W3C 推荐)中了解它,并在 Saxon 9.8 中提供适用于 Java、.NET 和 C++/C 以及 Altova XMLSpy/Raptor 2017 或 2018 您可以使用所谓的 shadow attributes (即 _select
而不是 select
)与 static variables or parameters
<xsl:variable name="exp1" as="xs:string" static="yes" select=""not(product_type='configurable')""/>
然后你可以使用例如
<xsl:for-each _select="/objects/object/items/item[{$exp1}]">
您还可以使用 XSLT 2 和 3 定义自己的函数,例如
<xsl:function name="mf:exp1" as="xs:boolean">
<xsl:param name="item"/>
<xsl:sequence select="not($item/product_type='configurable')"/>
</xsl:function>
然后使用
<xsl:for-each select="/objects/object/items/item[mf:exp1(.)]">
(这当然需要将前缀
mf
或您想要使用的任何前缀绑定(bind)到函数的命名空间,例如 xmlns:mf="http://example.com/mf"
)。
关于xml - 如何分解 XSLT 中的大型 XPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47205489/