XSLT loop returning the same value












0














New to XSLT and i want to flatten out my XML file for (later) ingestion into SQL. Currently I'm having trouble looping through the XML document. At the moment it picks up the number of tags but will return the same value. Also when there are loops within loops there seems to be a problem of not picking up a value at all.



XML:



<?xml version="1.0"?>
<Example>
<Num>12</Num>
<Num>13</Num>
<Num>14</Num>
<Num>15</Num>
<Num>16</Num>
<New>N</New>
<Age>118</Age>
<Name>
<PrimNam>
<Surnm>SAM</Surnm>
<FstNm>SAM</FstNm>
<SecNm>SAM</SecNm>
</PrimNam>
<Alias>
<Id>12</Id>
<DOB>01/01/1900</DOB>
<AliasNam>
<Surnm>SAM</Surnm>
<FstNm>SAMUEL</FstNm>
</AliasNam>
<AliasDate>28/12/2018</AliasDate>
</Alias>
</Name>
</Example>


Here is what my XSLT looks like at the moment



<xsl:for-each select="Example/Num">
<Num><xsl:value-of select="../Num"/></Num>
</xsl:for-each>

<New><xsl:value-of select="Example/New"/></New>
<Age><xsl:value-of select="Example/Age"/></Age>

<xsl:for-each select="Example/Name/PrimNam">
<Surnm><xsl:value-of select="../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../FstNm"/></FstNm>
<SecNm><xsl:value-of select="../../SecNm"/></SecNm>
</xsl:for-each>
<xsl:for-each select="Example/Name/Alias">
<Id><xsl:value-of select="../../Id"/></Id>
<DOB><xsl:value-of select="../../DOB"/></DOB>
<xsl:for-each select="Example/Name/Alias/AliasNam">
<Surnm><xsl:value-of select="../../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../../FstNm"/></FstNm>
</xsl:for-each>
<AliasDate><xsl:value-of select="../../AliasDate"/></AliasDate>
</xsl:for-each>


And the resulting output looks like this



<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<New>N</NewCon>
<Age>118</Age>
<Surnm></Surnm>
<FstNm></FstNm>
<SecNm></SecNm>
<Id></Id>
<DOB></DOB>
<AliasDate></AliasDate>









share|improve this question
























  • What would your desired outcome XML look like? Please add that information with an edit to your question.
    – zx485
    Dec 28 '18 at 0:59


















0














New to XSLT and i want to flatten out my XML file for (later) ingestion into SQL. Currently I'm having trouble looping through the XML document. At the moment it picks up the number of tags but will return the same value. Also when there are loops within loops there seems to be a problem of not picking up a value at all.



XML:



<?xml version="1.0"?>
<Example>
<Num>12</Num>
<Num>13</Num>
<Num>14</Num>
<Num>15</Num>
<Num>16</Num>
<New>N</New>
<Age>118</Age>
<Name>
<PrimNam>
<Surnm>SAM</Surnm>
<FstNm>SAM</FstNm>
<SecNm>SAM</SecNm>
</PrimNam>
<Alias>
<Id>12</Id>
<DOB>01/01/1900</DOB>
<AliasNam>
<Surnm>SAM</Surnm>
<FstNm>SAMUEL</FstNm>
</AliasNam>
<AliasDate>28/12/2018</AliasDate>
</Alias>
</Name>
</Example>


Here is what my XSLT looks like at the moment



<xsl:for-each select="Example/Num">
<Num><xsl:value-of select="../Num"/></Num>
</xsl:for-each>

<New><xsl:value-of select="Example/New"/></New>
<Age><xsl:value-of select="Example/Age"/></Age>

<xsl:for-each select="Example/Name/PrimNam">
<Surnm><xsl:value-of select="../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../FstNm"/></FstNm>
<SecNm><xsl:value-of select="../../SecNm"/></SecNm>
</xsl:for-each>
<xsl:for-each select="Example/Name/Alias">
<Id><xsl:value-of select="../../Id"/></Id>
<DOB><xsl:value-of select="../../DOB"/></DOB>
<xsl:for-each select="Example/Name/Alias/AliasNam">
<Surnm><xsl:value-of select="../../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../../FstNm"/></FstNm>
</xsl:for-each>
<AliasDate><xsl:value-of select="../../AliasDate"/></AliasDate>
</xsl:for-each>


And the resulting output looks like this



<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<New>N</NewCon>
<Age>118</Age>
<Surnm></Surnm>
<FstNm></FstNm>
<SecNm></SecNm>
<Id></Id>
<DOB></DOB>
<AliasDate></AliasDate>









share|improve this question
























  • What would your desired outcome XML look like? Please add that information with an edit to your question.
    – zx485
    Dec 28 '18 at 0:59
















0












0








0







New to XSLT and i want to flatten out my XML file for (later) ingestion into SQL. Currently I'm having trouble looping through the XML document. At the moment it picks up the number of tags but will return the same value. Also when there are loops within loops there seems to be a problem of not picking up a value at all.



XML:



<?xml version="1.0"?>
<Example>
<Num>12</Num>
<Num>13</Num>
<Num>14</Num>
<Num>15</Num>
<Num>16</Num>
<New>N</New>
<Age>118</Age>
<Name>
<PrimNam>
<Surnm>SAM</Surnm>
<FstNm>SAM</FstNm>
<SecNm>SAM</SecNm>
</PrimNam>
<Alias>
<Id>12</Id>
<DOB>01/01/1900</DOB>
<AliasNam>
<Surnm>SAM</Surnm>
<FstNm>SAMUEL</FstNm>
</AliasNam>
<AliasDate>28/12/2018</AliasDate>
</Alias>
</Name>
</Example>


Here is what my XSLT looks like at the moment



<xsl:for-each select="Example/Num">
<Num><xsl:value-of select="../Num"/></Num>
</xsl:for-each>

<New><xsl:value-of select="Example/New"/></New>
<Age><xsl:value-of select="Example/Age"/></Age>

<xsl:for-each select="Example/Name/PrimNam">
<Surnm><xsl:value-of select="../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../FstNm"/></FstNm>
<SecNm><xsl:value-of select="../../SecNm"/></SecNm>
</xsl:for-each>
<xsl:for-each select="Example/Name/Alias">
<Id><xsl:value-of select="../../Id"/></Id>
<DOB><xsl:value-of select="../../DOB"/></DOB>
<xsl:for-each select="Example/Name/Alias/AliasNam">
<Surnm><xsl:value-of select="../../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../../FstNm"/></FstNm>
</xsl:for-each>
<AliasDate><xsl:value-of select="../../AliasDate"/></AliasDate>
</xsl:for-each>


And the resulting output looks like this



<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<New>N</NewCon>
<Age>118</Age>
<Surnm></Surnm>
<FstNm></FstNm>
<SecNm></SecNm>
<Id></Id>
<DOB></DOB>
<AliasDate></AliasDate>









share|improve this question















New to XSLT and i want to flatten out my XML file for (later) ingestion into SQL. Currently I'm having trouble looping through the XML document. At the moment it picks up the number of tags but will return the same value. Also when there are loops within loops there seems to be a problem of not picking up a value at all.



XML:



<?xml version="1.0"?>
<Example>
<Num>12</Num>
<Num>13</Num>
<Num>14</Num>
<Num>15</Num>
<Num>16</Num>
<New>N</New>
<Age>118</Age>
<Name>
<PrimNam>
<Surnm>SAM</Surnm>
<FstNm>SAM</FstNm>
<SecNm>SAM</SecNm>
</PrimNam>
<Alias>
<Id>12</Id>
<DOB>01/01/1900</DOB>
<AliasNam>
<Surnm>SAM</Surnm>
<FstNm>SAMUEL</FstNm>
</AliasNam>
<AliasDate>28/12/2018</AliasDate>
</Alias>
</Name>
</Example>


Here is what my XSLT looks like at the moment



