XSL如何使用xsl:number连接文本?

时间:2012-04-10 14:01:16

标签: xml xslt xsl-fo

我是xsl的新手并试图编写一个脚本来解析xml消息并调用一个模板(我无法控制它来格式化输入)。 XML消息中的某些字段可以多次出现;在这些情况下,我想在“StringValue”的末尾附加一个数字,并将其传递给调用模板的with-param =“pet”,它指定它出现的位置。

下面的代码似乎做我想要的;但是XML Spy说它是无效的XML。是否有更好的方法为“Dog / Color”的每个模板匹配追加“StringValue”和“1”,“StringValue”和“2”等?

例如:

<xsl:template match="Dog/Color">
    <xsl:call-template name="FormatContents">
            <xsl:with-param name="pet">StringValue<xsl:number level="any"/></xsl:with-param>
            <xsl:with-param name="color">
                    <xsl:value-of select="."/>
            </xsl:with-param>
    </xsl:call-template>
</xsl:template>

提前致谢!

2 个答案:

答案 0 :(得分:2)

您提供的片段是有效的,您确定错误来自该位吗?最好发布完整的例子。

输入:

<Dog>
<Color>Red</Color>
<Color>Green</Color>
</Dog>

XSLT 1:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Dog/Color">
 <xsl:call-template name="FormatContents">
  <xsl:with-param name="pet">StringValue<xsl:number level="any"/></xsl:with-param>
  <xsl:with-param name="color">
   <xsl:value-of select="."/>
  </xsl:with-param>
 </xsl:call-template>
</xsl:template>

<xsl:template name="FormatContents">
 <xsl:param name="pet"/>
 <xsl:param name="color"/>
 [<xsl:value-of select="$pet"/>][<xsl:value-of select="$color"/>]
</xsl:template>
</xsl:stylesheet>

输出:

 [StringValue1][Red]


 [StringValue2][Green]

答案 1 :(得分:0)

我认为你想要的是position() - 即:

<xsl:template match="Dog/Color"> 
  <xsl:call-template name="FormatContents"> 
     <xsl:with-param name="pet">StringValue<xsl:value-of select="position()"/></xsl:with-param> 
     <xsl:with-param name="color"> 
       <xsl:value-of select="."/> 
     </xsl:with-param> 
  </xsl:call-template> 
</xsl:template>