I am dividing two XML elements using XSLT. I keep getting a NaN response after defining variables.












0














I am new to XML and I am building a few Recipies using XML. I am using XSLT to transform the XML, however, I keep getting NaN as the response.



I want to create a calorie per serving output by diving the qty of servings, under yield and divide it by the qty of calories -- defined by undernutrition. I am not sure that I am assigning numbers correctly.



Any tips would be much appreciated.



XML CODE:



<recipe>
<head>
<yield>
<qty>7</qty>
<unit>servings</unit>
</yield>
</head>
<nutrition>
<nutrient>
<n-name>calories</n-name>
<qty>1200</qty>
</nutrient>
</nutrition>
</recipe>


XSLT CODE:



<xsl:template match="nutrient">
<xsl:variable name="calorietotal" select="//nutrient[n-name='calories']/qty" />
<xsl:variable name="servings" select="head[yield='serving']/qty" />

<div class="ings">
<div class="numcals">Calories Per Serving:</div>
<xsl:value-of select="$calorietotal div $servings" />
</div>
</xsl:template>









share|improve this question



























    0














    I am new to XML and I am building a few Recipies using XML. I am using XSLT to transform the XML, however, I keep getting NaN as the response.



    I want to create a calorie per serving output by diving the qty of servings, under yield and divide it by the qty of calories -- defined by undernutrition. I am not sure that I am assigning numbers correctly.



    Any tips would be much appreciated.



    XML CODE:



    <recipe>
    <head>
    <yield>
    <qty>7</qty>
    <unit>servings</unit>
    </yield>
    </head>
    <nutrition>
    <nutrient>
    <n-name>calories</n-name>
    <qty>1200</qty>
    </nutrient>
    </nutrition>
    </recipe>


    XSLT CODE:



    <xsl:template match="nutrient">
    <xsl:variable name="calorietotal" select="//nutrient[n-name='calories']/qty" />
    <xsl:variable name="servings" select="head[yield='serving']/qty" />

    <div class="ings">
    <div class="numcals">Calories Per Serving:</div>
    <xsl:value-of select="$calorietotal div $servings" />
    </div>
    </xsl:template>









    share|improve this question

























      0












      0








      0







      I am new to XML and I am building a few Recipies using XML. I am using XSLT to transform the XML, however, I keep getting NaN as the response.



      I want to create a calorie per serving output by diving the qty of servings, under yield and divide it by the qty of calories -- defined by undernutrition. I am not sure that I am assigning numbers correctly.



      Any tips would be much appreciated.



      XML CODE:



      <recipe>
      <head>
      <yield>
      <qty>7</qty>
      <unit>servings</unit>
      </yield>
      </head>
      <nutrition>
      <nutrient>
      <n-name>calories</n-name>
      <qty>1200</qty>
      </nutrient>
      </nutrition>
      </recipe>


      XSLT CODE:



      <xsl:template match="nutrient">
      <xsl:variable name="calorietotal" select="//nutrient[n-name='calories']/qty" />
      <xsl:variable name="servings" select="head[yield='serving']/qty" />

      <div class="ings">
      <div class="numcals">Calories Per Serving:</div>
      <xsl:value-of select="$calorietotal div $servings" />
      </div>
      </xsl:template>









      share|improve this question













      I am new to XML and I am building a few Recipies using XML. I am using XSLT to transform the XML, however, I keep getting NaN as the response.



      I want to create a calorie per serving output by diving the qty of servings, under yield and divide it by the qty of calories -- defined by undernutrition. I am not sure that I am assigning numbers correctly.



      Any tips would be much appreciated.



      XML CODE:



      <recipe>
      <head>
      <yield>
      <qty>7</qty>
      <unit>servings</unit>
      </yield>
      </head>
      <nutrition>
      <nutrient>
      <n-name>calories</n-name>
      <qty>1200</qty>
      </nutrient>
      </nutrition>
      </recipe>


      XSLT CODE:



      <xsl:template match="nutrient">
      <xsl:variable name="calorietotal" select="//nutrient[n-name='calories']/qty" />
      <xsl:variable name="servings" select="head[yield='serving']/qty" />

      <div class="ings">
      <div class="numcals">Calories Per Serving:</div>
      <xsl:value-of select="$calorietotal div $servings" />
      </div>
      </xsl:template>






      xml xslt xpath






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 '18 at 23:49









      G.Santa

      31




      31
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Simply your XPaths are not correct within the context of nutrient node and with slight misspelling. Consider below adjustments where division will not result as NaN.



          <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty" />
          <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty" />


          Also, too, you are not exhaustively re-writing the tree so some unspecified node text renders in output such as "7 servings". Add another template to walk down the tree from root and only write a style for nutrient node. Even add output parameters for indentation and html method.



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

          <xsl:template match="/recipe">
          <xsl:apply-templates select="nutrition/nutrient"/>
          </xsl:template>

          <xsl:template match="nutrient">
          <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty"/>
          <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty"/>

          <div class="ings">
          <div class="numcals">Calories Per Serving:</div>
          <xsl:value-of select="$calorietotal div $servings"/>
          </div>
          </xsl:template>
          </xsl:stylesheet>


          XSLT Demo






          share|improve this answer































            0














            The head element is not a child of nutrient so the value of the $servings variable is an empty node-set, and converting an empty node-set to a number returns NaN.



            When you use path expressions you need to understand the concept of the "current node", which is where selections are made from. In a template that matches the "nutrient" element, that element is the current node.



            In future, when asking XSLT questions on StackOverflow, please be sure to say whether you are using XSLT 1.0 or 2.0. Both are widely used, and they have many differences.






            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%2f53254411%2fi-am-dividing-two-xml-elements-using-xslt-i-keep-getting-a-nan-response-after-d%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









              1














              Simply your XPaths are not correct within the context of nutrient node and with slight misspelling. Consider below adjustments where division will not result as NaN.



              <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty" />
              <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty" />


              Also, too, you are not exhaustively re-writing the tree so some unspecified node text renders in output such as "7 servings". Add another template to walk down the tree from root and only write a style for nutrient node. Even add output parameters for indentation and html method.



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

              <xsl:template match="/recipe">
              <xsl:apply-templates select="nutrition/nutrient"/>
              </xsl:template>

              <xsl:template match="nutrient">
              <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty"/>
              <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty"/>

              <div class="ings">
              <div class="numcals">Calories Per Serving:</div>
              <xsl:value-of select="$calorietotal div $servings"/>
              </div>
              </xsl:template>
              </xsl:stylesheet>


              XSLT Demo






              share|improve this answer




























                1














                Simply your XPaths are not correct within the context of nutrient node and with slight misspelling. Consider below adjustments where division will not result as NaN.



                <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty" />
                <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty" />


                Also, too, you are not exhaustively re-writing the tree so some unspecified node text renders in output such as "7 servings". Add another template to walk down the tree from root and only write a style for nutrient node. Even add output parameters for indentation and html method.



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

                <xsl:template match="/recipe">
                <xsl:apply-templates select="nutrition/nutrient"/>
                </xsl:template>

                <xsl:template match="nutrient">
                <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty"/>
                <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty"/>

                <div class="ings">
                <div class="numcals">Calories Per Serving:</div>
                <xsl:value-of select="$calorietotal div $servings"/>
                </div>
                </xsl:template>
                </xsl:stylesheet>


                XSLT Demo






                share|improve this answer


























                  1












                  1








                  1






                  Simply your XPaths are not correct within the context of nutrient node and with slight misspelling. Consider below adjustments where division will not result as NaN.



                  <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty" />
                  <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty" />


                  Also, too, you are not exhaustively re-writing the tree so some unspecified node text renders in output such as "7 servings". Add another template to walk down the tree from root and only write a style for nutrient node. Even add output parameters for indentation and html method.



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

                  <xsl:template match="/recipe">
                  <xsl:apply-templates select="nutrition/nutrient"/>
                  </xsl:template>

                  <xsl:template match="nutrient">
                  <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty"/>
                  <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty"/>

                  <div class="ings">
                  <div class="numcals">Calories Per Serving:</div>
                  <xsl:value-of select="$calorietotal div $servings"/>
                  </div>
                  </xsl:template>
                  </xsl:stylesheet>


                  XSLT Demo






                  share|improve this answer














                  Simply your XPaths are not correct within the context of nutrient node and with slight misspelling. Consider below adjustments where division will not result as NaN.



                  <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty" />
                  <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty" />


                  Also, too, you are not exhaustively re-writing the tree so some unspecified node text renders in output such as "7 servings". Add another template to walk down the tree from root and only write a style for nutrient node. Even add output parameters for indentation and html method.



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

                  <xsl:template match="/recipe">
                  <xsl:apply-templates select="nutrition/nutrient"/>
                  </xsl:template>

                  <xsl:template match="nutrient">
                  <xsl:variable name="calorietotal" select="n-name[.='calories']/following-sibling::qty"/>
                  <xsl:variable name="servings" select="/recipe/head/yield[unit='servings']/qty"/>

                  <div class="ings">
                  <div class="numcals">Calories Per Serving:</div>
                  <xsl:value-of select="$calorietotal div $servings"/>
                  </div>
                  </xsl:template>
                  </xsl:stylesheet>


                  XSLT Demo







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 '18 at 17:26

























                  answered Nov 12 '18 at 0:25









                  Parfait

                  49.6k84269




                  49.6k84269

























                      0














                      The head element is not a child of nutrient so the value of the $servings variable is an empty node-set, and converting an empty node-set to a number returns NaN.



                      When you use path expressions you need to understand the concept of the "current node", which is where selections are made from. In a template that matches the "nutrient" element, that element is the current node.



                      In future, when asking XSLT questions on StackOverflow, please be sure to say whether you are using XSLT 1.0 or 2.0. Both are widely used, and they have many differences.






                      share|improve this answer


























                        0














                        The head element is not a child of nutrient so the value of the $servings variable is an empty node-set, and converting an empty node-set to a number returns NaN.



                        When you use path expressions you need to understand the concept of the "current node", which is where selections are made from. In a template that matches the "nutrient" element, that element is the current node.



                        In future, when asking XSLT questions on StackOverflow, please be sure to say whether you are using XSLT 1.0 or 2.0. Both are widely used, and they have many differences.






                        share|improve this answer
























                          0












                          0








                          0






                          The head element is not a child of nutrient so the value of the $servings variable is an empty node-set, and converting an empty node-set to a number returns NaN.



                          When you use path expressions you need to understand the concept of the "current node", which is where selections are made from. In a template that matches the "nutrient" element, that element is the current node.



                          In future, when asking XSLT questions on StackOverflow, please be sure to say whether you are using XSLT 1.0 or 2.0. Both are widely used, and they have many differences.






                          share|improve this answer












                          The head element is not a child of nutrient so the value of the $servings variable is an empty node-set, and converting an empty node-set to a number returns NaN.



                          When you use path expressions you need to understand the concept of the "current node", which is where selections are made from. In a template that matches the "nutrient" element, that element is the current node.



                          In future, when asking XSLT questions on StackOverflow, please be sure to say whether you are using XSLT 1.0 or 2.0. Both are widely used, and they have many differences.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 '18 at 0:27









                          Michael Kay

                          108k660114




                          108k660114






























                              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%2f53254411%2fi-am-dividing-two-xml-elements-using-xslt-i-keep-getting-a-nan-response-after-d%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

                              Full-time equivalent

                              Bicuculline

                              さくらももこ