Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Fri, 8 Feb 2008 12:27:02 +0100,    group: microsoft.public.xsl        back       


Replace attribute value   
Hi,
I would like to replace attribute values in an XML file if it matches 
another value.

Given this XML fragment :
<Users>
<User fistName = "Olivier" advancedViewing="VRAI"/>
<User fistName = "Olivier" advancedViewing="FAUX"/>
</Users>

It should become :
<Users>
<User fistName = "Olivier" advancedViewing="true"/>
<User fistName = "Olivier" advancedViewing="false"/>
</Users>

How can I do this ?
TIA.
date: Fri, 8 Feb 2008 12:27:02 +0100   author:   Olivier MATROT

Re: Replace attribute value   
Olivier MATROT wrote:

> I would like to replace attribute values in an XML file if it matches 
> another value.
> 
> Given this XML fragment :
> <Users>
> <User fistName = "Olivier" advancedViewing="VRAI"/>
> <User fistName = "Olivier" advancedViewing="FAUX"/>
> </Users>
> 
> It should become :
> <Users>
> <User fistName = "Olivier" advancedViewing="true"/>
> <User fistName = "Olivier" advancedViewing="false"/>
> </Users>
> 
> How can I do this ?

An XSLT stylesheet is one way
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

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

   <xsl:template match="User/@advancedViewing[. = 'VRAI']">
     <xsl:attribute name="{name()}">
        <xsl:text>true</xsl:text>
     </xsl:attribute>
   </xsl:template>

   <xsl:template match="User/@advancedViewing[. = 'FAUX']">
     <xsl:attribute name="{name()}">
        <xsl:text>false</xsl:text>
     </xsl:attribute>
   </xsl:template>

</xsl:stylesheet>

Both MSXML and the .NET framework support XSLT 1.0 transformations, with 
the .NET framework you use System.Xml.Xsl.XslCompiledTransform.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Fri, 08 Feb 2008 13:32:23 +0100   author:   Martin Honnen

Re: Replace attribute value   
Thanks for that,
Is there a way to lower case the attribute value before trying to match with 
the constant ?
If you know a good tutorial about XSLT I'm interresting.

TIA.

"Martin Honnen"  wrote in message 
news:O#JWq6kaIHA.5164@TK2MSFTNGP03.phx.gbl...
> Olivier MATROT wrote:
>
>> I would like to replace attribute values in an XML file if it matches 
>> another value.
>>
>> Given this XML fragment :
>> <Users>
>> <User fistName = "Olivier" advancedViewing="VRAI"/>
>> <User fistName = "Olivier" advancedViewing="FAUX"/>
>> </Users>
>>
>> It should become :
>> <Users>
>> <User fistName = "Olivier" advancedViewing="true"/>
>> <User fistName = "Olivier" advancedViewing="false"/>
>> </Users>
>>
>> How can I do this ?
>
> An XSLT stylesheet is one way
> <xsl:stylesheet
>   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>   version="1.0">
>
>   <xsl:template match="@* | node()">
>     <xsl:copy>
>       <xsl:apply-templates select="@* | node()"/>
>     </xsl:copy>
>   </xsl:template>
>
>   <xsl:template match="User/@advancedViewing[. = 'VRAI']">
>     <xsl:attribute name="{name()}">
>        <xsl:text>true</xsl:text>
>     </xsl:attribute>
>   </xsl:template>
>
>   <xsl:template match="User/@advancedViewing[. = 'FAUX']">
>     <xsl:attribute name="{name()}">
>        <xsl:text>false</xsl:text>
>     </xsl:attribute>
>   </xsl:template>
>
> </xsl:stylesheet>
>
> Both MSXML and the .NET framework support XSLT 1.0 transformations, with 
> the .NET framework you use System.Xml.Xsl.XslCompiledTransform.
>
>
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
date: Fri, 8 Feb 2008 14:08:15 +0100   author:   Olivier MATROT

