change some attribute values using xsl

Discussion in 'XML' started by Hemal Pandya, Dec 13, 2006.

  1. Hemal  Pandya

    Hemal Pandya Guest

    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
     
    Hemal Pandya, Dec 13, 2006
    #1
    1. Advertising

  2. Hemal  Pandya

    Guest

    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="@password">
    <xsl:attribute 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="@password[parent::User]">
    <xsl:attribute 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="@password[parent::User]">
    <xsl:attribute 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, 2006
    #2
    1. Advertising

  3. Hemal  Pandya

    Hemal Pandya Guest

    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/identity.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
     
    Hemal Pandya, Dec 13, 2006
    #3
  4. Hemal  Pandya

    Guest

    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/identity.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:attribute name="value">
    <xsl:value-of select="concat('AAA',.,'AAA')"/>
    </xsl:attribute>
    </xsl:template>

    --
    Pavel Lepin
     
    , Dec 13, 2006
    #4
  5. Hemal  Pandya

    Hemal Pandya Guest

    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
     
    Hemal Pandya, Dec 14, 2006
    #5
  6. Hemal  Pandya

    elizas

    Joined:
    Jan 27, 2010
    Messages:
    27
    Location:
    India
    How to Create a Tablular Structure of a XML File Using XSL

    Sometimes we need to display the data that are present in a xml file in a tabular format. Following code explan how to display a tabular format of the XML file by applying XSL stylesheet.
    XML-FILE
    <?xml version="1.0" encoding="utf-8" ?>
    <?xml-stylesheet type="text/xsl" href="showtableformat.xsl"?>
    <StudentInfo>
    <Students>
    <FirstName>Monali</FirstName>
    <LastName> Nayak </LastName>
    </Students>
    <Students>
    <FirstName>Sarita</FirstName>
    <LastName> Dash </LastName>
    </Students>
    <Students>
    <FirstName>Asrita</FirstName>
    <LastName> Pandit </LastName>
    </Students>
    </StudentInfo>
     
    elizas, May 3, 2010
    #6
  7. Hemal  Pandya

    aljar

    Joined:
    May 15, 2010
    Messages:
    5
    Hi Pavel,

    I may have some bug but the only way I could answer the question of Hemal is like this:

    Code:
    <xsl:template match="/group[@name='user']">
    		<xsl:value-of select="concat('AAA', property/@value,'AAA')" />
    </xsl:template>
    I tried to run your suggestion
    Code:
    <xsl:template
    match="group[@name='user']/property[@name='password']/@value">
    <xsl:attribute name="value">
    <xsl:value-of select="concat('AAA',.,'AAA')"/>
    </xsl:attribute>
    </xsl:template>
    I got an empty result. Any clue?

    Thank you
     
    aljar, May 16, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 5tein
    Replies:
    1
    Views:
    473
    Patrick TJ McPhee
    Apr 30, 2004
  2. Replies:
    1
    Views:
    3,611
    A. Bolmarcich
    May 27, 2005
  3. Kourosh
    Replies:
    6
    Views:
    855
    Peter Flynn
    May 17, 2006
  4. Replies:
    8
    Views:
    523
  5. Replies:
    3
    Views:
    1,548
    Brandon McCombs
    May 2, 2007
Loading...

Share This Page