您的位置:首页 > 其它

利用XSL双向转换XML文档

2017-02-07 10:19 441 查看
出处:http://www.cnblogs.com/erqie/articles/1144143.html

在实际应用中,经常要用到XML文件来存储相关的信息.而XML文件可以有三种结构:

1:层次结构


<DBTableName>


  <TableInfo>


    <Name>SZYLTJ</Name>


    <Type>GB</Type>


    <Info>水资源量基本情况统计表(多年平均)</Info>


  </TableInfo>


  <TableInfo>


    <Name>SZYKFLY</Name>


    <Type>GB</Type>


    <Info>水资源开发利用情况</Info>


  </TableInfo>


  <TableInfo>


    <Name>JSL_XZ</Name>


    <Type>GB</Type>


    <Info>行政分区降水量</Info>


  </TableInfo>


</DBTableName>
即,所有的信息都在元素的形式存储.

2.属性结构


<DBTableName>


  <table type="gb" name="HSL_XZ" ch="行政分区耗水量" />


  <table type="gb" name="HSL_LY" ch="流域分区耗水量" />


  <table type="gb" name="HLSZ" ch="河流水质" />


  <table type="gb" name="HBSZ" ch="湖泊水质" />


  <table type="gb" name="SKSZ" ch="水库水质" />


  <table type="gb" name="HLWRJC" ch="河断面水质监测" />


  <table type="gb" name="PYDXWR" ch="平原区地下水水质污染(浅层)" />


  <table type="gb" name="SSJSTSZ" ch="市界水体水质" />


  <table type="gb" name="PYDXSZ" ch="平原区浅层地下水水质" />


  <table type="gb" name="ZYSS" ch="重要水事" />


</DBTableName>
所有的信息都以属性的形式存储

3.混合结构:以上两种结构的混合.如


<DBTableName>


  <table type="gb">


    <name>aa</name>


    <info>哈哈</info>


  </table>


  <table type="gb">


    <name>bb</name>


    <info>呵呵</info>


</table>


</DBTableName>
在asp.net中利用xmldatasource控件进行数据绑定的时候,基绑定的XML数据是有要求的.如在绑定到dropdownlist,gridview等控件的时候,需要XML文档为信息存储在属性里.即上述第二种结构.但如果要数据绑定到treeview控件里,则只能显示没有层次结构性的信息.如果用上述第一第元素结构的XML文件,可以显示详细的层次结构.

要进行这两种结构的软件,只需要包含几句XSL语句的转换文件.

1.转换元素结构文件为属性结构文件:


<?xml version="1.0"?>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


  <xsl:template match="/">


    <DBTableName>


    <xsl:for-each select="DBTableName/TableInfo">


      <table>


        <xsl:attribute name="name">


          <xsl:value-of select="Name"/>


        </xsl:attribute>


        <xsl:attribute name="type">


          <xsl:value-of select="Type"/>


        </xsl:attribute>


        <xsl:attribute name="info">


          <xsl:value-of select="Info"/>


        </xsl:attribute>


      </table>


    </xsl:for-each>


    </DBTableName>


  </xsl:template>


  </xsl:stylesheet>
则第一个示例的输出结果为:


<?xml version="1.0" encoding="utf-8"?>


<DBTableName>


  <table name="SZYLTJ" type="GB" info="水资源量基本情况统计表(多年平均)" />


  <table name="SZYKFLY" type="GB" info="水资源开发利用情况" />


  <table name="JSL_XZ" type="GB" info="行政分区降水量" />


</DBTableName>
2.转换属性信息为元素结构


<?xml version="1.0"?>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


  <xsl:template match="/">


    <DBTableName>


      <xsl:for-each select="DBTableName/table">


        <table>


          <xsl:element name="name">


            <xsl:value-of select="@name"/>


          </xsl:element>


          <xsl:element name="type">


            <xsl:value-of select="@type"/>


          </xsl:element>


          <xsl:element name="ch">


            <xsl:value-of select="@ch"/>


          </xsl:element>


        </table>


      </xsl:for-each>


    </DBTableName>


  </xsl:template>


</xsl:stylesheet>
输出结果为:


<?xml version="1.0" encoding="utf-8"?>


<DBTableName>


  <table>


    <name>HSL_XZ</name>


    <type>gb</type>


    <ch>行政分区耗水量</ch>


  </table>


  <table>


    <name>HSL_LY</name>


    <type>gb</type>


    <ch>流域分区耗水量</ch>


  </table>


  <table>


    <name>HLSZ</name>


    <type>gb</type>


    <ch>河流水质</ch>


  </table>


  <table>


    <name>HBSZ</name>


    <type>gb</type>


    <ch>湖泊水质</ch>


  </table>


  <table>


    <name>SKSZ</name>


    <type>gb</type>


    <ch>水库水质</ch>


  </table>


  <table>


    <name>HLWRJC</name>


    <type>gb</type>


    <ch>河断面水质监测</ch>


  </table>


  <table>


    <name>PYDXWR</name>


    <type>gb</type>


    <ch>平原区地下水水质污染(浅层)</ch>


  </table>


  <table>


    <name>SSJSTSZ</name>


    <type>gb</type>


    <ch>市界水体水质</ch>


  </table>


  <table>


    <name>PYDXSZ</name>


    <type>gb</type>


    <ch>平原区浅层地下水水质</ch>


  </table>


  <table>


    <name>ZYSS</name>


    <type>gb</type>


    <ch>重要水事</ch>


  </table>


</DBTableName>
当然,也可以根据需要转换为混合结构.用到的XSL语句就两种:

如果要生成元素,则用<xsl:element name="">如果生成属性,则为<xsl:attribute name="">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  XML XSLT XSL