xsl-list
[Top] [All Lists]

RE: Problem with grouping the handling of sibling nodes

2005-03-08 10:09:46
I try to convert some special tags into some more regular tags.
I have something like <starttag/>sometext<endtag/>, but starttag and 
endtag are tags without content, the content is the sometext.

So I have an example xml and a suggestion for an xsl, but it 
does not work.

The example xsl runs mostly within XMLSpy but I have to use 
Saxon8 in my application, and that gave me an exception.

What kind of exception? There's no value in reporting errors on this list
unless you're specific about the error message. The stylesheet below gives
no exceptions.

I try to stripp the xsl until it runs with saxon, but than I 
lost almost 
of my functionality.

So If someone could give me an advice how to do this with 
saxon or what 
else to do.
I I would be very grateful if someone could help me.

I think it would be useful to simplify the problem. It's difficult to guess
at what you're trying to do by studying code that doesn't work.

I think I would tackle this (even in 2.0) with an apply-templates sibling
traversal, along the lines:

<xsl:template match="*" mode="group-start-and-end">
  <xsl:apply-templates select="child::node()[1]" mode="traverse"/>
</xsl:template>

<xsl:template match="start" mode="traverse">
  <span><xsl:apply-templates select="following-sibling::node()[1]"
mode="traverse"/></span>
  <xsl:apply-templates
select="following-sibling::end/following-sibling::node()[1]"
                       mode="traverse"/>
</xsl:template>

<xsl:template match="end" mode="traverse"/>

<xsl:template match="node()" mode="traverse">
  <xsl:copy-of select="."/>
  <xsl:apply-templates select="following-sibling::node()[1]"
mode="traverse"/>
</xsl:template>

Don't try to do anything else in the same transformation pass, keep that for
a second pass over the data once you've sorted the hierarchic structure out.

Michael Kay
http://www.saxonica.com/



Regards
Olaf

Here are the XML (tinytest.xml)
==============================================================
=========
==============================================================
=========
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="tinytest.xsl"?>
<document>
      <story>Test text with
      <tabcontent>
      <row>
              <cell>cell 1</cell>
              <cell>cell 2<start type="1"/>TEST TAG TEXT<end 
endfor="start"/></cell>
              <cell>cell 3<start type="5"/>TEST TAG TEXT<end 
endfor="start"/> </cell>
      </row>
      </tabcontent> more <start type="2"/>TEST TAG TEXT<end 
endfor="start"/>
      if TEST  <start type="3"/><test desc="should be 
removed"/>TEST TAG
      <test1 desc="should be removed">also trash</test1><end 
endfor="start"/>would need.
      And another test text TEST TAG TEXT with some regex markup.
      <start type="4"/>TEST TAG TEXT<end endfor="start"/></story>
</document>
==============================================================
=========
==============================================================
=========
and the XSL (tinytest.xsl)
==============================================================
=========
==============================================================
=========
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
      xmlns:xs="http://www.w3.org/2001/XMLSchema";
      xmlns="http://www.example.com/tinytest";
      >

      <xsl:output method="xml" version="1.0" encoding="UTF-8" 
indent="yes"/>

      <xsl:template match="/">
      <xsl:apply-templates />
      </xsl:template>

      <xsl:template match="story" >
              <text x="single story">
                      <xsl:for-each select="node()">
                              <xsl:call-template name="breaktest"/>
                      </xsl:for-each>
              </text>
      </xsl:template>
      
      <xsl:template match="cell" >
              <td x="single cell">
                      <xsl:for-each select="node()">
                              <xsl:call-template name="breaktest"/>
                      </xsl:for-each>
              </td>
      </xsl:template>

      <xsl:template match="row" >
              <tr>
                      <xsl:apply-templates />
              </tr>
      </xsl:template>

      <xsl:template match="tabcontent" >
              <table>
                      <xsl:apply-templates />
              </table>
      </xsl:template>

      <xsl:template name="breaktest">
      <xsl:choose>
              <xsl:when test="(name() eq 'start')
                                              and 
(following-sibling::*[1]/name() eq 'end')
                                              and 
(following-sibling::*[1]/@endfor eq 'start') ">
                      <FoundStart>
                              <xsl:attribute 
name="mytype"><xsl:value-of 
select="@type"/></xsl:attribute>
                      </FoundStart>
              </xsl:when>
              <xsl:when test="(name() eq 'end')
                                              and (@endfor eq 'start')
                                              and 
(preceding-sibling::*[1]/name() eq 
'start')"><FoundEndWillBeDeleted/></xsl:when>
              <xsl:when test="(self::text())
                                              and 
(preceding-sibling::*[1]/name() eq 'start')
                                              and 
(following-sibling::*[1]/name() eq 'end')
                                              and 
(following-sibling::*[1]/@endfor eq 
'start')"><FoundTextWillBeDeleted/></xsl:when>
              <xsl:when test="(self::text())">
                      <xsl:analyze-string select="." 
regex="(.*?)(TEST TAG TEXT)">
                              <xsl:matching-substring>
                                      <xsl:value-of 
select="regex-group(1)"/><FoundTextWillBeChanged/>
                              </xsl:matching-substring>
                              <xsl:non-matching-substring>
                                      <xsl:value-of select="."/>
                              </xsl:non-matching-substring>
                      </xsl:analyze-string>
              </xsl:when>
              <xsl:otherwise>
                      <xsl:analyze-string select="." 
regex="(.*?)(TEST TAG TEXT)">
                              <xsl:matching-substring>
                                      <xsl:value-of 
select="regex-group(1)"/><FoundTextWillBeChanged/>
                              </xsl:matching-substring>
                              <xsl:non-matching-substring>
                                      <xsl:value-of select="."/>
                              </xsl:non-matching-substring>
                      </xsl:analyze-string>
                      <xsl:apply-templates />
              </xsl:otherwise>
      </xsl:choose>
      </xsl:template>

</xsl:stylesheet>

==============================================================
=========
==============================================================
=========


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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