split XML node by half (XSLT transformation)












1














Could you please help how to split in half node with subnodes.



Input:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Output:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="hard-coded guid1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
<FileHashRule Id="hard-coded guid2">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Unfortunatly I dont have expirience with xslt and did not find such an examples how to do it.



Updated
I tried one of the suggested approach below and have this https://xsltfiddle.liberty-development.net/jyH9rNq/3 nodes doesn't copy



xslt:



            <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:variable name="fileHash" select="/FileHashRule/Conditions/FileHashCondition/FileHash" />
<xsl:variable name="half" select="count($fileHash) div 2" />

<xsl:template match="/">
<RuleCollection>
<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfa</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &lt;= $half]" />

</FileHashCondition>
</Conditions>
</FileHashRule>

<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfb</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &gt; $half]" />
</FileHashCondition>
</Conditions>
</FileHashRule>


</RuleCollection>
</xsl:template>

</xsl:stylesheet>


Output:



        <?xml version="1.0" encoding="UTF-8"?>
<RuleCollection>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfa">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfb">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
</RuleCollection>









share|improve this question
























  • Define "half", esp. in the case of odd number of nodes. -- Note also that the output you show is not well-formed XML (lacks a single root element).
    – michael.hor257k
    Nov 23 '18 at 12:12










  • @michael.hor257k, corrected, about half, doesnt matter
    – Alexander
    Nov 23 '18 at 12:22










  • When you're learning a new language, you don't need experience and you don't need an example that solves the exact problem you're tackling, you need a good book or tutorial that teaches the concepts, and you should learn by doing simple exercises first before you try and tackle a more difficult problem.
    – Michael Kay
    Nov 23 '18 at 12:26










  • You keep changing your input XML format. Now that you have added the RuleCollection wrapper, you must change the fileHash variable definition to: <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" /> - see: xsltfiddle.liberty-development.net/jyH9rNq/4
    – michael.hor257k
    Nov 24 '18 at 9:01












  • @michael.hor257k, Thanks a lot, works great. what I need
    – Alexander
    Nov 24 '18 at 20:37
















1














Could you please help how to split in half node with subnodes.



Input:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Output:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="hard-coded guid1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
<FileHashRule Id="hard-coded guid2">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Unfortunatly I dont have expirience with xslt and did not find such an examples how to do it.



Updated
I tried one of the suggested approach below and have this https://xsltfiddle.liberty-development.net/jyH9rNq/3 nodes doesn't copy



xslt:



            <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:variable name="fileHash" select="/FileHashRule/Conditions/FileHashCondition/FileHash" />
<xsl:variable name="half" select="count($fileHash) div 2" />

<xsl:template match="/">
<RuleCollection>
<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfa</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &lt;= $half]" />

</FileHashCondition>
</Conditions>
</FileHashRule>

<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfb</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &gt; $half]" />
</FileHashCondition>
</Conditions>
</FileHashRule>


</RuleCollection>
</xsl:template>

</xsl:stylesheet>


Output:



        <?xml version="1.0" encoding="UTF-8"?>
<RuleCollection>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfa">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfb">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
</RuleCollection>









share|improve this question
























  • Define "half", esp. in the case of odd number of nodes. -- Note also that the output you show is not well-formed XML (lacks a single root element).
    – michael.hor257k
    Nov 23 '18 at 12:12










  • @michael.hor257k, corrected, about half, doesnt matter
    – Alexander
    Nov 23 '18 at 12:22










  • When you're learning a new language, you don't need experience and you don't need an example that solves the exact problem you're tackling, you need a good book or tutorial that teaches the concepts, and you should learn by doing simple exercises first before you try and tackle a more difficult problem.
    – Michael Kay
    Nov 23 '18 at 12:26










  • You keep changing your input XML format. Now that you have added the RuleCollection wrapper, you must change the fileHash variable definition to: <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" /> - see: xsltfiddle.liberty-development.net/jyH9rNq/4
    – michael.hor257k
    Nov 24 '18 at 9:01












  • @michael.hor257k, Thanks a lot, works great. what I need
    – Alexander
    Nov 24 '18 at 20:37














