Link to home
Start Free TrialLog in
Avatar of directxBOB
directxBOBFlag for Ireland

asked on

XSLT : if one element is of value X change another element to value Y

I have this XML File:

<?xml version="1.0"?>
<details>
      <me>
            <email>ABCDE@hotmail.com</email>
            <name>name1</name>
            <password>123456</password>
            <character>player</character>
      </me>
      <me>
            <email>EDCBA@hotmail.com</email>
            <name>name2</name>
            <password>123456</password>
            <character>player</character>
      </me>
</details>

if element email = ABCDE@hotmail.com

then I want to change name to be MY NAME

I use the following:



<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="xml"/>
      <xsl:template match="email">
            <xsl:variable name="email" select="//email"/>
            <xsl:choose>
                  <xsl:when test="$email='ABCDE@hotmail.com'">
                        <email>ABCDE@hotmail.com</email>
                        <xsl:variable name="name" select="//name"/>
                        <xsl:choose>
                              <xsl:when test="$name='name1'">
                                    <name>MYNAME</name>
                              </xsl:when>
                        </xsl:choose>
                  </xsl:when>
                  <xsl:otherwise>
                        <xsl:copy-of select="."/>
                  </xsl:otherwise>
            </xsl:choose>
      </xsl:template>
            <xsl:template match="*">
            <xsl:copy>
                  <xsl:copy-of select="@*" />
                  <xsl:apply-templates />
            </xsl:copy>
      </xsl:template>
</xsl:stylesheet>



any ideaS?
Avatar of directxBOB
directxBOB
Flag of Ireland image

ASKER

oh and my ouput should be:

<?xml version="1.0"?>
<details>
     <me>
          <email>ABCDE@hotmail.com</email>
          <name>MYNAME</name>
          <password>123456</password>
          <character>player</character>
     </me>
     <me>
          <email>EDCBA@hotmail.com</email>
          <name>name2</name>
          <password>123456</password>
          <character>player</character>
     </me>
</details>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
something like this (untested)

    <xsl:template match="name">
          <xsl:choose>
               <xsl:when test="../email = 'ABCDE@hotmail.com'">
                    <name>MYNAME</name>
               </xsl:when>
               <xsl:otherwise>
                    <xsl:copy-of select="."/>
               </xsl:otherwise>
          </xsl:choose>
     </xsl:template>
So reverse my logic?

Will give it a stab.
works a treat.
since you are always writing to the output tree, "where you are"
and you always have access to the full input tree through XPath,

it is best to make decissions through tests, where you are outputing
and make a test path from there

the way I reversed your logic would be the easiest way to do it using XSLT

If you were working with techniques that stream the source XML,
such as SAX, STX or OmniMark, you would create temporary output buffers
and would catch the information in the source XML, while you are at it

cheers