<xsl:for-each select="Example/Num">
<Num><xsl:value-of select="../Num"/></Num>
</xsl:for-each>

<New><xsl:value-of select="Example/New"/></New>
<Age><xsl:value-of select="Example/Age"/></Age>

<xsl:for-each select="Example/Name/PrimNam">
<Surnm><xsl:value-of select="../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../FstNm"/></FstNm>
<SecNm><xsl:value-of select="../../SecNm"/></SecNm>
</xsl:for-each>
<xsl:for-each select="Example/Name/Alias">
<Id><xsl:value-of select="../../Id"/></Id>
<DOB><xsl:value-of select="../../DOB"/></DOB>
<xsl:for-each select="Example/Name/Alias/AliasNam">
<Surnm><xsl:value-of select="../../../Surnm"/></Surnm>
<FstNm><xsl:value-of select="../../../FstNm"/></FstNm>
</xsl:for-each>
<AliasDate><xsl:value-of select="../../AliasDate"/></AliasDate>
</xsl:for-each>


And the resulting output looks like this



<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<Num>12</Num>
<New>N</NewCon>
<Age>118</Age>
<Surnm></Surnm>
<FstNm></FstNm>
<SecNm></SecNm>
<Id></Id>
<DOB></DOB>
<AliasDate></AliasDate>






xml xslt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 0:56









zx485

13.4k122946




13.4k122946










asked Dec 28 '18 at 0:14









Sam

32




32












  • What would your desired outcome XML look like? Please add that information with an edit to your question.
    – zx485
    Dec 28 '18 at 0:59




















  • What would your desired outcome XML look like? Please add that information with an edit to your question.
    – zx485
    Dec 28 '18 at 0:59


















What would your desired outcome XML look like? Please add that information with an edit to your question.
– zx485
Dec 28 '18 at 0:59






What would your desired outcome XML look like? Please add that information with an edit to your question.
– zx485
Dec 28 '18 at 0:59














2 Answers
2






active

oldest

votes


















0
















  1. The instruction:



    <xsl:for-each select="Example/Num">


    puts you in the context of Num. In order to get the value of the
    current Num, you need to do:



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


    What you do:



    <xsl:value-of select="../Num"/>


    selects the first Num child of the parent of the current
    node - which is the same one for every Num.








  1. The instruction:



    <xsl:for-each select="Example/Name/PrimNam">


    puts you in the context of PrimNam. In order to get the value
    Surnm from here, you must do:



    <xsl:value-of select="Surnm"/>


    not:



    <xsl:value-of select="../../Surnm"/>


    because Surnm is a child of PrimNam. The same applies to the
    other nodes you're unable to select.






Note that you could shorten your stylesheet considerably by using xsl:copy - for example:



<xsl:copy-of select="Example/New"/>


instead of:



<New><xsl:value-of select="Example/New"/></New>





