当前位置:编程学习 > XML/UML >>

xml文件通过xsl较正

将xml格式的文件,解析,然后,通过xsl矫正,刷选,其中的元素


java代码实现

[java] 
public static Document styleDocument(Document document, String stylesheet) 
            throws Exception { 
 
        // load the transformer using JAXP 
        TransformerFactory factory = TransformerFactory.newInstance(); 
        Transformer transformer = factory.newTransformer(new StreamSource( 
                stylesheet)); 
 
        // now lets style the given document 
        DocumentSource source = new DocumentSource(document); 
        DocumentResult result = new DocumentResult(); 
        transformer.transform(source, result); 
 
        // return the transformed document 
        Document transformedDoc = result.getDocument(); 
        return transformedDoc; 
    } 

待转换的xml
[html] 
    <pre name="code" class="html"><Menus> 
<Items id="MailManager" text="邮件管理" iconCls="menu-mail" isPublic="false"> 
        <Item id="MailView" iconCls="menu-mail" text="发邮件"/> 
        <Item id="MailViewCome" iconCls="menu-mailbox" text="收件箱"> 
            <Function id="_MailDetail" text="查看" iconCls="btn-detail"> 
                <url>/communicate/detailMail.do</url> 
            </Function> 
            <Function id="_MailComDel" text="彻底删除" iconCls="menu-del">  
                <url>/communicate/multiDelMail.do</url>  
                <url>/communicate/listMail.do</url>   
            </Function> 
            <Function id="_MailDel" text="删除" iconCls="menu-del">  
                <url>/communicate/delFlagMail.do</url>  
                <url>/communicate/listMail.do</url>   
            </Function> 
            <Function id="_MailStar" text="标星" iconCls="menu-star">  
                <url>/communicate/multiStarMail.do</url>  
                <url>/communicate/listMail.do</url>   
            </Function> 
        </Item> 
        <Item id="MailViewStar" iconCls="menu-mailbox" text="标星邮件"/> 
        <Item id="MailViewDraft" iconCls="menu-mailbox" text="草稿箱"/> 
        <Item id="MailViewSend" iconCls="menu-mailbox" text="已发送"/>    
        <Item id="MailViewDelete" iconCls="menu-mailbox" text="已删除"/> 
        <Item id="MailViewTrash" iconCls="menu-mailbox" text="垃圾箱"/> 
        <Item id="MailSettingView" iconCls="menu-mailset" text="邮箱设置"/> 
    </Items>  
</Menus></pre><br> 
<pre></pre> 
<p><br> 
</p> 
xsl文件 
<p></p><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 <xsl:output omit-xml-declaration="yes"/> 
    <xsl:strip-space elements="Item"/> 
    <xsl:template match="node()|@*"> 
      <xsl:copy> 
         <xsl:apply-templates select="node()|@*"/> 
      </xsl:copy> 
    </xsl:template> 
    <xsl:template match="Function"/> 
    <xsl:template match="url"/> 
</xsl:stylesheet></pre><br> 
转化之后的文件就不包含,Function,url节点啦,<p></p> 
<p>如图:</p> 
<p><img src="" alt=""><br> 
<br> 
</p> 

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,