Is it possible to modify a source node using XSLT?

Discussion in 'XML' started by titanandrews@hotmail.com, May 5, 2005.

  1. Guest

    Hi,

    I have ran into a situation that I think should be possible, but I am
    fairly new to XSLT so maybe not.

    Suppose I have the following document
    <ROOT>
    <FOO name="A">
    <CHILD name="B"/>
    </FOO>
    </ROOT>

    I am creating a new XML doc using XSLT, and I change the name attribute
    of the first node FOO in my target doc. The name attribute of CHILD is
    dependent on it's parent, i.e. it's built from the parent name. So I
    select the parent name with XPATH "../@Name", but it returns the name
    from the source document. I need the name from the target document,
    because the old name is no good. How can I do this? Is it possible to
    modify a source node in XSLT? I can modify a DOM document in a program
    easily, so it seems like it would be possible to do in XSLT. If I can
    do this, then the task will be easy. Are there any other ways to
    achieve this?


    Many thanks for the help!


    regards,

    B
     
    , May 5, 2005
    #1
    1. Advertising

  2. "" <> writes:

    > Hi,
    >
    > I have ran into a situation that I think should be possible, but I am
    > fairly new to XSLT so maybe not.
    >
    > Suppose I have the following document
    > <ROOT>
    > <FOO name="A">
    > <CHILD name="B"/>
    > </FOO>
    > </ROOT>
    >
    > I am creating a new XML doc using XSLT, and I change the name attribute
    > of the first node FOO in my target doc. The name attribute of CHILD is
    > dependent on it's parent, i.e. it's built from the parent name. So I
    > select the parent name with XPATH "../@Name", but it returns the name
    > from the source document. I need the name from the target document,
    > because the old name is no good. How can I do this? Is it possible to
    > modify a source node in XSLT? I can modify a DOM document in a program
    > easily, so it seems like it would be possible to do in XSLT. If I can
    > do this, then the task will be easy. Are there any other ways to
    > achieve this?
    >
    >
    > Many thanks for the help!
    >
    >
    > regards,
    >
    > B


    XSLT input tree is read only, however as far as I understand your
    description you don't want to modify the source just generate a new
    element with some name. You haven't given any indication of what element
    name you need to generate. Also it needs to be @name not @Name, XML is
    case sensitive.

    What result document do you need to generate given the above input??

    David
     
    David Carlisle, May 5, 2005
    #2
    1. Advertising

  3. Hi,

    So the source is read only? That is good to know I suppose, although
    it seems odd to me. Anyway, let me try to explain a little more. My
    target document might look like this:

    <TARGET>
    <FI name="Changed">
    <CHILD_FI name="Changed_1"/>
    </FI>
    </TARGET>

    Notice that CHILD_FI name is based on FI. (with _1 appended) But I
    cannot do this, because when I select the name from the parent, I get
    the old name which is A. So instead my new name would end up being A_1,
    which is not what I want.
    Make sense?

    many thanks,

    B

    David Carlisle wrote:
    > "" <> writes:
    >
    >
    >>Hi,
    >>
    >> I have ran into a situation that I think should be possible, but I am
    >>fairly new to XSLT so maybe not.
    >>
    >>Suppose I have the following document
    >><ROOT>
    >> <FOO name="A">
    >> <CHILD name="B"/>
    >> </FOO>
    >></ROOT>
    >>
    >>I am creating a new XML doc using XSLT, and I change the name attribute
    >>of the first node FOO in my target doc. The name attribute of CHILD is
    >>dependent on it's parent, i.e. it's built from the parent name. So I
    >>select the parent name with XPATH "../@Name", but it returns the name
    >>from the source document. I need the name from the target document,
    >>because the old name is no good. How can I do this? Is it possible to
    >>modify a source node in XSLT? I can modify a DOM document in a program
    >>easily, so it seems like it would be possible to do in XSLT. If I can
    >>do this, then the task will be easy. Are there any other ways to
    >>achieve this?
    >>
    >>
    >>Many thanks for the help!
    >>
    >>
    >>regards,
    >>
    >>B

    >
    >
    > XSLT input tree is read only, however as far as I understand your
    > description you don't want to modify the source just generate a new
    > element with some name. You haven't given any indication of what element
    > name you need to generate. Also it needs to be @name not @Name, XML is
    > case sensitive.
    >
    > What result document do you need to generate given the above input??
    >
    > David
    >
     
    Barry Andrews, May 6, 2005
    #3
  4. Barry Andrews <> writes:

    > Hi,
    >
    > So the source is read only? That is good to know I suppose, although
    > it seems odd to me. Anyway,


    It would be very hard to define any behaviour to an xslt transformation
    if the input could change during a transformation. There is no
    requirement on a processor to process any templates in any particular
    order, only that the results are combined into the final result tree as
    specified, so if templates could alter the input, the behaviour of every
    stylesheet would be undefined, as the input to each template would
    depend on implementation-specific choices of the order of template
    execution.

    This is the usual behaviour of any declarative/functional language.
    If you evaluate a function f(x,y) a result is returned but there are no
    side effects, in particular the inputs to a unction are never changed.


    > let me try to explain a little more. My
    > target document might look like this:
    >
    > <TARGET>
    > <FI name="Changed">
    > <CHILD_FI name="Changed_1"/>
    > </FI>
    > </TARGET>
    >
    > Notice that CHILD_FI name is based on FI. (with _1 appended) But I
    > cannot do this, because when I select the name from the parent, I get
    > the old name which is A. So instead my new name would end up being A_1,
    > which is not what I want.
    > Make sense?
    >

    In a typical transformation the names of the result elements bear no
    relation at all to the names of the input elements. Think of converting
    docbook to html, or html to SVG or ... When generating the name of any
    element in the result you have access via xpath to any part of the
    source, so any information that caused you to generate the name FI on
    the parent element is available still so there is nothing to stop you
    generating an element name that contains the string _FI. However you
    haven't given any indication of the transformation that you are trying
    to so I can't offer any code.

    David
     
    David Carlisle, May 6, 2005
    #4
  5. Thanks for the response!

    You have answered my question about modifying the source very well.

    I will try to post my XSLT code tomorrow when I get to work. It will
    probably help explain what I am trying to do.


    thanks,

    B



    David Carlisle wrote:
    > Barry Andrews <> writes:
    >
    >
    >>Hi,
    >>
    >> So the source is read only? That is good to know I suppose, although
    >>it seems odd to me. Anyway,

    >
    >
    > It would be very hard to define any behaviour to an xslt transformation
    > if the input could change during a transformation. There is no
    > requirement on a processor to process any templates in any particular
    > order, only that the results are combined into the final result tree as
    > specified, so if templates could alter the input, the behaviour of every
    > stylesheet would be undefined, as the input to each template would
    > depend on implementation-specific choices of the order of template
    > execution.
    >
    > This is the usual behaviour of any declarative/functional language.
    > If you evaluate a function f(x,y) a result is returned but there are no
    > side effects, in particular the inputs to a unction are never changed.
    >
    >
    >
    >>let me try to explain a little more. My
    >>target document might look like this:
    >>
    >><TARGET>
    >> <FI name="Changed">
    >> <CHILD_FI name="Changed_1"/>
    >> </FI>
    >></TARGET>
    >>
    >>Notice that CHILD_FI name is based on FI. (with _1 appended) But I
    >>cannot do this, because when I select the name from the parent, I get
    >>the old name which is A. So instead my new name would end up being A_1,
    >>which is not what I want.
    >>Make sense?
    >>

    >
    > In a typical transformation the names of the result elements bear no
    > relation at all to the names of the input elements. Think of converting
    > docbook to html, or html to SVG or ... When generating the name of any
    > element in the result you have access via xpath to any part of the
    > source, so any information that caused you to generate the name FI on
    > the parent element is available still so there is nothing to stop you
    > generating an element name that contains the string _FI. However you
    > haven't given any indication of the transformation that you are trying
    > to so I can't offer any code.
    >
    > David
    >
     
    Barry Andrews, May 6, 2005
    #5
    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. Replies:
    0
    Views:
    1,650
  2. Ralf Wahner
    Replies:
    2
    Views:
    2,467
    Ralf Wahner
    Jul 15, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,508
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    3
    Views:
    3,934
    A. Bolmarcich
    Oct 20, 2006
  5. njsimha
    Replies:
    0
    Views:
    798
    njsimha
    Sep 16, 2008
Loading...

Share This Page