0

XML

 <root>
        <Algemeen>
             <foto>
                 <foe>
                     <fee>
                         <img src="www.blah.com/sample.jif"></img>
                     </fee>
                 </foe>
              </foto>
         </Algemeen>
    </root>

XSLT

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
            <result>
        <xsl:apply-templates select="/root/Algemeen/foto/foe/fee/img"/>
            </result>
        </xsl:template>

    <!--specific template match for this img -->
        <xsl:template match="/root/Algemeen/foto/foe/fee/img">
          <xsl:copy>
                <xsl:attribute name="width">100</xsl:attribute>
                <xsl:apply-templates select="@*|node()" />
              </xsl:copy>
        </xsl:template>

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

    </xsl:stylesheet>

I'm adding an attribute to "img" tag via template, how can i get the whole "foto" node? is this "@*|node()" refers to 2nd level parent node "foe"?

viewed links:

Community
  • 1
  • 1
iCeR
  • 139
  • 3
  • 15
  • So, what is the XML document and exactly what result do you want to get? Please, *edit* the question and add this important, missing information. – Dimitre Novatchev Dec 07 '12 at 06:27

1 Answers1

0

is this "@*|node()" refers to 2nd level parent node "foe"?

Nope! This refers to child nodes and attributes ..

How to copy <img> adding an attribute, along with its grand-parent <foto>??

In your code you are matching the root node by saying <xsl:template match="/"> and you are renaming it as <result>. Under that you are saying <xsl:apply-templates select="/root/Algemeen/foto/foe/fee/img"/> .. So that will skip the hierarchy and do what <xsl:template match="/root/Algemeen/foto/foe/fee/img"> says ..

Your <xsl:template match="/root/Algemeen/foto/foe/fee/img"> template looks perfect!! All you need is below correction!

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <result>
      <xsl:apply-templates select="/root/Algemeen/foto"/>
    </result>
  </xsl:template>

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

  <!--specific template match for this img -->
  <xsl:template match="/root/Algemeen/foto/foe/fee/img">
    <xsl:copy>
      <xsl:attribute name="width">100</xsl:attribute>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

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

</xsl:stylesheet>

If you want to access the parent or ancestor try this:

<xsl:for-each select="ancestor::foto">
Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
  • Thank you. It works :) . My last approach is inversely proportion to this one I cant find a way to get first the child then get the parent after. – iCeR Dec 07 '12 at 09:13
  • @iCeR, Welcome! well. The reverse XPath method, that you are mentioning is not encouraged! What I have provided is a better approach! – Rookie Programmer Aravind Dec 07 '12 at 09:22
  • I have provided the way to access ancestor from current node.. But in your case it is not required .. neither should you do it.. let me know if you need anymore help in understanding. – Rookie Programmer Aravind Dec 07 '12 at 09:33
  • for future reference :), sounds clear to me. thanks for the help :) – iCeR Dec 07 '12 at 10:41
  • @iCeR, glad to help.. If you are satisfied with the answer then click on a tick mark at the left-top corner of my answer.. so that, it will be accepted as answer to this question and will finish the Question and Answer session! – Rookie Programmer Aravind Dec 07 '12 at 11:01