2014-02-14 43 views
0

我有如下一個xml:複製所有節點排除修改的節點

<root> 
    <book>abcd</book> 
    <pen>ghi</pen> 
    <source Qual="poor" vise="n"> 

    <source> 
     <input value="in"></input> 
     <input></input> 
    </source> 

    </source> 
    <class>ab</class> 
    <source>noaatributes</source> 
    <studio>ghi</studio> 
    <source Qual="good" vise="m"> 

    <source> 
     <input></input> 
     <input value="out"></input> 
    </source> 

    </source> 
</root> 

我想將XML轉換在下面的格式:

<?xml version="1.0" encoding="utf-8" ?> 
<root> 
    <book>abcd</book> 
    <pen>ghi</pen> 
    <poor value="in"/> 


    <class>ab</class> 
    <source>noaatributes</source> 
    <studio>ghi</studio> 
    <good value="out"/> 



</root> 

,但我寫的代碼是不是給出所需的輸出..我卡在我的代碼中間..任何建議請。 這是迄今爲止我所編寫的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="@* | node()"> 


     <xsl:apply-templates select="@* | node()" /> 



    </xsl:template> 


    <xsl:template match="source/@Qual"> 
    <xsl:text disable-output-escaping="yes" ><![CDATA[ 
    <]]></xsl:text> 
    <xsl:value-of disable-output-escaping="yes" select="."/> 
    </xsl:template> 

    <xsl:template match="source/source"> 
    <xsl:apply-templates select="input"/> 
    <xsl:text disable-output-escaping="yes" ><![CDATA[ 
    />]]></xsl:text> 
    </xsl:template> 

    <xsl:template match="input"> 
    <xsl:if test ="string-length(./@value) &gt; 0"> 
     <xsl:text disable-output-escaping="yes" ><![CDATA[  
     ]]></xsl:text> 
     <xsl:text disable-output-escaping="yes" ><![CDATA[Value="]]></xsl:text> 
     <xsl:value-of disable-output-escaping="yes" select="./@value"/> 
     <xsl:text disable-output-escaping="yes" ><![CDATA[" ]]></xsl:text> 
    </xsl:if> 

    </xsl:template> 
</xsl:stylesheet> 

回答

0

爲了得到你想要的輸出,它可以更簡單:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="root"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*[not(source[@Qual])]"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 

    <xsl:template match="source[@Qual]"> 
     <xsl:element name="{@Qual}"> 
      <xsl:attribute name="value"> 
       <xsl:value-of select="source/input/@value" /> 
      </xsl:attribute> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 
+0

完美的代碼...感謝... – Blossom

1

這將是小一些

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:template match="node() | @*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="root/source[@Qual]"> 
    <xsl:element name="{@Qual[1]}"> 
     <xsl:copy-of select="source/input/@value[1]"/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 
相關問題