简体   繁体   中英

XSLT transformation converting elements to attributes

I am trying to transform this XML file :

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

to this format:

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

I am new to XSLT, How can I do this with XSLT?

thanks

Use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM