xsl-list
[Top] [All Lists]

copying with substitution of text content (WAS: test for element or attribute AND superfluous namespace declarations)

2002-12-11 12:04:26
Hi

New question is at the end!

_WAS: Test for current node being an element or attribute?_

Michael Kay wrote:
>>I'm using a copy template (recursively) that matches
>>"@*|node()" and would like to decide what to do
>>(<xsl:choose>) depending on the text content of the current
>>node (nodes with arbitrary names, so no fix testing possible!).
>
> It would be better to have separate template rules for elements and
> attributes, rather than using one rule that then does an <xsl:choose>.

OK, I have to admit that the original design of my stylesheet was
rather lousy and that testing a node of being an attribute or element
can be avoided easily in my case.

Here comes an example:

*1st INPUT*
===========
<INSERT into="kennungen">
  <FIELDS>kennung, path, lastarchiv, groesse,
  belegt, nutzung, voll, bereich, status, loeschbar,
  fkoerbe, dokutypkz, sektorsize, dokformat
  </FIELDS>
  <VALUES>:1,:2,:3,:4,:5,:6,:7,:8,
  :9,:10,:11,:12,:13,:14</VALUES>
</INSERT>

(Hint: "<FIELDS>" is tokenized with a "exslt" function and binding
number can be retrieved by a template called "pick.key-binding")

*2nd INPUT*
===========
<Kennungen name="[kennung]"
    area="[bereich]"
    basketFlag="[fkoerbe]"
    docTypeMark="[dokutypkz]"
    docFormat="[dokformat]"
    erasable="[loeschbar]">
  <Path>[path]</Path>
  <Archive>[lastarchiv]</Archive>
  <Status>[status]</Status>
  <CapacityInfo capacity="[groesse]"
      occupied="[belegt]"
      sectorSize="[sektorsize]"
      inUse="[nutzung]"
      complete="[voll]"/>
</Kennungen>

*3 OUTPUT* this should be the result
==========================
 <Kennungen name=":1"
     area=":8"
     basketFlag=":11"
     docTypeMark=":12"
     docFormat=":14"
     erasable=":10">
   <Path>:2</Path>
   <Archive>:3</Archive>
   <Status>:9</Status>
   <CapacityInfo capacity=":4"
       occupied=":5"
       sectorSize=":13"
       inUse=":6"
       complete=":7"/>
 </Kennungen>


With the following fragment of stylesheet, I get nearly a perfect
result, except that my text nodes (of elements) still keep the
old content

<xsl:template match="node()" mode="copy.template" >
  <xsl:copy>
    <xsl:for-each select="@*">
      <xsl:attribute name="{name()}">
        <xsl:variable name="attr.content">
          <xsl:value-of select="substring(.,2,string-length(.)-2)"/>
        </xsl:variable>
        <xsl:call-template name="pick.key-binding">
          <xsl:with-param name="key" select="$attr.content"/>
        </xsl:call-template>
     </xsl:attribute>
    </xsl:for-each>
    <xsl:variable name="element.content">
      <xsl:value-of select="substring(text(),2,string-length(text())-2)"/>
    </xsl:variable>
      <xsl:call-template name="pick.key-binding">
        <xsl:with-param name="key" select="$element.content"/>
      </xsl:call-template>
    <xsl:apply-templates mode="copy.template"/>
  </xsl:copy>
</xsl:template>

With this approach, I no more need to test for attributes or
elements, but there remains a problem with copying elements:

*NEW QUESTION*
==============

_WAS: superfluous namespace declarations with xsl:element_
Michael Kay wrote:
>>I have to manipulate the text content of these nodes (for the ones,
>>that have "[]" around themselves, I use a separate lookup table,
>>replacing the actual [value] by the one I found).
>
> I don't understand the detail of what you want to do, but
>
> <xsl:copy>
>   ... some instructions here ...
> </xsl:copy>
>
> is more-or-less equivalent to:
>
> <xsl:element name="{name()}">
>   ... some instructions here ...
> </xsl:element>
>
> The only differences are in the way that namespaces are handled, which
> probably won't affect you.

David Carlisle wrote:
> yes, you can still use xsl:copy.
>
>>   I don't think this is possible with xsl:copy - or am I wrong?
>
>   no - yes :-)

After David Carlisle and Michael Key encouraged me both to use
<xsl:copy>, my question remains mostly the same: how can I avoid with
<xsl:copy> that the old text content shows up in my elements (like
<Path>:2[path]</Path>)? Is there some built-in template, that should
be overwritten? Or have I to fallback on <xsl:element> in the end
again.

Some last advice should help ;-)
Georges


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



<Prev in Thread] Current Thread [Next in Thread>
  • copying with substitution of text content (WAS: test for element or attribute AND superfluous namespace declarations), Georges Schmitz <=