473,804 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

change some attribute values using xsl

Hello,

I am hoping this is a simple question with a straightforward solution.
I do not understand xsl much, so I apologize in advance if I am asking
a stupid question.

Is it possible to write an xsl that changes some attributes when
applied to an xml document, while preserving rest of it. Is it possible
to write this xsl independently of the structure of the xml document
itself, and only depend on the expression that identifies the attribute
and the value to assign to this attribute ?

For example, I am looking for the xsl document that will do the
following when applies to any xml document:

1. Replace the value of attribute password anywhere it appears in the
xml document with the string '***'
2. Replace the value of attribute password in the element User as above
3. Replace the value of attribute password by prepending and appending
the string 'AAA' to it.

I will really appreciate if someone can provide a sample for this.
Thanks in advance,

- hemal

Dec 13 '06 #1
4 21615

Hemal Pandya wrote:
Is it possible to write an xsl that changes some
attributes when applied to an xml document, while
preserving rest of it.
There's a very basic (and absolutely essential) technique
called 'identity transformation' . The following template
matches and copies all the nodes and attributes
recursively:

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

If you need to exclude some nodes from the identity
transformation, you create exclusion templates.
Is it possible to write this xsl independently of the
structure of the xml document itself,
Of course not, you must possess some knowledge about the
structure of the document in question to do anything
non-trivial with it.
and only depend on the expression that identifies the
attribute and the value to assign to this attribute ?
That *is* knowledge about the structure of the document.
1. Replace the value of attribute password anywhere it
appears in the xml document with the string '***'
<xsl:template match="@passwor d">
<xsl:attribut e name="password" >
<xsl:text>*** </xsl:text>
</xsl:attribute>
</xsl:template>
2. Replace the value of attribute password in the element
User as above
<xsl:template match="@passwor d[parent::User]">
<xsl:attribut e name="password" >
<xsl:text>*** </xsl:text>
</xsl:attribute>
</xsl:template>
3. Replace the value of attribute password by prepending
and appending the string 'AAA' to it.
<xsl:template match="@passwor d[parent::User]">
<xsl:attribut e name="password" >
<xsl:value-of select="concat( 'AAA',.,'AAA')"/>
</xsl:attribute>
</xsl:template>

I would strongly recommend reading a decent XPath/XSLT
tutorial. You'll learn everything about stuff like this,
and more.

--
Pavel Lepin

Dec 13 '06 #2
Thanks for your helpful response Pavel.

Pavel Lepin wrote:
Hemal Pandya wrote:
Is it possible to write an xsl that changes some
attributes when applied to an xml document, while
preserving rest of it.

There's a very basic (and absolutely essential) technique
called 'identity transformation' .
I googled for 'xsl identity transformation' and came across
http://www.dpawson.co.uk/xsl/sect2/i....html#d5687e43. After
reading that I think I understand most of what you suggest.

[....]
I would strongly recommend reading a decent XPath/XSLT
tutorial. You'll learn everything about stuff like this,
and more.
The reason I am interested in this is to change one of the files in the
project that comes from version control. So far I have been changing it
by hand but realized I can perhaps use xsl. I know I aught to read up
but I was hoping the problem is simple enough that I can use some magic
words.

Unfortunately, I realized too late that the example I gave is not quite
similar to the document I am working with. The document I am working
with has the following structure:

<group name="user">
<property name="password" value="none"/>
</group>

Now, I need to modify the value attribute or all property elements
contained in group user that have name=password. I could go as far as
the following for my exclusion template:

<xsl:template match="//group[@name='user']/property[@name='password ']">

But I am not able to figure out what goes inside. I am thinking
whatever I put inside will replace the entire property element; what I
need is preserve all of the element except for the value attribute. I
can imagine that it will again be the same principle of identity
conversion with exclusion but can't quite figure it out with my
seat-of-pants approach.

Can you please explain how I would do that?

Thanks,

- hemal
>
--
Pavel Lepin
Dec 13 '06 #3

Hemal Pandya wrote:
Pavel Lepin wrote:
Hemal Pandya wrote:
Is it possible to write an xsl that changes some
attributes when applied to an xml document, while
preserving rest of it.
There's a very basic (and absolutely essential)
technique called 'identity transformation' .

