Changing the value of an attribute depending on the value of the second attribute of the same element

This is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <trans>
       <language type="lang" lang="DE"/>
    </trans>
    <trans>
       <language type="lang" lang="EN"/>
    </trans>
</root>

      

My goal is to replace the value of the "type" attribute depending on the value specified in the "lang" attribute.

This is the desired output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <trans>
       <language type="German" lang="DE"/>
    </trans>
    <trans>
       <language type="English" lang="EN"/>
    </trans>
</root>

      

I started with the following but don't get it at the moment:

   <xsl:template match="language">
    <xsl:choose>
     <xsl:when test="@lang=DE">
       <xsl:attribute name="type">
         <xsl:value-of select="'German'"/>
   <xsl:apply-templates/>
     </xsl:when>
    <xsl:otherwise>
    ???

      

Any help was appreciated.

+3


source to share


3 answers


Try these 3 patterns (the first is the identity pattern, the second will eat all @type, and the third will generate both attributes again based on @type):



<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="//language/@type"/>

<xsl:template match="//language/@lang">
  <xsl:attribute name="lang">
    <xsl:value-of select="."/>
  </xsl:attribute>
  <xsl:attribute name="type">
    <xsl:choose>
      <xsl:when test=".='DE'">German</xsl:when>
      <xsl:when test=".='EN'">English</xsl:when>
      <xsl:otherwise>Other</xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
</xsl:template>

      

+1


source


XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:data="data">
  <xsl:output method="xml" indent="yes"/>
  <data:languages>
    <lang key="DE" value="German"/>
    <lang key="EN" value="English"/>
  </data:languages>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="language">
    <xsl:copy>
      <xsl:apply-templates select="@*[name() != 'type']"/>
      <xsl:attribute name="type">
        <xsl:value-of select="document('')//lang[@key = current()/@lang]/@value"/>
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

      



Output:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <trans>
    <language lang="DE" type="German" />
  </trans>
  <trans>
    <language lang="EN" type="English" />
  </trans>
</root>

      

+1


source


If there are only three languages, and you only use this translation in one place, then it xsl:choose

makes an attractive choice. Of course, it can be implemented much more simply:

XSLT 1.0 or 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="language/@type">
    <xsl:attribute name="type">
        <xsl:choose>
            <xsl:when test="../@lang='DE'">German</xsl:when>
            <xsl:when test="../@lang='EN'">English</xsl:when>
            <xsl:when test="../@lang='FR'">French</xsl:when>
        </xsl:choose>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

      

With a lot of languages ​​and / or if you need to reuse code in multiple places, it might be more convenient to switch to search:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:variable name="languages">
    <language code="DE">German</language>
    <language code="EN">English</language>
    <language code="FR">French</language>
</xsl:variable>

<xsl:key name="lang" match="language" use="@code" />

<xsl:template match="language/@type">
    <xsl:attribute name="type">
        <xsl:value-of select="key('lang', ../@lang, $languages)"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

      

0


source







All Articles