Re: Replace attribute value   
Olivier MATROT wrote:

> Is there a way to lower case the attribute value before trying to match 
> with the constant ?

It is a bit complicated in XPath 1.0 but possible:

>>   <xsl:template match="User/@advancedViewing[. = 'VRAI']">

   <xsl:param name="ul"
     select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
   <xsl:param name="ll"
     select="'abcdefghijklmnopqrstuvwxyz'"/>

   <xsl:template match="User/@advancedViewing[translate(., $ul, $ll) = 
'vrai']">


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Fri, 08 Feb 2008 14:55:52 +0100   author:   Martin Honnen

Re: Replace attribute value   
There is an error on "xsl:template" (<xsl:template 
match="User/@advancedViewing[translate(., $ul, $ll) = 'vrai']">)
"Variables cannot be used within this expression"

"Martin Honnen"  wrote in message 
news:uMOUUplaIHA.1208@TK2MSFTNGP03.phx.gbl...
> Olivier MATROT wrote:
>
>> Is there a way to lower case the attribute value before trying to match 
>> with the constant ?
>
> It is a bit complicated in XPath 1.0 but possible:
>
>>>   <xsl:template match="User/@advancedViewing[. = 'VRAI']">
>
>   <xsl:param name="ul"
>     select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
>   <xsl:param name="ll"
>     select="'abcdefghijklmnopqrstuvwxyz'"/>
>
>   <xsl:template match="User/@advancedViewing[translate(., $ul, $ll) = 
> 'vrai']">
>
>
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
date: Fri, 8 Feb 2008 15:25:53 +0100   author:   Olivier MATROT

Re: Replace attribute value   
Olivier MATROT wrote:
> There is an error on "xsl:template" (<xsl:template 
> match="User/@advancedViewing[translate(., $ul, $ll) = 'vrai']">)
> "Variables cannot be used within this expression"

My bad, forgot about that issue with variables. In that case you have to 
spell out the string the variables refer to e.g.

   <xsl:template
     match="User/@advancedViewing[translate(., 
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'vrai']">

And the list of charactes (above A..Z) is just an example, in case you 
want to convert French strings with accents you need to add those to the 
second and third argument of the translate call.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Fri, 08 Feb 2008 16:02:10 +0100   author:   Martin Honnen

Re: Replace attribute value   
Thanks.
One last question : Could we use a function to translate ? This will 
eliminate the need to copy strings for each attribute I would like to 
process.

"Martin Honnen"  wrote in message 
news:uZ3rWOmaIHA.1376@TK2MSFTNGP02.phx.gbl...
> Olivier MATROT wrote:
>> There is an error on "xsl:template" (<xsl:template 
>> match="User/@advancedViewing[translate(., $ul, $ll) = 'vrai']">)
>> "Variables cannot be used within this expression"
>
> My bad, forgot about that issue with variables. In that case you have to 
> spell out the string the variables refer to e.g.
>
>   <xsl:template
>     match="User/@advancedViewing[translate(., 
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'vrai']">
>
> And the list of charactes (above A..Z) is just an example, in case you 
> want to convert French strings with accents you need to add those to the 
> second and third argument of the translate call.
>
>
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
date: Fri, 8 Feb 2008 16:20:16 +0100   author:   Olivier MATROT

Re: Replace attribute value   
Olivier MATROT wrote:

> One last question : Could we use a function to translate ? This will 
> eliminate the need to copy strings for each attribute I would like to 
> process.

I am not sure I understand what you are looking for. Standard XSLT 1.0 
does not allow you to define your own functions. All you can do is write 
named templates and call them with xsl:call-template.
With XSLT 2.0 you have much more power, first it allows you to use 
variables in match patterns (meaning my first suggestion would work), 
secondly it has functions 'upper-case' and 'lower-case' so you don't 
need the cumbersome 'translate' function, and furthermore you can define 
your own functions in stylesheets.
Microsoft does not have an XSLT 2.0 processor but there are other 
implementations around, one being Saxon (currently version 9) from 
<URL:http://saxon.sourceforge.net/> which has both a Java and a .NET 
version.


