Question

Here is my input 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>

and I would like to transform it using XSLT v1 into:

    <?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>

There are several other patterns in my XML files, "org:", "si:"... about twelve in all and wanted to learn how to do this. Thank you for your help!

Was it helpful?

Solution

This "first part of an XML element name" is a prefix that's no more than a shortcut for the namespace of the element.The prefix is not really required if the namespace is assigned explicitly. For example, this:

<element xmlns="http://example.com/tns">

is the same as:

<tns:element xmlns:tns="http://example.com/tns">

To transform your XML as requested, try the following stylesheet. Note the namespace declaration and how the tns prefix is bound to it

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top