1












1








1







Could you please help how to split in half node with subnodes.



Input:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Output:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="hard-coded guid1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
<FileHashRule Id="hard-coded guid2">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Unfortunatly I dont have expirience with xslt and did not find such an examples how to do it.



Updated
I tried one of the suggested approach below and have this https://xsltfiddle.liberty-development.net/jyH9rNq/3 nodes doesn't copy



xslt:



            <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:variable name="fileHash" select="/FileHashRule/Conditions/FileHashCondition/FileHash" />
<xsl:variable name="half" select="count($fileHash) div 2" />

<xsl:template match="/">
<RuleCollection>
<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfa</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &lt;= $half]" />

</FileHashCondition>
</Conditions>
</FileHashRule>

<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfb</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &gt; $half]" />
</FileHashCondition>
</Conditions>
</FileHashRule>


</RuleCollection>
</xsl:template>

</xsl:stylesheet>


Output:



        <?xml version="1.0" encoding="UTF-8"?>
<RuleCollection>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfa">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfb">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
</RuleCollection>









share|improve this question















Could you please help how to split in half node with subnodes.



Input:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Output:



<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FileHashRule Id="hard-coded guid1">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xCC864"></FileHash>
<FileHash Type="SHA256" Data="0x9D973"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
<FileHashRule Id="hard-coded guid2">
<Conditions>
<FileHashCondition>
<FileHash Type="SHA256" Data="0xA92EF"></FileHash>
<FileHash Type="SHA256" Data="0x279CD"></FileHash>
</FileHashCondition>
</Conditions>
</FileHashRule>
</RuleCollection>


Unfortunatly I dont have expirience with xslt and did not find such an examples how to do it.



Updated
I tried one of the suggested approach below and have this https://xsltfiddle.liberty-development.net/jyH9rNq/3 nodes doesn't copy



xslt:



            <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:variable name="fileHash" select="/FileHashRule/Conditions/FileHashCondition/FileHash" />
<xsl:variable name="half" select="count($fileHash) div 2" />

<xsl:template match="/">
<RuleCollection>
<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfa</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &lt;= $half]" />

</FileHashCondition>
</Conditions>
</FileHashRule>

<FileHashRule>
<xsl:attribute name="Id">ad22d301-eb88-485d-ae1d-537790bdebfb</xsl:attribute>
<Conditions>

<FileHashCondition>
<xsl:copy-of select="$fileHash[position() &gt; $half]" />
</FileHashCondition>
</Conditions>
</FileHashRule>


</RuleCollection>
</xsl:template>

</xsl:stylesheet>


Output:



        <?xml version="1.0" encoding="UTF-8"?>
<RuleCollection>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfa">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
<FileHashRule Id="ad22d301-eb88-485d-ae1d-537790bdebfb">
<Conditions>
<FileHashCondition/>
</Conditions>
</FileHashRule>
</RuleCollection>






xml xslt-1.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 8:21







Alexander

















asked Nov 23 '18 at 11:15









AlexanderAlexander

103




103












  • Define "half", esp. in the case of odd number of nodes. -- Note also that the output you show is not well-formed XML (lacks a single root element).
    – michael.hor257k
    Nov 23 '18 at 12:12










  • @michael.hor257k, corrected, about half, doesnt matter
    – Alexander
    Nov 23 '18 at 12:22










  • When you're learning a new language, you don't need experience and you don't need an example that solves the exact problem you're tackling, you need a good book or tutorial that teaches the concepts, and you should learn by doing simple exercises first before you try and tackle a more difficult problem.
    – Michael Kay
    Nov 23 '18 at 12:26










  • You keep changing your input XML format. Now that you have added the RuleCollection wrapper, you must change the fileHash variable definition to: <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" /> - see: xsltfiddle.liberty-development.net/jyH9rNq/4
    – michael.hor257k
    Nov 24 '18 at 9:01












  • @michael.hor257k, Thanks a lot, works great. what I need
    – Alexander
    Nov 24 '18 at 20:37


















  • Define "half", esp. in the case of odd number of nodes. -- Note also that the output you show is not well-formed XML (lacks a single root element).
    – michael.hor257k
    Nov 23 '18 at 12:12










  • @michael.hor257k, corrected, about half, doesnt matter
    – Alexander
    Nov 23 '18 at 12:22










  • When you're learning a new language, you don't need experience and you don't need an example that solves the exact problem you're tackling, you need a good book or tutorial that teaches the concepts, and you should learn by doing simple exercises first before you try and tackle a more difficult problem.
    – Michael Kay
    Nov 23 '18 at 12:26










  • You keep changing your input XML format. Now that you have added the RuleCollection wrapper, you must change the fileHash variable definition to: <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" /> - see: xsltfiddle.liberty-development.net/jyH9rNq/4
    – michael.hor257k
    Nov 24 '18 at 9:01












  • @michael.hor257k, Thanks a lot, works great. what I need
    – Alexander
    Nov 24 '18 at 20:37
