With XSLT 1.0 instead of doing e.g.

   <xsl:template match="User/@advancedViewing[translate(., 
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'vrai']">
     <xsl:attribute name="{name()}">
        <xsl:text>true</xsl:text>
     </xsl:attribute>
   </xsl:template>

   <xsl:template match="User/@advancedViewing[translate(., 
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'faux']">
     <xsl:attribute name="{name()}">
        <xsl:text>false</xsl:text>
     </xsl:attribute>
   </xsl:template>

you might prefer one template

   <xsl:param name="ul"
     select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
   <xsl:param name="ll"
     select="'abcdefghijklmnopqrstuvwxyz'"/>

   <xsl:template match="User/@advancedViewing">
     <xsl:variable name="lvalue" select="translate(., $ul, $ll)"/>
     <xsl:attribute name="{name()}">
       <xsl:choose>
         <xsl:when test="$lvalue = 'vraix'">
           <xsl:text>true</xsl:text>
         </xsl:when>
         <xsl:when test="$lvalue = 'faux'">
           <xsl:text>false</xsl:text>
         </xsl:when>
       </xsl:choose>
     </xsl:attribute>
   </xsl:template>

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Fri, 08 Feb 2008 16:55:06 +0100   author:   Martin Honnen

Re: Replace attribute value   
My fault, language abuse here.
You sould have read templates.
Thanks for your help that is very valuable.

"Martin Honnen"  wrote in message 
news:eNrV8rmaIHA.1532@TK2MSFTNGP04.phx.gbl...
> Olivier MATROT wrote:
>
>> One last question : Could we use a function to translate ? This will 
>> eliminate the need to copy strings for each attribute I would like to 
>> process.
>
> I am not sure I understand what you are looking for. Standard XSLT 1.0 
> does not allow you to define your own functions. All you can do is write 
> named templates and call them with xsl:call-template.
> With XSLT 2.0 you have much more power, first it allows you to use 
> variables in match patterns (meaning my first suggestion would work), 
> secondly it has functions 'upper-case' and 'lower-case' so you don't need 
> the cumbersome 'translate' function, and furthermore you can define your 
> own functions in stylesheets.
> Microsoft does not have an XSLT 2.0 processor but there are other 
> implementations around, one being Saxon (currently version 9) from 
> <URL:http://saxon.sourceforge.net/> which has both a Java and a .NET 
> version.
>
>
> With XSLT 1.0 instead of doing e.g.
>
>   <xsl:template match="User/@advancedViewing[translate(., 
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'vrai']">
>     <xsl:attribute name="{name()}">
>        <xsl:text>true</xsl:text>
>     </xsl:attribute>
>   </xsl:template>
>
>   <xsl:template match="User/@advancedViewing[translate(., 
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'faux']">
>     <xsl:attribute name="{name()}">
>        <xsl:text>false</xsl:text>
>     </xsl:attribute>
>   </xsl:template>
>
> you might prefer one template
>
>   <xsl:param name="ul"
>     select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
>   <xsl:param name="ll"
>     select="'abcdefghijklmnopqrstuvwxyz'"/>
>
>   <xsl:template match="User/@advancedViewing">
>     <xsl:variable name="lvalue" select="translate(., $ul, $ll)"/>
>     <xsl:attribute name="{name()}">
>       <xsl:choose>
>         <xsl:when test="$lvalue = 'vraix'">
>           <xsl:text>true</xsl:text>
>         </xsl:when>
>         <xsl:when test="$lvalue = 'faux'">
>           <xsl:text>false</xsl:text>
>         </xsl:when>
>       </xsl:choose>
>     </xsl:attribute>
>   </xsl:template>
>
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
date: Fri, 8 Feb 2008 17:03:25 +0100   author:   Olivier MATROT

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us