xsl-list
[Top] [All Lists]

Re: numbering and document()

2003-08-22 10:23:34
Use the following transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="rowset">
    <site>
      <xsl:apply-templates select="row[site_parent = 0]"/>
    </site>
  </xsl:template>

  <xsl:template match="row">
    <xsl:copy-of select="site_id | site_name"/>
    <xsl:variable name="vChildren"
        select="../row[site_parent = current()/site_id]"/>
    <xsl:if test="$vChildren">
      <site>
        <xsl:apply-templates select="$vChildren"/>
      </site>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

When applied against your source.xml:

<page>
  <rowset>
    <row>
      <site_id>1</site_id>
      <site_name>Test Parent1</site_name>
      <site_parent>0</site_parent>
      <level>1</level>
      <path>/0</path>
    </row>
    <row>
      <site_id>2</site_id>
      <site_name>Test Child1 of Test Parent1</site_name>
      <site_parent>1</site_parent>
      <level>2</level>
      <path>/0/1</path>
    </row>
    <row>
      <site_id>4</site_id>
      <site_name>Test Child1 of Test Child1</site_name>
      <site_parent>2</site_parent>
      <level>3</level>
      <path>/0/1/2</path>
    </row>
    <row>
      <site_id>3</site_id>
      <site_name>Test Parent2</site_name>
      <site_parent>0</site_parent>
      <level>1</level>
      <path>/0</path>
    </row>
  </rowset>
</page>

the wanted result is produced:

<site>
  <site_id>1</site_id>
  <site_name>Test Parent1</site_name>
  <site>
    <site_id>2</site_id>
    <site_name>Test Child1 of Test Parent1</site_name>
    <site>
      <site_id>4</site_id>
      <site_name>Test Child1 of Test Child1</site_name>
    </site>
  </site>
  <site_id>3</site_id>
  <site_name>Test Parent2</site_name>
</site>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL



"Felix Breuer" <felix(_at_)fbreuer(_dot_)de> wrote in message
news:1061569227(_dot_)4072(_dot_)32(_dot_)camel(_at_)tapir(_dot_)(_dot_)(_dot_)
Hello!

The XML DT I am creating a stylesheet for has an

<include file="sub.xml"/>

element, which can be used to include sub.xml seamlessly in the main
document. Now, suppose the documents look like this

main.xml:
-----------------------------
<doc>
    <section/>
    <section/>
    <include file="sub.xml"/>
    <section/>
</doc>
-----------------------------

sub.xml:
-----------------------------
<doc>
    <section/>
    <section/>
</doc>
-----------------------------

and my stylesheet is supposed to number the sections in these documents
in the following way:

1 Section
2 Section
3 Section     <--- from sub.xml
4 Section     <--- from sub.xml
5 Section

whereas 4 and 5 are from sub.xml. I tried to achieve this using the
following templates:

<xsl:template match="section>
    <xsl:number/> Section
</xsl:template>

<xsl:template match="include">
    <xsl:for-each select="document(@file)/doc/*">
        <xsl:apply-templates select="self::section"/>
    </xsl:for-each>
</xsl:template>

but the numbering I get, is

1 Section
2 Section
1 Section     <--- from sub.xml
2 Section     <--- from sub.xml
3 Section

I am at a loss as to how to solve this problem. If anybody has an idea,
please tell me.

Thanks in advance,
Felix Breuer


 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>