Define "half", esp. in the case of odd number of nodes. -- Note also that the output you show is not well-formed XML (lacks a single root element).
– michael.hor257k
Nov 23 '18 at 12:12




Define "half", esp. in the case of odd number of nodes. -- Note also that the output you show is not well-formed XML (lacks a single root element).
– michael.hor257k
Nov 23 '18 at 12:12












@michael.hor257k, corrected, about half, doesnt matter
– Alexander
Nov 23 '18 at 12:22




@michael.hor257k, corrected, about half, doesnt matter
– Alexander
Nov 23 '18 at 12:22












When you're learning a new language, you don't need experience and you don't need an example that solves the exact problem you're tackling, you need a good book or tutorial that teaches the concepts, and you should learn by doing simple exercises first before you try and tackle a more difficult problem.
– Michael Kay
Nov 23 '18 at 12:26




When you're learning a new language, you don't need experience and you don't need an example that solves the exact problem you're tackling, you need a good book or tutorial that teaches the concepts, and you should learn by doing simple exercises first before you try and tackle a more difficult problem.
– Michael Kay
Nov 23 '18 at 12:26












You keep changing your input XML format. Now that you have added the RuleCollection wrapper, you must change the fileHash variable definition to: <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" /> - see: xsltfiddle.liberty-development.net/jyH9rNq/4
– michael.hor257k
Nov 24 '18 at 9:01






You keep changing your input XML format. Now that you have added the RuleCollection wrapper, you must change the fileHash variable definition to: <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" /> - see: xsltfiddle.liberty-development.net/jyH9rNq/4
– michael.hor257k
Nov 24 '18 at 9:01














@michael.hor257k, Thanks a lot, works great. what I need
– Alexander
Nov 24 '18 at 20:37




@michael.hor257k, Thanks a lot, works great. what I need
– Alexander
Nov 24 '18 at 20:37












3 Answers
3






active

oldest

votes


















0














You could find the half point quite simply by:



<xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" />
<xsl:variable name="half" select="count($fileHash) div 2" />


Then just create the two FileHashRule elements and use:



<xsl:copy-of select="$fileHash[position() &lt;= $half]" />


to populate the first one, and:



<xsl:copy-of select="$fileHash[position() &gt; $half]" />


for the second one.






