Unity 1 to Unity 2 configuration xslt

Jan 18, 2011 at 10:35 AM
Edited Jan 18, 2011 at 10:47 AM

Hi,

I prepared some xslt to transform from Unity 1 to Unity 2.

Feel free to use it/modify it.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes" />
  <xsl:variable name="ns">http://schemas.microsoft.com/practices/2010/unity</xsl:variable>
  <xsl:variable name="newline">
    <xsl:text>
</xsl:text>
  </xsl:variable>
  <xsl:template match="configuration/unity">
    <xsl:element name="configuration">
      <xsl:value-of select="$newline"/>
      <xsl:element name="configSections">
        <xsl:value-of select="$newline"/>
        <xsl:element name="section">
          <xsl:attribute name="name">unity</xsl:attribute>
          <xsl:attribute name="type">Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration</xsl:attribute>
        </xsl:element>
        <xsl:value-of select="$newline"/>
      </xsl:element>
      <xsl:value-of select="$newline"/>
      <xsl:element name="unity">
        <xsl:for-each select="typeAliases/typeAlias">
          <xsl:value-of select="$newline"/>
          <xsl:element name="alias">
            <xsl:attribute name="alias">
              <xsl:value-of select="@alias"/>
            </xsl:attribute>
            <xsl:attribute name="type">
              <xsl:value-of select="@type"/>
            </xsl:attribute>
          </xsl:element>
        </xsl:for-each>
        <xsl:for-each select="containers/container">
          <xsl:value-of select="$newline"/>
          <xsl:element name="container">
            <xsl:for-each select="types/type">
              <xsl:value-of select="$newline"/>
              <xsl:element name="register">
		<xsl:if test="string-length(@name)>0">
                  <xsl:attribute name="name">
                    <xsl:value-of select="@name"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="string-length(@type)>0">
                  <xsl:attribute name="type">
                    <xsl:value-of select="@type"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="string-length(@mapTo)>0">
                  <xsl:attribute name="mapTo">
                    <xsl:value-of select="@mapTo"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:for-each select="lifetime">
                  <xsl:value-of select="$newline"/>
                  <xsl:element name="lifetime">
                    <xsl:attribute name="type">
                      <xsl:value-of select="@type"/>
                    </xsl:attribute>
                  </xsl:element>
                </xsl:for-each>
                <xsl:for-each select="typeConfig/constructor">
                  <xsl:value-of select="$newline"/>
                  <xsl:element name="constructor">
                    <xsl:for-each select="param">
                      <xsl:value-of select="$newline"/>
                      <xsl:element name="param">
                        <xsl:attribute name="name">
                          <xsl:value-of select="@name"/>
                        </xsl:attribute>
                        <xsl:if test="string-length(@parameterType)>0">
                          <xsl:attribute name="type">
                            <xsl:value-of select="@parameterType"/>
                          </xsl:attribute>
                        </xsl:if>
                        <xsl:apply-templates select="dependency"/>
                        <xsl:apply-templates select="array"/>
                        <xsl:apply-templates select="value"/>
                        <xsl:if test="name(child::node())='array'">
                          <xsl:value-of select="$newline"/>
                        </xsl:if>
                        <xsl:if test="name(child::node())='value' and string-length(child::node()/@typeConverter)>0">
                          <xsl:value-of select="$newline"/>
                        </xsl:if>
                      </xsl:element>
                    </xsl:for-each>
                    <xsl:value-of select="$newline"/>
                  </xsl:element>
                </xsl:for-each>
                <xsl:for-each select="typeConfig/property">
                  <xsl:value-of select="$newline"/>
                  <xsl:element name="property">
                    <xsl:attribute name="name">
                      <xsl:value-of select="@name"/>
                    </xsl:attribute>
                    <xsl:apply-templates select="dependency"/>
                    <xsl:apply-templates select="array"/>
                    <xsl:apply-templates select="value"/>
                    <xsl:if test="name(child::node())='array'">
                      <xsl:value-of select="$newline"/>
                    </xsl:if>
                    <xsl:if test="name(child::node())='value' and string-length(child::node()/@typeConverter)>0">
                      <xsl:value-of select="$newline"/>
                    </xsl:if>
                  </xsl:element>
                </xsl:for-each>
                <xsl:value-of select="$newline"/>
              </xsl:element>
            </xsl:for-each>
            <xsl:value-of select="$newline"/>
          </xsl:element>
        </xsl:for-each>
        <xsl:value-of select="$newline"/>
      </xsl:element>
      <xsl:value-of select="$newline"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="dependency">
    <xsl:choose>
      <xsl:when test="name(..)='array'">
        <xsl:value-of select="$newline"/>
        <xsl:element name="dependency">
          <xsl:attribute name="name">
            <xsl:value-of select="@name"/>
          </xsl:attribute>
          <xsl:if test="string-length(@type)>0">
            <xsl:attribute name="type">
              <xsl:value-of select="@type"/>
            </xsl:attribute>
          </xsl:if>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="dependencyName">
          <xsl:value-of select="@name"/>
        </xsl:attribute>
        <xsl:if test="string-length(@type)>0">
          <xsl:attribute name="dependencyType">
            <xsl:value-of select="@type"/>
          </xsl:attribute>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="array">
    <xsl:value-of select="$newline"/>
    <xsl:element name="array">
      <xsl:apply-templates select="array"/>
      <xsl:apply-templates select="value"/>
      <xsl:apply-templates select="dependency"/>
      <xsl:value-of select="$newline"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="value">
    <xsl:choose>
      <xsl:when test="name(..)='array' or string-length(@typeConverter)>0">
        <xsl:value-of select="$newline"/>
        <xsl:element name="value">
          <xsl:attribute name="value">
            <xsl:value-of select="@value"/>
          </xsl:attribute>
          <xsl:if test="string-length(@typeConverter)>0">
            <xsl:attribute name="typeConverter">
              <xsl:value-of select="@typeConverter"/>
            </xsl:attribute>
          </xsl:if>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="value">
          <xsl:value-of select="@value"/>
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>


 

Jan 19, 2011 at 5:51 AM

Very cool, thanks for sharing this!