XSLT - 检查节点是否存在或不使用函数
XSLT - check node is exist or not using function
我有 xml 如下,
<doc>
<meta-data>
<paper-name>ABC</paper-name>
<paper-type>fi</paper-type>
</meta-data>
<section>
<figure>fig1</figure>
<figure>fig2</figure>
</section>
</doc>
我的要求是,如果 <paper-type>
节点在 <meta-data>
中可用,将 <figure>
节点更改为 <image>
节点。
所以,输出应该是这样的,
<doc>
<meta-data>
<paper-name>ABC</paper-name>
<paper-type>fi</paper-type>
</meta-data>
<section>
<image>fig1</image>
<image>fig2</image>
</section>
</doc>
我已经编写了以下 xsl 来完成此任务,
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:function name="abc:check-paper-type" as="xs:boolean">
<xsl:sequence select="root()//meta-data/paper-type"/>
</xsl:function>
<xsl:template match="figure[abc:check-paper-type()]">
<image>
<xsl:apply-templates/>
</image>
</xsl:template>
检查 <paper-type>
节点在 <meta-data>
中是否可用 我在这里写了一个名为 'check-paper-type' 的函数。但是没有按预期工作。
有什么建议可以组织我的功能来检查,<paper-type>
是否可用?
请注意,我需要通过检查 <paper-type>
节点是否存在来更改大量节点。因此,使用函数检查 <paper-type>
是否存在很重要。
您的尝试失败的原因是:
Within the body of a stylesheet function, the focus is initially
undefined; this means that any attempt to reference the context item,
context position, or context size is a non-recoverable dynamic error.
[XPDY0002]
http://www.w3.org/TR/xslt20/#stylesheet-functions
你可以简单地做:
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="figure[../../meta-data/paper-type]">
<image>
<xsl:apply-templates select="@*|node()"/>
</image>
</xsl:template>
根据您的输入,这将产生:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<meta-data>
<paper-name>ABC</paper-name>
<paper-type>fi</paper-type>
</meta-data>
<section>
<image>fig1</image>
<image>fig2</image>
</section>
</doc>
或者,如果需要重复引用节点的存在,可以将其定义为变量,而不是函数[=28] =]:
<xsl:variable name="check-paper-type" select="exists(/doc/meta-data/paper-type)" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="figure[$check-paper-type]">
<image>
<xsl:apply-templates select="@*|node()"/>
</image>
</xsl:template>
我有 xml 如下,
<doc>
<meta-data>
<paper-name>ABC</paper-name>
<paper-type>fi</paper-type>
</meta-data>
<section>
<figure>fig1</figure>
<figure>fig2</figure>
</section>
</doc>
我的要求是,如果 <paper-type>
节点在 <meta-data>
中可用,将 <figure>
节点更改为 <image>
节点。
所以,输出应该是这样的,
<doc>
<meta-data>
<paper-name>ABC</paper-name>
<paper-type>fi</paper-type>
</meta-data>
<section>
<image>fig1</image>
<image>fig2</image>
</section>
</doc>
我已经编写了以下 xsl 来完成此任务,
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:function name="abc:check-paper-type" as="xs:boolean">
<xsl:sequence select="root()//meta-data/paper-type"/>
</xsl:function>
<xsl:template match="figure[abc:check-paper-type()]">
<image>
<xsl:apply-templates/>
</image>
</xsl:template>
检查 <paper-type>
节点在 <meta-data>
中是否可用 我在这里写了一个名为 'check-paper-type' 的函数。但是没有按预期工作。
有什么建议可以组织我的功能来检查,<paper-type>
是否可用?
请注意,我需要通过检查 <paper-type>
节点是否存在来更改大量节点。因此,使用函数检查 <paper-type>
是否存在很重要。
您的尝试失败的原因是:
Within the body of a stylesheet function, the focus is initially undefined; this means that any attempt to reference the context item, context position, or context size is a non-recoverable dynamic error. [XPDY0002]
http://www.w3.org/TR/xslt20/#stylesheet-functions
你可以简单地做:
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="figure[../../meta-data/paper-type]">
<image>
<xsl:apply-templates select="@*|node()"/>
</image>
</xsl:template>
根据您的输入,这将产生:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<meta-data>
<paper-name>ABC</paper-name>
<paper-type>fi</paper-type>
</meta-data>
<section>
<image>fig1</image>
<image>fig2</image>
</section>
</doc>
或者,如果需要重复引用节点的存在,可以将其定义为变量,而不是函数[=28] =]:
<xsl:variable name="check-paper-type" select="exists(/doc/meta-data/paper-type)" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="figure[$check-paper-type]">
<image>
<xsl:apply-templates select="@*|node()"/>
</image>
</xsl:template>