Insert XPath during copy using apply-templates

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

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.

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> 

And let's say this in the input:

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

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

Thanks again for any assistance!

回答1:

Good one. How about...

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!