Xml 如何在XSLT中分解大型XPath?
我正在用XSLT编写一些产品导出,我有一些相当大的select语句,因为我有这些嵌套的排序/头 给定select语句,例如Xml 如何在XSLT中分解大型XPath?,xml,xslt,xpath,Xml,Xslt,Xpath,我正在用XSLT编写一些产品导出,我有一些相当大的select语句,因为我有这些嵌套的排序/头 给定select语句,例如 /objects/object/items/item[ not( custom_options/custom_option[bracelet_piece='engraving_style']/value = preceding::item[ ( custom_options/custom_opt
/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')
是否可以将语句的各个部分分解为可重用的字符串,并在运行时进行计算
听起来好像我想要属性值模板,但我能找到的是,它们不能在select语句中使用
为了从上面的例子中引出一个最简单、可能不太有用的例子,假设我希望能够在select语句中包含not(product_type='configurable')
,而不必每次复制文本,有没有办法做到这一点
注意:我不能在这里存储该部分的结果,因为这是一个针对每个指令的选择。好吧,当您提到属性值模板时,我猜您正在谈论
选择属性,那么您可能想在XSLT 3中了解这一点(今年6月以来的W3C建议)并且可用于Java、.NET和C++/C以及XMLSpy/Raptor 2017或2018,您可以使用
然后你可以使用例如
<xsl:for-each _select="/objects/object/items/item[{$exp1}]">
您还可以使用XSLT2和XSLT3定义自己的函数,例如
<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
或任何要使用的前缀绑定到函数的命名空间,例如xmlns:mf=”http://example.com/mf“
)。噢!当然是功能。事后看来,这似乎很明显。谢谢更新:我一直使用XSLV1,所以函数对我不起作用。我认为这可能是一个无法克服的限制一些XSLT 1处理器支持XSLT 1的一些扩展来定义函数,比如使用一些脚本语言的or扩展函数,但一般来说,给定XSLT 1和XPath 1,您可以做的事情不多。那么,您的目标是哪个XSLT1处理器呢?我使用PHP中内置的东西。文档说它使用libxslt和链接
<xsl:for-each select="/objects/object/items/item[mf:exp1(.)]">