这是我的输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<Elements xmlns:cs="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ComputerSystem"
xmlns:loc="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_Location"
xmlns:si="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_SystemIdentification">
<Element>
<Identification>
<si:CreationClassName />
<si:Name itam="Other">XXX</si:Name>
<si:NamespaceCreationClassName />
<si:NamespaceName />
<si:ObjectManagerCreationClassName />
</Identification>
<Location>
<loc:Description>Not Populated</loc:Description>
<loc:Name>Not Populated</loc:Name>
<loc:PhysicalPosition />
</Location>
</Element>
我希望使用XSLT v1将其转换为:
<?xml version="1.0" encoding="UTF-8"?>
<Elements xmlns:cs="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ComputerSystem"
xmlns:loc="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_Location"
xmlns:si="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_SystemIdentification">
<Element>
<Identification>
<tns:si_x3a_CreationClassName />
<tns:si_x3a_Name itam="Other">XXX</tns:si_x3a_Name>
<tns:si_x3a_NamespaceCreationClassName />
<tns:si_x3a_NamespaceName />
<tns:si_x3a_ObjectManagerCreationClassName />
</Identification>
<Location>
<tns:loc_x3a_Description>Not Populated</loc_x3a_Description>
<tns:loc_x3a_Name>Not Populated</tns:loc_x3a_Name>
<tns:loc_x3a_PhysicalPosition />
</Location>
</Element>
我的XML文件中还有其他几种模式,“org:”,“si:”......大约十二种,并想学习如何做到这一点。谢谢你的帮助!
答案 0 :(得分:0)
这个“XML元素名称的第一部分”是一个前缀,它只不过是元素名称空间的快捷方式。如果明确指定名称空间,则不需要前缀。例如,这个:
<element xmlns="http://example.com/tns">
与:
相同<tns:element xmlns:tns="http://example.com/tns">
要根据请求转换XML,请尝试以下样式表。请注意名称空间声明以及tns前缀如何绑定到它
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:loc="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_Location"
xmlns:si="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_SystemIdentification"
xmlns:tns="http://example.com/tns"
exclude-result-prefixes="loc si">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- this template is not really required other than for cosmetic purposes -->
<xsl:template match="Elements">
<Elements xmlns:tns="http://example.com/tns">
<xsl:apply-templates select="@*|node()"/>
</Elements>
</xsl:template>
<xsl:template match="si:*">
<xsl:element name="tns:si_x3a_{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="loc:*">
<xsl:element name="tns:loc_x3a_{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>