2016-04-21 34 views
0

我有喜欢的XML如下XSLT - 校验节点存在或者不使用功能

<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>我写了一个函数这里命名“签纸类型”。但不能按预期工作。

任何建议如何组织我的功能检查,<paper-type>是否可用?

请注意,我需要通过检查<paper-type>节点是否存在来更改大量节点。所以,使用函数检查<paper-type>是否存在非常重要。

回答

1

之所以你的企图不能工作是这样的:

在样式表功能的机构,重点是最初 不确定的;这意味着任何引用上下文项目,上下文位置或上下文大小的尝试都是不可恢复的动态错误。 [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> 

另外,如果您需要指存在节点的ENCE反复,你可以将其定义为变量,而不是功能

<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> 
+0

这是一个示例代码,我需要通过检查节点改变很多节点名称是存在或不。因此,如果我可以使用函数来检查是否可用,它将会更加有效。我对原始问题缺乏细节表示歉意。 – sanjay

+0

看看编辑答案是否有帮助。 –