XSLT 和 Java:默认命名空间 XMLNS



我是XSLT的新手,我正在尝试将一个XML文件转换为另一个XML文件。我的问题是原始 xml 文件中有一个命名空间"xmlns"没有任何前缀,当我通过 xslt 将其转换为另一个命名空间时,什么也没发生,但是如果我删除该 xmlns 命名空间,那么它会起作用,但实际上我无法修改原始.xml文件,因为我只能使用该文件, 所以我必须将 xmlns 保留在原始文件中。所以任何人都可以建议对我的.xsl或java代码进行一些修改来克服这个问题。

我的原始 XML 命名空间如下所示-

<?xml version="1.0" encoding="UTF-8"?>
        <manifest identifier="eXeorm_sample4823c6301f29a89a4c1f"
        xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1"
        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"      
</manifest>

我想要的 xml 是:

 <?xml version="1.0" encoding="UTF-8"?>
        <manifest identifier="eXescorm_quiz4823c6301f29a8419515" 
            xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" 
            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" 
        </manifest>

我修改的 XSLT-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:ims="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" 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="ims:manifest">   
</xsl:stylesheet> 

本页顶部有一个搜索框:键入"XSLT 默认命名空间",您将找到数百个关于这个问题的答案。

顺便说一下,你的代码非常冗长。取而代之的是:

<xsl:element name="item">
    <xsl:attribute name="identifier">ITEM-eXeorm_sample4823c6301f29a89a4d27</xsl:attribute>
    <xsl:attribute name="isvisible">true</xsl:attribute>
    <xsl:attribute name="identifierref">RES-eXeorm_sample4823c6301f29a89a4d28</xsl:attribute>
</xsl:element>

使用这个:

<item identifier="ITEM-eXeorm_sample4823c6301f29a89a4d27" invisible="true"
      identifierref="RES-eXeorm_sample4823c6301f29a89a4d28"/>

最新更新