xsl-list
[Top] [All Lists]

RE: yet another XPath question

2003-01-30 09:00:41
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(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of S 
Woodside
Sent: quinta-feira, 30 de Janeiro de 2003 2:12
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] 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



<Prev in Thread] Current Thread [Next in Thread>