XSLT with output files

Discussion in 'XML' started by bbembi_de@lycos.de, Jan 30, 2008.

  1. Guest

    Hello everyone,

    I have a problem with a xsl transformation.

    I have a input file like this:

    <root>
    <element name="element1">
    <group name="111" />
    <group name="222" />
    </element>
    <element name="element2">
    <group name="111" />
    <group name="333" />
    <group name="444" />
    </element>
    </root>

    Now I want a output file for every group. the output file should be
    like this:

    111.txt:
    element1
    element2

    222.txt
    element1
    element2

    333.txt
    element3

    and so on...

    I use xslt 2.0 with saxon.
    I know I have to write every output file at once. But how?

    In my current solution I only get one output line each file:

    ....
    <xsl:template match="/">
    <xsl:for-each-group select="root/element/group" group-by="name">
    <xsl:result-document href="{name}.xml">
    <xsl:call-template name="CodeImplTemp"/>
    </xsl:result-document>
    </xsl:for-each-group>
    ....

    <xsl:template name="CodeImplTemp">
    <xsl:value-of select="../name"/>
    </xsl:template>

    Thanks
    bye bembi
    , Jan 30, 2008
    #1
    1. Advertising

  2. wrote:

    > I have a input file like this:
    >
    > <root>
    > <element name="element1">
    > <group name="111" />
    > <group name="222" />
    > </element>
    > <element name="element2">
    > <group name="111" />
    > <group name="333" />
    > <group name="444" />
    > </element>
    > </root>
    >
    > Now I want a output file for every group. the output file should be
    > like this:
    >
    > 111.txt:
    > element1
    > element2
    >
    > 222.txt
    > element1
    > element2


    But there is only one <group name="222"> as a child of <element
    name="element1"> so I am not sure I understand where the element2 in
    222.txt comes from.

    > 333.txt
    > element3
    >
    > and so on...
    >
    > I use xslt 2.0 with saxon.
    > I know I have to write every output file at once. But how?
    >
    > In my current solution I only get one output line each file:
    >
    > ...
    > <xsl:template match="/">
    > <xsl:for-each-group select="root/element/group" group-by="name">
    > <xsl:result-document href="{name}.xml">


    <xsl:result-document href="{current-grouping-key()}.txt">
    <xsl:apply-templates select="current-group()"/>
    </xsl:result-document>
    </xsl:for-each-group>
    </xsl:template>


    <xsl:template match="group">
    <xsl:value-of select="../@name"/>
    <xsl:if test="position() != last()">
    <xsl:text>
    </xsl:text>
    </xsl:if>
    </xsl:template>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 30, 2008
    #2
    1. Advertising

  3. Guest

    On 30 Jan., 16:23, Martin Honnen <> wrote:
    > wrote:
    > > I have a input file like this:

    >
    > > <root>
    > > <element name="element1">
    > > <group name="111" />
    > > <group name="222" />
    > > </element>
    > > <element name="element2">
    > > <group name="111" />
    > > <group name="333" />
    > > <group name="444" />
    > > </element>
    > > </root>

    >
    > > Now I want a output file for every group. the output file should be
    > > like this:

    >
    > > 111.txt:
    > > element1
    > > element2

    >
    > > 222.txt
    > > element1
    > > element2

    >
    > But there is only one <group name="222"> as a child of <element
    > name="element1"> so I am not sure I understand where the element2 in
    > 222.txt comes from.
    >
    > > 333.txt
    > > element3

    >
    > > and so on...

    >
    > > I use xslt 2.0 with saxon.
    > > I know I have to write every output file at once. But how?

    >
    > > In my current solution I only get one output line each file:

    >
    > > ...
    > > <xsl:template match="/">
    > > <xsl:for-each-group select="root/element/group" group-by="name">
    > > <xsl:result-document href="{name}.xml">

    >
    > <xsl:result-document href="{current-grouping-key()}.txt">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:result-document>
    > </xsl:for-each-group>
    > </xsl:template>
    >
    > <xsl:template match="group">
    > <xsl:value-of select="../@name"/>
    > <xsl:if test="position() != last()">
    > <xsl:text>
    > </xsl:text>
    > </xsl:if>
    > </xsl:template>
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    Oh, OK sorry my mistake.
    It should be only element1 in 222.txt.

    bye bembi
    , Jan 30, 2008
    #3
  4. Guest

    On 30 Jan., 17:36, "" <> wrote:
    > On 30 Jan., 16:23, Martin Honnen <> wrote:
    >
    >
    >
    > > wrote:
    > > > I have a input file like this:

    >
    > > > <root>
    > > > <element name="element1">
    > > > <group name="111" />
    > > > <group name="222" />
    > > > </element>
    > > > <element name="element2">
    > > > <group name="111" />
    > > > <group name="333" />
    > > > <group name="444" />
    > > > </element>
    > > > </root>

    >
    > > > Now I want a output file for every group. the output file should be
    > > > like this:

    >
    > > > 111.txt:
    > > > element1
    > > > element2

    >
    > > > 222.txt
    > > > element1
    > > > element2

    >
    > > But there is only one <group name="222"> as a child of <element
    > > name="element1"> so I am not sure I understand where the element2 in
    > > 222.txt comes from.

    >
    > > > 333.txt
    > > > element3

    >
    > > > and so on...

    >
    > > > I use xslt 2.0 with saxon.
    > > > I know I have to write every output file at once. But how?

    >
    > > > In my current solution I only get one output line each file:

    >
    > > > ...
    > > > <xsl:template match="/">
    > > > <xsl:for-each-group select="root/element/group" group-by="name">
    > > > <xsl:result-document href="{name}.xml">

    >
    > > <xsl:result-document href="{current-grouping-key()}.txt">
    > > <xsl:apply-templates select="current-group()"/>
    > > </xsl:result-document>
    > > </xsl:for-each-group>
    > > </xsl:template>

    >
    > > <xsl:template match="group">
    > > <xsl:value-of select="../@name"/>
    > > <xsl:if test="position() != last()">
    > > <xsl:text>
    > > </xsl:text>
    > > </xsl:if>
    > > </xsl:template>

    >
    > > --

    >
    > > Martin Honnen
    > > http://JavaScript.FAQTs.com/

    >
    > Oh, OK sorry my mistake.
    > It should be only element1 in 222.txt.
    >
    > bye bembi


    By the way - it works just perfectly!
    Thanks very much!!!

    bye bembi
    , Jan 31, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    632
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,655
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    610
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    704
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    651
Loading...

Share This Page