share|improve this answer































    1














    You can start from something like (XSLT Fiddle):



    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="//xs">
    <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select='x[position() &lt;= last() div 2]'/>
    </xsl:copy>

    <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select='x[position() &gt; last() div 2]'/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="@* | node()">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>


    It will split



    <?xml version="1.0" encoding="utf-8" ?>
    <data>
    <xs a="b">
    <will-not-be-copied/>
    <x>1</x>
    <x>2</x>
    <x>3</x>
    <x>4</x>
    </xs>
    </data>


    into



    <?xml version="1.0" encoding="UTF-8"?>
    <data>
    <xs a="b">
    <x>1</x>
    <x>2</x>
    </xs>
    <xs a="b">
    <x>3</x>
    <x>4</x>
    </xs>
    </data>


    Note though, that you need to clarify how you want to deal with tags like <will-not-be-copied> and you might want to add your id values to the splitted <xs> tags.






    share|improve this answer





















    • Your wrappers are identical - unlike the expected output.
      – michael.hor257k
      Nov 23 '18 at 12:37



















    0














    Here's one way:



    <xsl:template match="@*|node()" mode="#all">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="#current"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="FileHashRule">
    <FileHashRule Id="{@Id}">
    <xsl:apply-templates mode="one"/>
    </FileHashRule>
    <FileHashRule Id="{@Id + 1}">
    <xsl:apply-templates mode="two"/>
    </FileHashRule>
    </xsl:template>

    <xsl:template match="FileHash[position() > last() div 2]" mode="one"/>
    <xsl:template match="FileHash[position() &lt;= last() div 2]" mode="two"/>


    The first rule says: by default, copy nodes unchanged.



    The second rule says: For a FileHashRule, create two copies, incrementing the @Id attribute in the second.



    The third rule says: during the first phase, skip any FileHash element in the second half of the list.



    The fourth rule says: during the second phase, skip any FileHash element in the first half of the list.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445663%2fsplit-xml-node-by-half-xslt-transformation%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You could find the half point quite simply by:



      <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" />
      <xsl:variable name="half" select="count($fileHash) div 2" />


      Then just create the two FileHashRule elements and use:



      <xsl:copy-of select="$fileHash[position() &lt;= $half]" />


      to populate the first one, and:



      <xsl:copy-of select="$fileHash[position() &gt; $half]" />


      for the second one.






      share|improve this answer




























        0














        You could find the half point quite simply by:



        <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" />
        <xsl:variable name="half" select="count($fileHash) div 2" />


        Then just create the two FileHashRule elements and use:



        <xsl:copy-of select="$fileHash[position() &lt;= $half]" />


        to populate the first one, and:



        <xsl:copy-of select="$fileHash[position() &gt; $half]" />


        for the second one.






        share|improve this answer


























          0












          0








          0






          You could find the half point quite simply by:



          <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" />
          <xsl:variable name="half" select="count($fileHash) div 2" />


          Then just create the two FileHashRule elements and use:



          <xsl:copy-of select="$fileHash[position() &lt;= $half]" />


          to populate the first one, and:



          <xsl:copy-of select="$fileHash[position() &gt; $half]" />


          for the second one.






          share|improve this answer














          You could find the half point quite simply by:



          <xsl:variable name="fileHash" select="/RuleCollection/FileHashRule/Conditions/FileHashCondition/FileHash" />
          <xsl:variable name="half" select="count($fileHash) div 2" />


          Then just create the two FileHashRule elements and use:



          <xsl:copy-of select="$fileHash[position() &lt;= $half]" />


          to populate the first one, and:



          <xsl:copy-of select="$fileHash[position() &gt; $half]" />


          for the second one.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 9:02

























          answered Nov 23 '18 at 12:34









          michael.hor257kmichael.hor257k

          73.8k42236




          73.8k42236

























              1














              You can start from something like (XSLT Fiddle):



              <?xml version="1.0" encoding="UTF-8"?>
              <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
              <xsl:output method="xml" indent="yes"/>

              <xsl:template match="//xs">
              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &lt;= last() div 2]'/>
              </xsl:copy>

              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &gt; last() div 2]'/>
              </xsl:copy>
              </xsl:template>

              <xsl:template match="@* | node()">
              <xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
              </xsl:copy>
              </xsl:template>

              </xsl:stylesheet>


              It will split



              <?xml version="1.0" encoding="utf-8" ?>
              <data>
              <xs a="b">
              <will-not-be-copied/>
              <x>1</x>
              <x>2</x>
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              into



              <?xml version="1.0" encoding="UTF-8"?>
              <data>
              <xs a="b">
              <x>1</x>
              <x>2</x>
              </xs>
              <xs a="b">
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              Note though, that you need to clarify how you want to deal with tags like <will-not-be-copied> and you might want to add your id values to the splitted <xs> tags.






              share|improve this answer





















              • Your wrappers are identical - unlike the expected output.
                – michael.hor257k
                Nov 23 '18 at 12:37
















              1














              You can start from something like (XSLT Fiddle):



              <?xml version="1.0" encoding="UTF-8"?>
              <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
              <xsl:output method="xml" indent="yes"/>

              <xsl:template match="//xs">
              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &lt;= last() div 2]'/>
              </xsl:copy>

              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &gt; last() div 2]'/>
              </xsl:copy>
              </xsl:template>

              <xsl:template match="@* | node()">
              <xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
              </xsl:copy>
              </xsl:template>

              </xsl:stylesheet>


              It will split



              <?xml version="1.0" encoding="utf-8" ?>
              <data>
              <xs a="b">
              <will-not-be-copied/>
              <x>1</x>
              <x>2</x>
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              into



              <?xml version="1.0" encoding="UTF-8"?>
              <data>
              <xs a="b">
              <x>1</x>
              <x>2</x>
              </xs>
              <xs a="b">
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              Note though, that you need to clarify how you want to deal with tags like <will-not-be-copied> and you might want to add your id values to the splitted <xs> tags.






              share|improve this answer





















              • Your wrappers are identical - unlike the expected output.
                – michael.hor257k
                Nov 23 '18 at 12:37














              1












              1








              1






              You can start from something like (XSLT Fiddle):



              <?xml version="1.0" encoding="UTF-8"?>
              <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
              <xsl:output method="xml" indent="yes"/>

              <xsl:template match="//xs">
              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &lt;= last() div 2]'/>
              </xsl:copy>

              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &gt; last() div 2]'/>
              </xsl:copy>
              </xsl:template>

              <xsl:template match="@* | node()">
              <xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
              </xsl:copy>
              </xsl:template>

              </xsl:stylesheet>


              It will split



              <?xml version="1.0" encoding="utf-8" ?>
              <data>
              <xs a="b">
              <will-not-be-copied/>
              <x>1</x>
              <x>2</x>
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              into



              <?xml version="1.0" encoding="UTF-8"?>
              <data>
              <xs a="b">
              <x>1</x>
              <x>2</x>
              </xs>
              <xs a="b">
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              Note though, that you need to clarify how you want to deal with tags like <will-not-be-copied> and you might want to add your id values to the splitted <xs> tags.






              share|improve this answer












              You can start from something like (XSLT Fiddle):



              <?xml version="1.0" encoding="UTF-8"?>
              <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
              <xsl:output method="xml" indent="yes"/>

              <xsl:template match="//xs">
              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &lt;= last() div 2]'/>
              </xsl:copy>

              <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates select='x[position() &gt; last() div 2]'/>
              </xsl:copy>
              </xsl:template>

              <xsl:template match="@* | node()">
              <xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
              </xsl:copy>
              </xsl:template>

              </xsl:stylesheet>


              It will split



              <?xml version="1.0" encoding="utf-8" ?>
              <data>
              <xs a="b">
              <will-not-be-copied/>
              <x>1</x>
              <x>2</x>
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              into



              <?xml version="1.0" encoding="UTF-8"?>
              <data>
              <xs a="b">
              <x>1</x>
              <x>2</x>
              </xs>
              <xs a="b">
              <x>3</x>
              <x>4</x>
              </xs>
              </data>


              Note though, that you need to clarify how you want to deal with tags like <will-not-be-copied> and you might want to add your id values to the splitted <xs> tags.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '18 at 12:32









              Micha WiedenmannMicha Wiedenmann

              10.3k1364103




              10.3k1364103












              • Your wrappers are identical - unlike the expected output.
                – michael.hor257k
                Nov 23 '18 at 12:37


















              • Your wrappers are identical - unlike the expected output.
                – michael.hor257k
                Nov 23 '18 at 12:37
















              Your wrappers are identical - unlike the expected output.
              – michael.hor257k
              Nov 23 '18 at 12:37




              Your wrappers are identical - unlike the expected output.
              – michael.hor257k
              Nov 23 '18 at 12:37











              0














              Here's one way:



              <xsl:template match="@*|node()" mode="#all">
              <xsl:copy>
              <xsl:apply-templates select="@*|node()" mode="#current"/>
              </xsl:copy>
              </xsl:template>

              <xsl:template match="FileHashRule">
              <FileHashRule Id="{@Id}">
              <xsl:apply-templates mode="one"/>
              </FileHashRule>
              <FileHashRule Id="{@Id + 1}">
              <xsl:apply-templates mode="two"/>
              </FileHashRule>
              </xsl:template>

              <xsl:template match="FileHash[position() > last() div 2]" mode="one"/>
              <xsl:template match="FileHash[position() &lt;= last() div 2]" mode="two"/>


              The first rule says: by default, copy nodes unchanged.



              The second rule says: For a FileHashRule, create two copies, incrementing the @Id attribute in the second.



              The third rule says: during the first phase, skip any FileHash element in the second half of the list.



              The fourth rule says: during the second phase, skip any FileHash element in the first half of the list.






              share|improve this answer


























                0














                Here's one way:



                <xsl:template match="@*|node()" mode="#all">
                <xsl:copy>
                <xsl:apply-templates select="@*|node()" mode="#current"/>
                </xsl:copy>
                </xsl:template>

                <xsl:template match="FileHashRule">
                <FileHashRule Id="{@Id}">
                <xsl:apply-templates mode="one"/>
                </FileHashRule>
                <FileHashRule Id="{@Id + 1}">
                <xsl:apply-templates mode="two"/>
                </FileHashRule>
                </xsl:template>

                <xsl:template match="FileHash[position() > last() div 2]" mode="one"/>
                <xsl:template match="FileHash[position() &lt;= last() div 2]" mode="two"/>


                The first rule says: by default, copy nodes unchanged.



                The second rule says: For a FileHashRule, create two copies, incrementing the @Id attribute in the second.



                The third rule says: during the first phase, skip any FileHash element in the second half of the list.



                The fourth rule says: during the second phase, skip any FileHash element in the first half of the list.






                share|improve this answer
























                  0












                  0








                  0






                  Here's one way:



                  <xsl:template match="@*|node()" mode="#all">
                  <xsl:copy>
                  <xsl:apply-templates select="@*|node()" mode="#current"/>
                  </xsl:copy>
                  </xsl:template>

                  <xsl:template match="FileHashRule">
                  <FileHashRule Id="{@Id}">
                  <xsl:apply-templates mode="one"/>
                  </FileHashRule>
                  <FileHashRule Id="{@Id + 1}">
                  <xsl:apply-templates mode="two"/>
                  </FileHashRule>
                  </xsl:template>

                  <xsl:template match="FileHash[position() > last() div 2]" mode="one"/>
                  <xsl:template match="FileHash[position() &lt;= last() div 2]" mode="two"/>


                  The first rule says: by default, copy nodes unchanged.



                  The second rule says: For a FileHashRule, create two copies, incrementing the @Id attribute in the second.



                  The third rule says: during the first phase, skip any FileHash element in the second half of the list.



                  The fourth rule says: during the second phase, skip any FileHash element in the first half of the list.






                  share|improve this answer












                  Here's one way:



                  <xsl:template match="@*|node()" mode="#all">
                  <xsl:copy>
                  <xsl:apply-templates select="@*|node()" mode="#current"/>
                  </xsl:copy>
                  </xsl:template>

                  <xsl:template match="FileHashRule">
                  <FileHashRule Id="{@Id}">
                  <xsl:apply-templates mode="one"/>
                  </FileHashRule>
                  <FileHashRule Id="{@Id + 1}">
                  <xsl:apply-templates mode="two"/>
                  </FileHashRule>
                  </xsl:template>

                  <xsl:template match="FileHash[position() > last() div 2]" mode="one"/>
                  <xsl:template match="FileHash[position() &lt;= last() div 2]" mode="two"/>


                  The first rule says: by default, copy nodes unchanged.



                  The second rule says: For a FileHashRule, create two copies, incrementing the @Id attribute in the second.



                  The third rule says: during the first phase, skip any FileHash element in the second half of the list.



                  The fourth rule says: during the second phase, skip any FileHash element in the first half of the list.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 12:38









                  Michael KayMichael Kay

                  109k660114




                  109k660114






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445663%2fsplit-xml-node-by-half-xslt-transformation%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      How to ignore python UserWarning in pytest?

                      Alexandru Averescu