加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XSLT转换xml文件的案例

发布时间:2020-12-16 05:13:53 所属栏目:百科 来源:网络整理
导读:XML文件: ?xml version="1.0" encoding="utf-8"?dochtmltable a="b"/tabletable style="a:b;" a="b"/tabletable style="WIDTH:100px;" a="b"/tabletable style="WIDTH:100PX;"th TEXT-ALIGN='10' a="a"td BACKGROUND-COLOR="red" TEXT-ALIGN='20' a='b'1/td

XML文件:

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

<table a="b"></table>
<table style="a:b;" a="b"></table>
<table style="WIDTH:100px;" a="b"></table>
<table style="WIDTH:100PX;">
	<th TEXT-ALIGN='10' a="a">
		<td BACKGROUND-COLOR="red" TEXT-ALIGN='20' a='b'>1</td>
	</th>
</table>
<span style="TEXT-DECORATION:underline"><a>ABC</a></span>
<span style="text-decoration:underline"><a>ABC</a></span>
<span style="TEXT-DECORATION:underline" a="b"><a>ABC</a></span>
<span style="color:#ff3300">dfafadssfas</span>
<span style="COLOR:#0033ff">dfafadssfas </span>
<span style="color:red"><a>EFG</a></span>
<p style="COLOR:#ff3300">dfafadssfas </p>
<p style="COLOR:#0033ff">dfafadssfas </p>
<p style="align:center">dfafadssfas </p>

<ul>
  <li>
   	 <p>1</p>
    <ul>
      <li>
        <p><span style="TEXT-DECORATION:underline"><a>ABC</a></span></p>
        <ul>
          <li>
            <p><span style="color:#ff3300"><span style="TEXT-DECORATION:underline"><a>ABC</a></span></span></p>
          </li>
        </ul>
      </li>
    </ul>
  </li>
  <li>1231</li>
</ul>

<ul><ul></ul></ul>


<strong a="1" b="2">a</strong>

<h1>a</h1>
<h2>q</h2>
<h3>w</h3>

<em>em</em>
</html>
</doc>

结果:

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


 <table a="b" />
 <table a="b" />
 <table width="100px&#10;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;"
  a="b" />
 <table width="100px&#10;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;">
  <th align="10" a="a">
   <td bgcolor="red" align="20" a="b">1</td>
  </th>
 </table>
 <u>
  <a>ABC</a>
 </u>
 <u>
  <a>ABC</a>
 </u>
 <u a="b">
  <a>ABC</a>
 </u>
 <red>dfafadssfas</red>
 <blue>dfafadssfas </blue>
 <span>
  <a>EFG</a>
 </span>
 <red>dfafadssfas </red>
 <blue>dfafadssfas </blue>
 <p>dfafadssfas </p>

 <ul1>
  <li1>
   <p>1</p>
   <ul2>
    <li2>
     <p>
      <u>
       <a>ABC</a>
      </u>
     </p>
     <ul3>
      <li3>
       <p>
        <red>
         <u>
          <a>ABC</a>
         </u>
        </red>
       </p>
      </li3>
     </ul3>
    </li2>
   </ul2>
  </li1>
  <li1>1231</li1>
 </ul1>

 <ul1>
  <ul2 />
 </ul1>


 <b a="1" b="2">a</b>

 <h1>a</h1>
 <h2>q</h2>
 <h3>w</h3>

 <i>em</i>

</jpxml>



XSL文件内容:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" encoding="utf-8"
		indent="yes" cdata-section-elements="DataContent " />
	
	<xsl:template match="/">
		<jpxml>
			<xsl:apply-templates />
		</jpxml>
	</xsl:template>
	
<!-- 	ul,ol的转换 -->
	<xsl:template match="ul|ol">
		<xsl:choose>
			<xsl:when test="number(count(ancestor::ul)) > 0">
				<xsl:element name="{concat('ul',number(count(ancestor-or-self::ul)))}">
					<xsl:for-each select="@*">
						<xsl:copy/>
					</xsl:for-each>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="ul1">
					<xsl:for-each select="@*">
						<xsl:copy/>
					</xsl:for-each>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

<!-- 	li的转换 -->
	<xsl:template match="li">
		<xsl:choose>
			<xsl:when test="number(count(ancestor::li)) > 0">
				<xsl:element name="{concat('li',number(count(ancestor-or-self::li)))}">
					<xsl:for-each select="@*">
						<xsl:copy/>
					</xsl:for-each>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="li1">
					<xsl:for-each select="@*">
						<xsl:copy/>
					</xsl:for-each>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	
<!-- 	表格处理:style含有WIDTH的变成width属性,删除style属性 -->
					
	<xsl:template match="table">
		<xsl:element name="{name(.)}">
			<xsl:choose>
				<!-- 判断当前table是否含有style属性值WIDTH -->
				<xsl:when test="contains(@style,'WIDTH')">
					<xsl:for-each select="@*">
						<!-- 判断当前属性是否是style -->
						<xsl:choose>
							<!-- 当前属性是style -->
							<xsl:when test="name(.) = 'style'">
									<xsl:attribute name="width">
										<!--判断width的属性值是否有px -->
										<xsl:choose>
											<xsl:when test="contains(substring-before(substring-after(string(.),'WIDTH:'),';'),'px')"> 
												<xsl:value-of select="number(substring-before(substring-after(string(.),'px;'))"/>px
			 								</xsl:when>
			 								<xsl:when test="contains(substring-before(substring-after(string(.),'PX')"> 
			 									<xsl:value-of select="number(substring-before(substring-after(string(.),'PX;'))"/>px
			 								</xsl:when>
											<!-- WITDTH值没有px -->
											<xsl:otherwise>
												<xsl:value-of select="number(substring-before(substring-after(string(.),';'))"/>
											</xsl:otherwise>
										</xsl:choose>
									</xsl:attribute>
							</xsl:when>
							<xsl:otherwise>
								<!-- 当前属性不是style -->
								<xsl:attribute name="{name(.)}">
									<xsl:value-of select="."/>
								</xsl:attribute>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="@*">
						<xsl:if test="name(.) != 'style'">
							<xsl:attribute name="{name(.)}">
								<xsl:value-of select="."/>
							</xsl:attribute>
						</xsl:if>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	
