将元素转换为属性的XSLT转换

时间:2014-06-15 09:53:45

标签: sql xml xslt xpath xslt-1.0

我正在尝试转换此XML文件:

  <database name="sql_sales">
        <!-- Table Customer -->
        <table name="Customer">
            <column name="CustomerID">1</column>
            <column name="Name">Roth Farrell</column>
            <column name="Age">50</column>
            <column name="Country">Canada</column>
            <column name="City">Fermont</column>
            <column name="Signup_date">2013-03-09 16:34:13</column>
            <column name="Channel">Coupon</column>
        </table>
       <table name="Customer">
        <column name="CustomerID">3</column>
        <column name="Name">Randall Mosley</column>
        <column name="Age">20</column>
        <column name="Country">Belgium</column>
        <column name="City">Leuze</column>
        <column name="Signup_date">2012-03-26 04:02:37</column>
        <column name="Channel">SEO</column>
    </table>
 </database>

采用以下格式:

     <dataset>
      <Cust CustomerID="1" Name="Roth Farrell" Age="50" Country="Canada" City="Fermont" Signup_date="2012-04-26 17:34:13.0" Channel="Coupon"/>
<Cust CustomerID="3" Name="Randall Mosley" Age="20" Country="Belgium" City="Leuze" Signup_date="2011-05-14 05:02:37.0" Channel="SEO"/>

      </dataset>

我是XSLT的新手,我怎么能用XSLT做到这一点?

感谢

1 个答案:

答案 0 :(得分:2)

使用

<xsl:template match="database">
  <dataset>
    <xsl:apply-templates/>
  </dataset>
</xsl:template>

<xsl:template match="table[@name = 'Customer']">
  <Cust>
    <xsl:apply-templates select="column"/>
  </Cust>
</xsl:template>

<xsl:template match="column">
  <xsl:attribute name="{@name}">
     <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>