0

I have written an XSL-file, that reads some filenames from the source file and uses this filenames, to split another file (which is opened in the XSL-file via the document() function). The filenames are used to create several output files and certain parts of the loaded file are written to these output files.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">

    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="Link">
        <xsl:result-document href="{@url}" method="xml">
            <xsl:apply-templates select="document('Input.xml')//Node"/>
        </xsl:result-document>
    </xsl:template>

    <xsl:template match="Node">
        <xsl:copy-of select="."/>

        <xsl:if test="following-sibling::*[1][self::NextPart]">
            <!-- write some test node -->
            <xsl:element name="FoundNextPart"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

The sourcefile looks something like this

<Root>
    <SomeNode>
        <Link url="part_0.xml"/>
        <Link url="part_1.xml"/>
        <Link url="part_2.xml"/>
    </SomeNode>
</Root>

The Input.xml file will have a structure like this

<Root>
    <Node>
        <PartContent>
            <ImportantContent>0</ImportantContent>
        </PartContent>
    </Node>
    <Node>
        <PartContent>
            <ImportantContent>0</ImportantContent>
        </PartContent>
    </Node>
    <NextPart/>
    <Node>
        <PartContent>
            <ImportantContent>1</ImportantContent>
        </PartContent>
    </Node>
    <Node>
        <PartContent>
            <ImportantContent>1</ImportantContent>
        </PartContent>
    </Node>
    <NextPart/>
</Root>

My problem is now with the

<xsl:template match="Node">

I want to copy the content of the Input.xml up to the first appearance of the

<NextPart/>

node. Then I want to somehow break out of the current nodeset (//Node of the Input.xml) and continue with the next //Link. But for this next Link (file) I want to copy the content of the Input.xml between the first and the second appearance of the

<NextPart/>

node.

I'm not sure if this is feasible in any way. Also I'm not sure if my approach can be used for this. I've read something like using

<xsl:call-template name="copy">

to use the following-sibling of the current node as a parameter. But anyway I have to pass the current count of the

<NextPart/>

so that I know, which content to copy!?

1 Answer 1

2

How about processing and grouping that Input.xml once with e.g.

<xsl:variable name="groups">
  <xsl:for-each-group select="document('Input.xml')/Root/*" group-ending-with="NextPart">
    <group>
      <xsl:copy-of select="current-group()[self::Node]"/>
    </group>
  </xsl:for-each-group>
</xsl:variable>

in a global variable, then in your template you do

<xsl:template match="Link">
    <xsl:variable name="pos" select="position()"/>
    <xsl:result-document href="{@url}" method="xml">
        <xsl:copy-of select="$groups/group[$pos]/Node"/>
    </xsl:result-document>
</xsl:template>

to output the Node elements grouped earlier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.