Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 13th, 2010, 09:58 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default creating nested nodes

I am trying to create such a structure
Code:
 <be:part id="AAAAB">
      <be:foretak>
         <be:generelleopplysinger>
                <be:navn>Ehsan Sadeghi</be:navn>
                 <be:adresse>
                   <be:postboks>111</be:postboks>
                </be:adresse>
         </be:generelleopplysinger>      
      </be:foretak>
       <be:organisasjon />
    </be:part>
from
Code:
   <be:part id="AAAAB">
      <be:foretak>
        <be:navn>ehsansad</be:navn>
         <be:adresse>
              <be:postboks>111</be:postboks>
         </be:adresse>
      </be:foretak>
        <be:organisasjon />
    </be:part>
I have tried read the part and then apply templates for foretak , navn...., but this does not work so I had to create this ugly code
Code:
 <xsl:template match="be:part">
    <xsl:comment>part</xsl:comment>
    <xsl:text>&#xa;</xsl:text>
    <xsl:element name="be:part">
      <xsl:attribute name="id">
        <xsl:value-of select="@id"/>        
      </xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>        
  </xsl:template>

  <xsl:template match="be:part/be:foretak">
    <xsl:element name="be:foretak">
      <xsl:element name="be:generelleopplysinger">
        <xsl:element name="be:navn">
          <xsl:apply-templates select="be:navn"/>
        </xsl:element>
      </xsl:element>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template name="be:navn">    
      <xsl:value-of select="."/>
  </xsl:template>
1- if I use the full path in the <xsl:template name="be:navn"> such
<xsl:template name="be:part/be:foretak/be:navn">
I will get an error for the name which says it is not a correct QName, why don't understand.

2- why can't I just create each node in its own template as such

Code:
 <xsl:template name="be:navn">
    <xsl:element name="be:navn">     
      <xsl:value-of select="."/>
    </xsl:element> 
  </xsl:template>
if I do this the nodes will not be created and I will only get the value of the node.

why can't I get what I'm asking for?

cheers
es
 
Old October 13th, 2010, 10:05 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

For all such tasks start with the template
Code:
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
then you only need to add templates for those elements you want to manipulate, in your case you need
Code:
<xsl:template match="be:generelleopplysinger">
  <xsl:apply-templates/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 13th, 2010, 10:10 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Sorry, I misread your post, my code would work if you wanted a reverse transformation of what you posted.
In your case you need
Code:
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="be:foretak">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <be:generelleopplysinger>
      <xsl:apply-templates/>
    </be:generelleopplysinger>
  </xsl:copy>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 14th, 2010, 04:57 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

Thanks Martin for both codes,but this is not working for me. when I create a
Code:
<xsl:template name="be:navn">
    <xsl:element name="be:navn">     
      <xsl:value-of select="."/>
    </xsl:element> 
  </xsl:template>
I still don't get the <be:navn> element. This is really strange since the my other templates are working correctly excpt the template for the <be:part> element
 
Old October 14th, 2010, 06:09 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

To give you a complete example, with the input
Code:
<be:part id="AAAAB" xmlns:be="http://example.com/">
    <be:foretak>
      <be:navn>ehsansad</be:navn>
       <be:adresse>
            <be:postboks>111</be:postboks>
       </be:adresse>
    </be:foretak>
    <be:organisasjon />
</be:part>
and the stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:be="http://example.com/">
  
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="be:foretak">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <be:generelleopplysinger>
      <xsl:apply-templates/>
    </be:generelleopplysinger>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Saxon 6.5.5 outputs
Code:
<be:part xmlns:be="http://example.com/" id="AAAAB">
   <be:foretak>
      <be:generelleopplysinger>
         <be:navn>ehsansad</be:navn>
         <be:adresse>
            <be:postboks>111</be:postboks>
         </be:adresse>
      </be:generelleopplysinger>
   </be:foretak>
   <be:organisasjon/>
</be:part>
So the be:generelleopplysinger has been added. Is that not what you want?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 14th, 2010, 06:20 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

well based on the example I gave Yes this does the job. The thing is that the example is a small section of the actual XML file. But I did get an idea to solve the problem. But I was wondering if you could help me understand something. In the original question I have a template by the name of be:navn

Code:
<xsl:template name="be:navn">
    <xsl:element name="be:navn">     
      <xsl:value-of select="."/>
    </xsl:element> 
  </xsl:template>
if I change the name to
Code:
<xsl:template name="be:part/be:foretak/be:navn">
I will get this error from VS 2010:
Error 284 The '/' character, hexadecimal value 0x2F, cannot be included in a name.

Do you know why I'm getting this?

ehsan
 
Old October 14th, 2010, 06:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

An xsl: template element allows the attributes "name" and "match". "name" needs to be a qualified name, "match" a pattern. So you can use
Code:
<xsl;template match="be:part/be:foretak/be:navn">
as "be:part/be:foretak/be:navn" is a pattern. It is not a qualified name however so you cannot use name="be:part/be:foretak/be:navn"
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
ehsansad (October 14th, 2010)
 
Old October 14th, 2010, 06:56 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

I trully think this is a common mistake between xslt novice programmers:-) thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
nested for-each of different nodes kokoness XSLT 5 August 25th, 2009 09:22 AM
How to get the value of last 3 nested nodes using xsl:for-each rabs XSLT 3 February 26th, 2009 10:17 AM
How to get the value of first 3 nested nodes using xsl:for-each eruditionist XSLT 5 February 25th, 2009 04:18 PM
How to value in b/n nodes Swetha XSLT 1 May 20th, 2008 12:20 PM
Nested If pans Access 4 December 10th, 2004 12:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.