我正在使用XSLT从一个 XML转换为另一个 XML.通过应用论坛中给出的答案,我能够实现我对所需输出的所有要求,但唯一的问题是在输出中一个额外的前缀ns0会自动添加到两个位置并且命名空
输入文件
<?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" xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"> <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" xsi:schemalocation="http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd"> <resources> <resource identifier="RES22" type="webcontent" href="index.html" adlcp:scormtype="sco"> <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:ims="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsl ims adlcp xsi"> <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="/*"> <xsl:element name="{name()}" namespace="{namespace-uri()}"> <xsl:copy-of select="namespace::*[name()]"/> <xsl:apply-templates select="@*"/> <xsl:attribute name="xsi:schemaLocation"> <xsl:value-of select= "'http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'" /> </xsl:attribute> <xsl:apply-templates select="node()"/> </xsl:element> </xsl:template> <xsl:template match="ims:resource" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:attribute name="adlcp:scormtype">sco</xsl:attribute> <xsl:apply-templates select="node()"/> <file href="new1.js"/> <file href="new2.js"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
我得到的输出:
代替
<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">
我明白了
<ns0:manifest xmlns:ns0="http://www.imsglobal.org/xsd/imscp_v1p1" >
而不是
<resources>
我明白了
<resources xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">`
(并且这个xmlns也被添加到其他一些节点的开头),而其余的东西都很好.
谢谢!
无法重现问题!我在提供的XML文档上运行提供的转换,我在计算机上使用了所有11个XSLT处理器,并且所有处理器都生成了相同的,想要的,正确的结果.
无论如何,为什么不尝试这种稍微修改过的转换,这也会产生想要的结果?:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" exclude-result-prefixes="xsl ims adlcp xsi"> <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="/*"> <xsl:element name="{name()}" namespace="{namespace-uri()}"> <xsl:copy-of select="namespace::*[name()]"/> <xsl:apply-templates select="@*"/> <xsl:attribute name="xsi:schemaLocation"> <xsl:value-of select= "'http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'" /> </xsl:attribute> <xsl:apply-templates select="node()"/> </xsl:element> </xsl:template> <xsl:template match="ims:resource"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:attribute name="adlcp:scormtype">sco</xsl:attribute> <xsl:apply-templates select="node()"/> <file href="new1.js" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"/> <file href="new2.js" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"/> </xsl:copy> </xsl:template> </xsl:stylesheet>