开发者

overriding node check by xsl compile

I wish be build the following xsl structure below:

<body>
<item></item>
<item></item>
<item></item>
</body>

the number of items between the body node is variable so I want to call a template with param 'place' where if the value is start or end the body node is created or closed respectively. The basic code structure is below. The issue i have is that its not possible to use this method as you cant have the node open when closing the when statement. How can I overri开发者_开发技巧de this behaviour of the xsl compiler

<xsl:choose>
  <xsl:when test="$place='start'">
   <body>
  </xsl:when>
  <xsl:when test="$place='end'">
   </body>
  </xsl:when>
</xsl:choose>


I want to call a template with param 'place' where if the value is start or end the body node is created or closed respectively

This is impossible. You cannot have incorrectly nested tags in XSLT (just like you cannot have incorrectly nested braces/parentheses/control structures in any other programming language).

Luckily, this is also completely unnecessary. This will do what you want:

<xsl:template match="body">
  <body>
    <xsl:apply-templates select="item" /> 
  </body>
</xsl:template>

<xsl:template match="item">
  <!-- whatever you want to do with the items -->
</xsl:template>


You need something like this:

<xsl:param name="tot" select="3"/>

<xsl:template match="body">
  <body>
    <xsl:call-template name="add-items" /> 
  </body>
</xsl:template>

<xsl:template name="add-items">
 <xsl:param name="num" select="1"/>
 <item />
 <xsl:if test="$num &lt; $tot">
  <xsl:call-template name="add-items">
   <xsl:with-param name="num" select="$num + 1"/>
  </xsl:call-template>
 </xsl:if>
</xsl:template>


XSLT doesn't "create" and "close" nodes as separate operations: you can't do one and not the other. It builds a result tree, and a node either exists on the result tree or it doesn't. You're thinking about tags when you need to think about a tree of nodes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