I have a task to split an XML that I'm receiving using XSLT. The input XML is formatted as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<startup tenantid="edf81f6b-b223-432c-8ce8-38cbd38c76dc">
    <application sysid="cb9121ea-a82b-46ae-9521-f4563a51c5c0">
        <property type="Ws" name="module_name">Pratica</property>
        <property name="saas_user">batch_user</property>
    </application>
    <processing>
        <template type="Name">TemplateProva</template>
        <processmode>asynchronous</processmode>
        <channel>save</channel>
        <modus>central</modus>
        <editor>false</editor>
    </processing>
    <standarddata>
        <data>
            <object uniqueid="1" name="Pratica" boid="e90737aa-8785-4cbe-ba24-d7c479206872">
                <property name="ClienteCap" propid="eef76da7-e5e0-44d9-a1aa-e14621d2a993">10145</property>
                <property name="ClienteIndirizzo" propid="6b214298-61d4-4f42-ae25-9279f1db7e8f">Via Roma 10</property>
                <property name="NomeFileOutput" propid="6b1aa407-3ec8-4668-9095-da8c100ea3a1">NomeFileProva2225</property>
            </object>
            <object uniqueid="1" name="Pratica" boid="e90737aa-8785-4cbe-ba24-d7c479206872">
                <property name="ClienteCap" propid="eef76da7-e5e0-44d9-a1aa-e14621d2a993">10146</property>
                <property name="ClienteIndirizzo" propid="6b214298-61d4-4f42-ae25-9279f1db7e8f">Via Roma 11</property>
                <property name="NomeFileOutput" propid="6b1aa407-3ec8-4668-9095-da8c100ea3a1">NomeFileProva2226</property>
            </object>
        </data>
    </standarddata>
</startup>

I need to create 2 output XML documents, that should be like the input above, but each of them should just report 1 <object> element. Any suggestion on how to achieve this?

Example output files:

output1.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<startup tenantid="edf81f6b-b223-432c-8ce8-38cbd38c76dc">
    <application sysid="cb9121ea-a82b-46ae-9521-f4563a51c5c0">
        <property type="Ws" name="module_name">Pratica</property>
        <property name="saas_user">batch_user</property>
    </application>
    <processing>
        <template type="Name">TemplateProva</template>
        <processmode>asynchronous</processmode>
        <channel>save</channel>
        <modus>central</modus>
        <editor>false</editor>
    </processing>
    <standarddata>
        <data>
            <object uniqueid="1" name="Pratica" boid="e90737aa-8785-4cbe-ba24-d7c479206872">
                <property name="ClienteCap" propid="eef76da7-e5e0-44d9-a1aa-e14621d2a993">10145</property>
                <property name="ClienteIndirizzo" propid="6b214298-61d4-4f42-ae25-9279f1db7e8f">Via Roma 10</property>
                <property name="NomeFileOutput" propid="6b1aa407-3ec8-4668-9095-da8c100ea3a1">NomeFileProva2225</property>
            </object>
        </data>
    </standarddata>
</startup>

output2.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<startup tenantid="edf81f6b-b223-432c-8ce8-38cbd38c76dc">
    <application sysid="cb9121ea-a82b-46ae-9521-f4563a51c5c0">
        <property type="Ws" name="module_name">Pratica</property>
        <property name="saas_user">batch_user</property>
    </application>
    <processing>
        <template type="Name">TemplateProva</template>
        <processmode>asynchronous</processmode>
        <channel>save</channel>
        <modus>central</modus>
        <editor>false</editor>
    </processing>
    <standarddata>
        <data>
            <object uniqueid="1" name="Pratica" boid="e90737aa-8785-4cbe-ba24-d7c479206872">
                <property name="ClienteCap" propid="eef76da7-e5e0-44d9-a1aa-e14621d2a993">10146</property>
                <property name="ClienteIndirizzo" propid="6b214298-61d4-4f42-ae25-9279f1db7e8f">Via Roma 11</property>
                <property name="NomeFileOutput" propid="6b1aa407-3ec8-4668-9095-da8c100ea3a1">NomeFileProva2226</property>
            </object>
        </data>
    </standarddata>
</startup>

3 Replies 3

Easy with XSLT 3.0 (or 2.0) with xsl:result-document to generate several result documents. Are you using an XSLT 2 or 3 processor? Easy these days on lots of platform with SaxonCHE available as a Python package for pip, Saxon HE Java, Saxon HE .NET, SaxonJS for the browser or Node.js.

The positional grouping example 2 at https://martin-honnen.github.io/cheerpj3-saxonhe12-fiddle/index-latest-cheerp-test.html creates several result documents, try to adapt that.

Equally easy with an XSLT 1.0 processor such as libxslt that supports the exsl:document extension element or XSLT 1.1.

Does your input really have duplicate values in the uniqueid attribute of object?

In XSLT 3.0 you want something like

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="/">
  <xsl:for-each select="//object">
    <xsl:result-document href="{position()}.xml">
      <xsl:apply-templates select="/*">
        <xsl:with-param name="object" select="." tunnel="yes"/>
      <xsl:apply-templates>
    </xsl:result-document>
  </xsl:for-each>
</xsl:template>

<xsl:template match="object">
  <xsl:param name="object" tunnel="yes"/>
  <xsl:if test="$object is .">
    <xsl:next-match/>
  </xsl:if>
</xsl:template>

Not tested.

Your Reply

By clicking “Post Your Reply”, 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.