I googled for 'xsl identity transformation' and came
across
http://www.dpawson.co.uk/xsl/sect2/i....html#d5687e43
After reading that I think I understand most of what you
suggest.
By the way, that's a part of an *excellent* (if somewhat
poorly organized) XSL FAQ. Most of it is probably
irrelevant to your present woes, but it's definitely worth
reading if you intend to start studying XSLT seriously.
I would strongly recommend reading a decent XPath/XSLT
tutorial. You'll learn everything about stuff like
this, and more.

I know I aught to read up but I was hoping the problem is
simple enough that I can use some magic words.
You're probably not interested in my preachings, but I can
tell you from painful personal experiences that it's always
best to understand the magic you're invoking (if you can
afford it time- and cost-wise).
<group name="user">
<property name="password" value="none"/>
</group>

<xsl:template
match="//group[@name='user']/property[@name='password ']">
You're almost there. Indeed, it is workable as is, but
that's a somewhat inelegant solution. Since you need to
change just the value attribute, *match* just the value
attribute:

<xsl:template
match=
"
group[@name='user']/property[@name='password ']/@value
">
<xsl:attribut e name="value">
<xsl:value-of select="concat( 'AAA',.,'AAA')"/>
</xsl:attribute>
</xsl:template>

--
Pavel Lepin

Dec 13 '06 #4
Pavel Lepin wrote:
[....]

Thanks a ton Pavel. I got it working. I do realize I should study it if
I want to do anything serious, but with your kind help I am in good
shape for now.

Best regards.

- hemal

Dec 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2897
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY> </REGION> <REGION Name="NorthAmerica" WingDing="Yellow"> <COUNTRY Name="Canada"> <TOWN Name="Quebec" WingDing="Brown" />
4
2936
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are defined in a different namespace from the element.
3
2525
by: Dimitris \(GIS\) | last post by:
Hi I am a JavaScript rookie. I have an applet and want to change it's parameters with multiple links in my page. How can I do it? this is my code: <applet code="GIS.class"> <param id="moneyID" name="money" value="100"> <param id="vicimsID" name="vicims" value="2"> <param id="countryID" name="country" value="UK">
2
3959
by: Bill Cohagan | last post by:
In my app I'm validating an XML file against an XSD which contains several attribute default value specifications. I'm performing the validation via an xml document load() using a ValidatingXMLReader. After loading the document (and validating it) I assume that the resulting doc will (as appropriate) have elements with attributes set to their default values in those cases where there was nothing specified in the input XML file. However,...
0
1832
by: beanweed | last post by:
BACKGROUND ---------- I have an ASP.NET application having two panels. In one panel, an XML document, transformed using xsl, is displayed. In the other panel are some controls that allow a user to change the xml. For example, each "l_item" element appears as a row in a table labelled with a "label"; so if I have <l_item id="1"> <label>blah</label> ...
10
42971
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me, the XML specification seems a little ambiguous on this, so I defer to the XML authorities. Refer to sections 2.4 and 2.7 (it all hinges on if CDATA attribute values are part of markup or not.) Thanks.
2
10062
by: Philipp | last post by:
Hi, I wrote a small c# programm, which generates an xml-string by serializing a class and sends it to a server. Now I want to change some values in that xml-string. The only way for me to do that might be, to deserialize it, change the particular attribute and serialize it again, to finally save it on my harddisk. But I believe, that there is a more convenient way to do this, since it is also possible to append an element...
5
2876
by: Soledad Vel | last post by:
Hi All, i write this code: var sliderwidth=100; var sliderheight = 100; var div1 = document.createElement('div'); div1.setAttribute('id','d5'); div1.setAttribute('style', 'background-color:yellow;position:relative;width:'+sliderwidth+'px;heigh
8
1888
by: patrizio.trinchini | last post by:
Hi All, I'would like to write an XSL transformation that changes the value of the atribute of a given element according to the value of another atttribute of the same element. For instance, given the following XML file: <?xml version="1.0" encoding="UTF-8"?> <sample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.