如何使用XSLT版本1转换XML元素名称的第一部分?
以下是我的输入XML:如何使用XSLT版本1转换XML元素名称的第一部分?,xml,xslt,xslt-1.0,transformation,Xml,Xslt,Xslt 1.0,Transformation,以下是我的输入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://schema
<?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>
XXX
未填充
未填充
我想使用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>
XXX
未填充
未填充
在我的XML文件中还有几个其他模式,“org:”,“si:”。。。一共有12个人,他想学怎么做。谢谢你的帮助 此“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>
如果希望在XML文档中有一个类似于
tns:si_x3a\u CreationClassName
的名称,则需要为该前缀tns
提供命名空间声明。您的示例没有名称空间。请注意,
和
是同一件事–您的特定xslt实现可能提供也可能不提供为名称空间设置特定名称的方法,因此您可能希望告诉我们您使用的名称空间。