xsl 转换 xml 文档

xml:

 <?xml version="1.0" encoding="UTF-8"?>
<table name="student" id="444">
	<columns>
	
		<column name="name" id="11111" tableId="111111" dataType="varchar" length="2" comment="dddd" uuid="33333" isPk="1" default="" ></column>
<!-- 		<column name="" id="" tableId="" dataType="" length="" comment="" uuid="" isPk="0"></column> -->
<!-- 		<column name="" id="" tableId="" dataType="" length="" comment="" uuid="" isPk="0"></column> -->
<!-- 		<column name="" id="" tableId="" dataType="" length="" comment="" uuid="" isPk="0"></column> -->
		
		
	
	</columns>
</table>

xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<!-- TODO: Auto-generated template -->
		<xsl:apply-templates select="table"></xsl:apply-templates>
	</xsl:template>

	<!-- 	转换 table -->
	<xsl:template match="table">
	<h6>
		-- <xsl:value-of select="./@name"></xsl:value-of>表:<xsl:value-of select="./@comnent"></xsl:value-of>
		create Table <xsl:value-of select="./@name"></xsl:value-of>(
			<!--  判断是否多主键  -->
			<xsl:apply-templates select="columns/column"></xsl:apply-templates>
			
			
			<xsl:if test="count(//@isPk) &gt; 0 ">
				--TODO  联合主键
				,CONSTRAINT <xsl:value-of select="./@name"></xsl:value-of>_PK primary key (
				<xsl:if  test="//column[@isPk=1]"><xsl:apply-templates select="//column[@isPk=1]"></xsl:apply-templates> <!-- <xsl:value-of select="//column[@isPk=1]"></xsl:value-of> --> </xsl:if> <xsl:if test="position()!=last()">,
		 		&#160;</xsl:if>)
			</xsl:if>
		)
	</h6>
	</xsl:template>
	
	<!-- 	转换 column -->
	<xsl:template match="columns/column">
	
		 <xsl:value-of select="./@name"></xsl:value-of> 
		 <!-- 	判断是否有输入长度	  -->
		 <xsl:if test="string-length(./@length) &gt; 0  ">&#160;<xsl:value-of select="./@dataType"></xsl:value-of> </xsl:if>
		 <xsl:if test="string-length(./@default) &gt; 0  "> default <xsl:value-of select="./@default"></xsl:value-of></xsl:if>
		 <xsl:if test="string-length(./@comment) &gt; 0  "> comment <xsl:value-of select="./@comment"></xsl:value-of></xsl:if>
		 <!-- 	判断是否主键	  
 		 <xsl:if test="number(./@isPk) = 1 "> primary key </xsl:if> -->
		 <xsl:if test="position()!=last()">,
		 &#160;</xsl:if>
		
	</xsl:template>
	
	<xsl:template match="//column[@isPk=1]">
		 <xsl:value-of select="./@name"></xsl:value-of>	
		 <xsl:if test="position()!=last()">,
		 &#160;</xsl:if>
		
	</xsl:template> 

</xsl:stylesheet>


转载于:https://my.oschina.net/zhanggf1988/blog/266824

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值