[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: yet another XPath question

Subject: RE: yet another XPath question
From: Américo Albuquerque <aalbuquerque@xxxxxxxxxxxxxxxx>
Date: Thu, 30 Jan 2003 16:00:41 -0000
xpath no child
Hi Simon.

I code this little template to do what you want, it doesn't use xpath
but an indentity template with a recursive template.
Hope this helps you.

 <xsl:param name="path" select="'/resume/header/address/country'"/>
 <xsl:param name="value" select="'XLandia'"/>
 
 <xsl:template match="*">
 <xsl:variable name="localpath">
 <xsl:for-each select="ancestor-or-self::*">
  <xsl:text>/</xsl:text>
  <xsl:value-of select="name()"/>
 </xsl:for-each>
 </xsl:variable>
 <xsl:variable name="child"
select="substring-before(substring($path,string-length($localpath)+2),'/
')"/>
 <xsl:copy>
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates/>
  <!--
  if $child isn't realy a child $localpath isn't the start of $path then
...
  This test could be improved. It make sure that we are on the right
path and there is no child with that name.
  If the last name ('country' in your example) already exists it doesn't
replace it, just adds another one with the same name and with the new
value.
  -->
  <xsl:if test="not(*[name()=$child]) and
$localpath=substring($path,1,string-length($localpath))">
   <xsl:call-template name="create">
    <xsl:with-param name="path"
select="substring($path,string-length($localpath)+2)"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:copy>
 </xsl:template>
 
 <xsl:template name="create">
  <xsl:param name="path"/>
  <xsl:variable name="step"
select="substring-before(concat($path,'/'),'/')"/>
  <xsl:variable name="next" select="substring-after($path,'/')"/>
  <xsl:choose>
   <xsl:when test="not($step) and $next">
   <xsl:call-template name="create">
    <xsl:with-param name="path" select="$next"/>
   </xsl:call-template>
   </xsl:when>
   <xsl:when test="$step and not($next)">
   <xsl:element name="{$step}">
    <xsl:value-of select="$value"/>
   </xsl:element>
   </xsl:when>
   <xsl:when test="$step and $next">
   <xsl:element name="{$step}">
    <xsl:call-template name="create">
     <xsl:with-param name="path" select="$next"/>
    </xsl:call-template>
   </xsl:element>
   </xsl:when>
  </xsl:choose>
 </xsl:template>


-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of S Woodside
Sent: quinta-feira, 30 de Janeiro de 2003 2:12
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re:  yet another XPath question


So I guess there's no easy way out, eh? I'm sure I can code something 
up to do this...

simon

On Tuesday, January 28, 2003, at 01:40  PM, S Woodside wrote:

> OK, that's easy, I just test="/Big/Long/XPath" but wait, if it's /not/
> present then I want to create sufficient nodes to insert the new 
> element in that place. That means I want to test each location step in

> the XPath, one by one, from root to leaf, to see if it's there. If it 
> isn't, I then create a new node for that location step, and then 
> descend the rest of the XPath, creating nodes until I'm done and then 
> I insert the value. Here's an example of what I want to have happen:
---
www.simonwoodside.com


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.