本文介绍了使用apply-templates在复制过程中插入XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用身份模板和与源中潜在XPath匹配的几个模板创建了XSLT.但是,匹配路径并不总是存在.有没有一种方法可以在应用匹配模板之前插入"路径?由于我知道XSLT不会按程序执行,因此我不确定如何执行此操作.下面的示例.

I've created an XSLT using an identity template and several templates that match to a potential XPath in the source. However, the matching paths do not always exist. Is there a way to "insert" the path before the matching template applies? Since I know XSLT does not execute procedurally, I wasn't sure how to do this. Examples below.

让我们说这是XSLT:

Let's say this is the XSLT:

<xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:if>
</xsl:template>

<xsl:template match='pathA'>
   do stuff
</xsl:template>

<xsl:template match='pathB'>
  do something
</xsl:template>

<xsl:template match='pathC'>
   do other stuff
</xsl:template>

让我们在输入中说:

<root>
    <Child>
        <pathA>I have Data!</pathA>
        <pathC>We skipped B!</pathC>
    </Child>
</root>

是否可以创建" pathB,以便与XPath匹配的模板可以执行?

Is there a way to "create" pathB so that the template that matches the XPath can execute?

再次感谢您的帮助!

推荐答案

好人.怎么样...

<xsl:template name="pathB">
    <xsl:param name="nodes"/>
    do something 
</xsl:template>


<xsl:template match="Child">
    <xsl:copy>
        <xsl:apply-templates select="@* | pathA[not(../pathB)] | pathB/preceding-sibling::node()"/>
    </xsl:copy>

   <xsl:call-template name="pathB">
       <!-- pass the set of elements of type "pathB", possibly an empty nodeset -->
       <xsl:with-param name="nodes" select="pathB"/>
   </xsl:call-template>

    <xsl:copy>
        <xsl:apply-templates select="node()[not(self::pathA) and not(../pathB)] | pathB/following-sibling::node()"/>
    </xsl:copy>
<xsl:template>

这篇关于使用apply-templates在复制过程中插入XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:37