我正在使用 XSLT 从一个 XML 转换为另一个 XML。通过应用论坛中给出的答案,我能够实现所需输出的所有要求,但唯一的问题是在输出中,一个额外的前缀ns0
会在两个位置自动添加,并且命名空间xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
会在每个节点的开头添加.
输入文件
<?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 也被添加到其他一些节点的开头),而其余的一切都很好。
谢谢!