Xslt生成器到sql代码

时间:2011-06-13 07:35:15

标签: asp.net xslt

  <xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="/Update/Object/Insert">
  Insert into <xsl:value-of select="@Table" />(
  <xsl:for-each select="Field">
    <xsl:value-of select="@Name" />,      
  </xsl:for-each>)
  Values(
    <xsl:for-each select="Field">
      <xsl:value-of select="@Value" />,
    </xsl:for-each>)
</xsl:template>

输出 插入好(       GoodCode,       ...
      ,
      )       值(         1,         ...         ,
        )

你可以看到有一个额外的“,”问题是我如何纠正它?

    <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='book.xslt'?>
<Update>
  <Object Name="Good">
    <Insert Table="Good">
      <Field Name="GoodCode" Value="1" Type="Integer" />
      <Field Name="GoodUserCode" Value="" Type="String" />
</insert>
</object>

1 个答案:

答案 0 :(得分:2)

使用类似的东西:

 <xsl:output method="text" omit-xml-declaration="yes" />
   <xsl:template match="/Update/Object/Insert">
      Insert into <xsl:value-of select="@Table" />(
   <xsl:for-each select="Field">
      <xsl:value-of select="@Name" /><xsl:if test = "position() != last()">, </xsl:if>      
   </xsl:for-each>)
   Values(
   <xsl:for-each select="Field">
      <xsl:value-of select="@Value" /><xsl:if test = "position() != last()">, </xsl:if>
   </xsl:for-each>)
 </xsl:template>

http://www.w3schools.com/xsl/el_if.asp