2012-08-09 167 views
0

我必须使用XSLT从一个XML转换为另一个XML。我要带属性添加<resource>标签内两个新节点<file href="new1.js"><file href="new2.js">地方,以往任何时候都appears-XSLT:添加具有属性的新节点

我的输入文件:

<?xml version="1.0" encoding="UTF-8"?> 
      <manifest identifier="eXescorm_quiz4823c6301f3d3afc1c1f" 
      xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 
      xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
      xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <resources> 
     <resource identifier="RES22" type="webcontent" href="index.html"> 
       <file href="index.html"/> 
       <file href="common.js"/> 
     </resource> 
    </resources> 
</manifest> 

所需的输出:

<?xml version="1.0" encoding="UTF-8"?> 
    <manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 
       identifier="eXeorm_sample4823c6301f29a89a4c1f" 
       xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
       xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <resources> 
     <resource identifier="RES22" type="webcontent" href="index.html"> 
       <file href="index.html"/>          
       <file href="common.js"/> 
       <file href="new1.js"/> 
       <file href="new2.js"/> 
     </resource> 
    </resources> 
</manifest> 

我XSLT:

 <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xhtml="http://www.imsglobal.org/xsd/imscp_v1p1" 
       xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 
       exclude-result-prefixes="xhtml"> 
       <xsl:output method="html" indent="yes" encoding="UTF-8"/> 
       <xsl:strip-space elements="*" /> 

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

    <xsl:template match="xhtml:resource"> 
    <file href="new1.js"/> 
    <file href="new2.js"/> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

但它不会添加新的文件节点。 谢谢!

回答

1
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1" 
    exclude-result-prefixes="xsl ims"> 
<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="ims:resource" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> 
<xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    <file href="new1.js"/> 
    <file href="new2.js"/> 
</xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感谢。它完美的作品! :) – RahulD 2012-08-10 02:27:43