share|improve this answer































    0














    It seems that you want to flatten your XML.

    One possibility is the following XSLT-1.0 template:



    <xsl:template match="Example/Num | Example/New | Example/Age | Example/Name/PrimNam/* | Example/Name/Alias/Id | Example/Name/Alias/DOB | Example/Name/Alias/AliasNam/* | Example/Name/Alias/AliasDate">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>


    The output is:



    <Num>12</Num>
    <Num>13</Num>
    <Num>14</Num>
    <Num>15</Num>
    <Num>16</Num>
    <New>N</New>
    <Age>118</Age>
    <Surnm>SAM1</Surnm>
    <FstNm>SAM2</FstNm>
    <SecNm>SAM3</SecNm>
    <Id>12</Id>
    <DOB>01/01/1900</DOB>
    <Surnm>SAM</Surnm>
    <FstNm>SAMUEL</FstNm>
    <AliasDate>28/12/2018</AliasDate>


    This is a flattened version of your input XML.






    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%2f53952280%2fxslt-loop-returning-the-same-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0
















      1. The instruction:



        <xsl:for-each select="Example/Num">


        puts you in the context of Num. In order to get the value of the
        current Num, you need to do:



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


        What you do:



        <xsl:value-of select="../Num"/>


        selects the first Num child of the parent of the current
        node - which is the same one for every Num.








      1. The instruction:



        <xsl:for-each select="Example/Name/PrimNam">


        puts you in the context of PrimNam. In order to get the value
        Surnm from here, you must do:



        <xsl:value-of select="Surnm"/>


        not:



        <xsl:value-of select="../../Surnm"/>


        because Surnm is a child of PrimNam. The same applies to the
        other nodes you're unable to select.






      Note that you could shorten your stylesheet considerably by using xsl:copy - for example:



      <xsl:copy-of select="Example/New"/>


      instead of:



      <New><xsl:value-of select="Example/New"/></New>





      share|improve this answer




























        0
















        1. The instruction:



          <xsl:for-each select="Example/Num">


          puts you in the context of Num. In order to get the value of the
          current Num, you need to do:



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


          What you do:



          <xsl:value-of select="../Num"/>


          selects the first Num child of the parent of the current
          node - which is the same one for every Num.








        1. The instruction:



          <xsl:for-each select="Example/Name/PrimNam">


          puts you in the context of PrimNam. In order to get the value
          Surnm from here, you must do:



          <xsl:value-of select="Surnm"/>


          not:



          <xsl:value-of select="../../Surnm"/>


          because Surnm is a child of PrimNam. The same applies to the
          other nodes you're unable to select.






        Note that you could shorten your stylesheet considerably by using xsl:copy - for example:



        <xsl:copy-of select="Example/New"/>


        instead of:



        <New><xsl:value-of select="Example/New"/></New>





        share|improve this answer


























          0












          0








          0








          1. The instruction:



            <xsl:for-each select="Example/Num">


            puts you in the context of Num. In order to get the value of the
            current Num, you need to do:



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


            What you do:



            <xsl:value-of select="../Num"/>


            selects the first Num child of the parent of the current
            node - which is the same one for every Num.








          1. The instruction:



            <xsl:for-each select="Example/Name/PrimNam">


            puts you in the context of PrimNam. In order to get the value
            Surnm from here, you must do:



            <xsl:value-of select="Surnm"/>


            not:



            <xsl:value-of select="../../Surnm"/>


            because Surnm is a child of PrimNam. The same applies to the
            other nodes you're unable to select.






          Note that you could shorten your stylesheet considerably by using xsl:copy - for example:



          <xsl:copy-of select="Example/New"/>


          instead of:



          <New><xsl:value-of select="Example/New"/></New>





          share|improve this answer
















          1. The instruction:



            <xsl:for-each select="Example/Num">


            puts you in the context of Num. In order to get the value of the
            current Num, you need to do:



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


            What you do:



            <xsl:value-of select="../Num"/>


            selects the first Num child of the parent of the current
            node - which is the same one for every Num.








          1. The instruction:



            <xsl:for-each select="Example/Name/PrimNam">


            puts you in the context of PrimNam. In order to get the value
            Surnm from here, you must do:



            <xsl:value-of select="Surnm"/>


            not:



            <xsl:value-of select="../../Surnm"/>


            because Surnm is a child of PrimNam. The same applies to the
            other nodes you're unable to select.






          Note that you could shorten your stylesheet considerably by using xsl:copy - for example:



          <xsl:copy-of select="Example/New"/>


          instead of:



          <New><xsl:value-of select="Example/New"/></New>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 28 '18 at 3:31

























          answered Dec 28 '18 at 3:25









          michael.hor257k

          73.8k42236




          73.8k42236

























              0














              It seems that you want to flatten your XML.

              One possibility is the following XSLT-1.0 template:



              <xsl:template match="Example/Num | Example/New | Example/Age | Example/Name/PrimNam/* | Example/Name/Alias/Id | Example/Name/Alias/DOB | Example/Name/Alias/AliasNam/* | Example/Name/Alias/AliasDate">
              <xsl:copy>
              <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
              </xsl:template>


              The output is:



              <Num>12</Num>
              <Num>13</Num>
              <Num>14</Num>
              <Num>15</Num>
              <Num>16</Num>
              <New>N</New>
              <Age>118</Age>
              <Surnm>SAM1</Surnm>
              <FstNm>SAM2</FstNm>
              <SecNm>SAM3</SecNm>
              <Id>12</Id>
              <DOB>01/01/1900</DOB>
              <Surnm>SAM</Surnm>
              <FstNm>SAMUEL</FstNm>
              <AliasDate>28/12/2018</AliasDate>


              This is a flattened version of your input XML.






              share|improve this answer




























                0














                It seems that you want to flatten your XML.

                One possibility is the following XSLT-1.0 template:



                <xsl:template match="Example/Num | Example/New | Example/Age | Example/Name/PrimNam/* | Example/Name/Alias/Id | Example/Name/Alias/DOB | Example/Name/Alias/AliasNam/* | Example/Name/Alias/AliasDate">
                <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
                </xsl:template>


                The output is:



                <Num>12</Num>
                <Num>13</Num>
                <Num>14</Num>
                <Num>15</Num>
                <Num>16</Num>
                <New>N</New>
                <Age>118</Age>
                <Surnm>SAM1</Surnm>
                <FstNm>SAM2</FstNm>
                <SecNm>SAM3</SecNm>
                <Id>12</Id>
                <DOB>01/01/1900</DOB>
                <Surnm>SAM</Surnm>
                <FstNm>SAMUEL</FstNm>
                <AliasDate>28/12/2018</AliasDate>


                This is a flattened version of your input XML.






                share|improve this answer


























                  0












                  0








                  0






                  It seems that you want to flatten your XML.

                  One possibility is the following XSLT-1.0 template:



                  <xsl:template match="Example/Num | Example/New | Example/Age | Example/Name/PrimNam/* | Example/Name/Alias/Id | Example/Name/Alias/DOB | Example/Name/Alias/AliasNam/* | Example/Name/Alias/AliasDate">
                  <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
                  </xsl:copy>
                  </xsl:template>


                  The output is:



                  <Num>12</Num>
                  <Num>13</Num>
                  <Num>14</Num>
                  <Num>15</Num>
                  <Num>16</Num>
                  <New>N</New>
                  <Age>118</Age>
                  <Surnm>SAM1</Surnm>
                  <FstNm>SAM2</FstNm>
                  <SecNm>SAM3</SecNm>
                  <Id>12</Id>
                  <DOB>01/01/1900</DOB>
                  <Surnm>SAM</Surnm>
                  <FstNm>SAMUEL</FstNm>
                  <AliasDate>28/12/2018</AliasDate>


                  This is a flattened version of your input XML.






                  share|improve this answer














                  It seems that you want to flatten your XML.

                  One possibility is the following XSLT-1.0 template:



                  <xsl:template match="Example/Num | Example/New | Example/Age | Example/Name/PrimNam/* | Example/Name/Alias/Id | Example/Name/Alias/DOB | Example/Name/Alias/AliasNam/* | Example/Name/Alias/AliasDate">
                  <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
                  </xsl:copy>
                  </xsl:template>


                  The output is:



                  <Num>12</Num>
                  <Num>13</Num>
                  <Num>14</Num>
                  <Num>15</Num>
                  <Num>16</Num>
                  <New>N</New>
                  <Age>118</Age>
                  <Surnm>SAM1</Surnm>
                  <FstNm>SAM2</FstNm>
                  <SecNm>SAM3</SecNm>
                  <Id>12</Id>
                  <DOB>01/01/1900</DOB>
                  <Surnm>SAM</Surnm>
                  <FstNm>SAMUEL</FstNm>
                  <AliasDate>28/12/2018</AliasDate>


                  This is a flattened version of your input XML.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 28 '18 at 1:30

























                  answered Dec 28 '18 at 1:08









                  zx485

                  13.4k122946




                  13.4k122946






























                      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%2f53952280%2fxslt-loop-returning-the-same-value%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas