gifpif
gifpif

Reputation: 4917

XSLT enumeration mapping

Using xsl to transformer one xml to another xml schema.

how can i map enumeration of one xml with another.

example: xsd 1:

<xsd:element  name="Hindi">
<xsd:simpleType>
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="Chay" />
        <xsd:enumeration value="Roti" />
    </xsd:restriction>
</xsd:simpleType>

xsd 2:

<xsd:element  name="Hindi">
<xsd:simpleType>
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="Tea" />
        <xsd:enumeration value="Bread" />
    </xsd:restriction>
</xsd:simpleType>

How can i map chay with Tea using xsl.

Input xml:

<Hindi>Chay</Hindi>

output xml:

<Hindi>Tea</Hindi>

Upvotes: 0

Views: 2263

Answers (2)

gifpif
gifpif

Reputation: 4917

Solved this by using xsl:when

<English>
    <xsl:choose>
        <xsl:when test="Hindi = 'Chay'">Tea</xsl:when>
        <xsl:when test="Hindi = 'Roti'">Bread</xsl:when>
    </xsl:choose>
<English>   

Upvotes: 2

Christian Mosz
Christian Mosz

Reputation: 543

I hope you are only using two languages or my code will not work. I did just code it quickly, so it might not be the fanciest code. Keep in mind, that the entries have to have the same order. So if you want to translate a word on position x in the enumeration, x has to be the position of the word in the other enumeration too.

Dictionary variable:

<xsl:variable name="dict">
    <element name="Hindi">
        <simpleType>
            <restriction base="xsd:string">
                <enumeration value="Inp1" />
                <enumeration value="Inp2" />
            </restriction>
        </simpleType>
    </element>
    <element name="English">
        <simpleType>
            <restriction base="xsd:string">
                <enumeration value="Bread1" />
                <enumeration value="Bread2" />
            </restriction>
        </simpleType>
    </element>
</xsl:variable>

Match template:

<!-- This will match on every node with a specified element/@name in the dictionary -->
<xsl:template match="*[name() = $dict/element/@name]">
    <xsl:variable name="fromLanguage" select="name()"/>
    <xsl:variable name="wordToTranslate" select="."/>
    <!-- My translation requires the same order of enumerations in order to do it correctly -->
    <!-- I am counting the preceding siblings, so we can take the xth element of the other dictionary -->
    <xsl:variable name="precSiblings" select="$dict/element[@name = $fromLanguage]//enumeration[@value = $wordToTranslate][1]/count(preceding-sibling::*)"/>
    <!-- Renaming the language node -->
    <xsl:element name="{$dict/element[@name != $fromLanguage]/@name}">
        <xsl:value-of select="$dict/element[@name != $fromLanguage]//enumeration[$precSiblings + 1]/@value"/>
    </xsl:element>
</xsl:template>

Upvotes: 1

Related Questions