<!-- 	td处理:将TEXT-ALIGN属性改为align,BACKGROUND-COLOR改为bgcolor,属性值不变 -->
	<xsl:template match="td">
		<xsl:element name="{name(.)}">
			<xsl:for-each select="@*">
				<xsl:choose>
					<xsl:when test="name(.) = 'TEXT-ALIGN'">
						<xsl:attribute name="align">
							<xsl:value-of select="."/>
						</xsl:attribute>	
					</xsl:when>
					<xsl:when test="name(.) = 'BACKGROUND-COLOR'">
						<xsl:attribute name="bgcolor">
							<xsl:value-of select="."/>
						</xsl:attribute>	
					</xsl:when>
					<xsl:otherwise>
						<xsl:copy/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
		
<!-- 	th处理:将TEXT-ALIGN属性改为align,属性值不变 -->
	<xsl:template match="th">
		<xsl:element name="{name(.)}">
			<xsl:for-each select="@*">
				<xsl:choose>
					<xsl:when test="name(.) = 'TEXT-ALIGN'">
						<xsl:attribute name="align">
							<xsl:value-of select="."/>
						</xsl:attribute>	
					</xsl:when>
					<xsl:otherwise>
						<xsl:copy/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	
<!-- 	<span style="TEXT-DECORATION:underline">ABC</span> -->
<!-- <span style="COLOR:#ff3300">dfafadssfas </span>  -->
<!-- span标签下划线和两种颜色的转换和去除span标签中的属性和值 -->

	<xsl:template match="span">
		<xsl:choose>
			<xsl:when test="@style = 'TEXT-DECORATION:underline' or @style = 'text-decoration:underline'">
				<xsl:element name="u">
					<xsl:for-each select="@*">
						<xsl:if test="name(.) != 'style'">
							<xsl:attribute name="{name(.)}">
								<xsl:value-of select="."/>
							</xsl:attribute>
						</xsl:if>
					</xsl:for-each>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:when test="@style = 'COLOR:#ff3300' or @style = 'color:#ff3300'">
				<xsl:element name="red">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:when test="@style = 'COLOR:#0033ff' or @style = 'color:#0033ff'">
				<xsl:element name="blue">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="{name(.)}">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
<!-- <p style="COLOR:#ff3300">dfafadssfas </p> -->
<!-- p标签两种颜色和删除属性和值的转换 -->
	<xsl:template match="p">
		<xsl:choose>
			<xsl:when test="@style = 'COLOR:#ff3300' or @style = 'color:#ff3300'">
				<xsl:element name="red">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:when test="@style = 'COLOR:#0033ff' or @style = 'color:#0033ff'">
				<xsl:element name="blue">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="{name(.)}">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	
	</xsl:template>


	<xsl:template match="a">
		<xsl:element name="{name(.)}">
			<xsl:for-each select="@*">
				<xsl:copy />
			</xsl:for-each>
			<xsl:apply-templates />
		</xsl:element>

	</xsl:template>

	<xsl:template name="strong" match="strong">
		<xsl:element name="b">
			<xsl:for-each select="@*">
				<xsl:copy />
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="em">
		<xsl:element name="i">
			<xsl:for-each select="@*">
				<xsl:copy />
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="h1|h2|h3">
		<xsl:element name="{name(.)}">
			<xsl:for-each select="@*">
				<xsl:copy/>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

</xsl:stylesheet>

Java代码:
public static void main(String argv[]){
		String src = "E:workspace2test1WebContenttest2a.xml";
		String dest = "E:workspace2test1WebContenttest2a1.xml";
		String xslt = "E:workspace2test1WebContenttest2a.xsl";
//		String dest = "E:workspace2.metadata.pluginsorg.eclipse.wst.server.coretmp1wtpwebappstest1WEB-INFresultsa2.xml";
//		String xslt = "E:workspace2.metadata.pluginsorg.eclipse.wst.server.coretmp1wtpwebappstest1WEB-INFtemplatesa.xsl";

		File src2 = new File(src);
		File dest2 = new File(dest);
		File xslt2 = new File (xslt);

		Source srcSource = new StreamSource(src2);
		Result destResult = new StreamResult(dest2);
		Source xsltSource = new StreamSource(xslt2);

		try{
			TransformerFactory transFact = TransformerFactory.newInstance();
			Transformer trans = transFact.newTransformer(xsltSource);
			trans.transform(srcSource,destResult);
		}catch(TransformerConfigurationException e){
			e.printStackTrace();
		}catch(TransformerFactoryConfigurationError e){
			e.printStackTrace();
		}catch(TransformerException e){
			e.printStackTrace();
		}
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读