directxBOB
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</charact er>
</me>
<me>
<email>EDCBA@hotmail.com</ email>
<name>name2</name>
<password>123456</password >
<character>player</charact er>
</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@hotmai l.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?
<?xml version="1.0"?>
<details>
<me>
<email>ABCDE@hotmail.com</
<name>name1</name>
<password>123456</password
<character>player</charact
</me>
<me>
<email>EDCBA@hotmail.com</
<name>name2</name>
<password>123456</password
<character>player</charact
</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@hotmai
<email>ABCDE@hotmail.com</
<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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
<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>
ASKER
So reverse my logic?
Will give it a stab.
Will give it a stab.
ASKER
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
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
ASKER
<?xml version="1.0"?>
<details>
<me>
<email>ABCDE@hotmail.com</
<name>MYNAME</name>
<password>123456</password
<character>player</charact
</me>
<me>
<email>EDCBA@hotmail.com</
<name>name2</name>
<password>123456</password
<character>player</charact
</me>
</details>