xsl-list
[Top] [All Lists]

RE: badly need xsl help(prob with accessing child nodes in the script)

2003-10-14 07:13:59
Hi Benjamin,
  Thanks for your reply. It looks like this solution works for me. 
But I already have all that code in javascript. I use it in other places.
I have more data types like Date, Currency and Time. I am trying to re-use it 
here.
And I found a stupid work around for my original problem. Like

  loading the xml from RegOpts[0] in the init() function into a new DOM and 
accessing that from there.

I know it is lame but working.
        
Now I can re-write all that in xsl but question is 

1) Which is faster in transformation, xsl or JavaScript?
2) Is it really good maintaining two copies(xsl and javascript) of same code?
(I know this second question is not really for the forum. It is for myself)

~Mur

-----Original Message-----
From: Benjamin Farrow [mailto:lovinjess(_at_)hotmail(_dot_)com]
Sent: Monday, October 13, 2003 8:19 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] badly need xsl help(prob with accessing child nodes
in the script)


Murali,
    It's past my time to head home, but it appears that your javascript is 
not really needed.  All of what you do there could be done in XSL.  If my 
quick glance was correct, you have different date formats defined by the 
element Date/ShortFormat.  I didn't have a chance to look close enough to 
how each of these formats are defined in your switch statement in your 
javascript , but perhaps this gives an idea of how to go about it...

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:ri="urn:schemas.abcd-com:RepInven"
xmlns:s="urn:schemas.abcd-com:Static"
xmlns:l="urn:schemas.abcd-com:layers">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:variable name="Date" select="l:data/s:RegionalOptions/s:Date"/>

  <xsl:template match="/">
    <_Ctrl xmlns="">
      <xsl:apply-templates 
select="l:data/ri:Rpt_Inven/ri:Inven/ri:EffectiveDate"/>
    </_Ctrl>
  </xsl:template>

  <xsl:template match="ri:Rpt_Inven/ri:Inven/ri:EffectiveDate">
    <_Item>
      <xsl:call-template name="convertDate">
        <xsl:with-param name="date" select="."/>
      </xsl:call-template>
    </_Item>
  </xsl:template>

  <xsl:template name="convertDate">
    <xsl:param name="date"/>
    <xsl:variable name="year" select="substring($date,1,4)"/>
    <xsl:variable name="month" select="substring($date,6,2)"/>
    <xsl:variable name="day" select="substring($date,9,2)"/>
    <!-- put a choose/when for each of your different formats and use the 
variables as needed... -->
    <xsl:value-of select="$month"/>
    <xsl:value-of select="$Date/s:Separator"/>
    <xsl:value-of select="$day"/>
    <xsl:value-of select="$Date/s:Separator"/>
    <xsl:value-of select="$year"/>
  </xsl:template>

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

</xsl:stylesheet>

again, I just whipped this together and realized, it's time to head home for 
me...so if you don't get an answer by tomorrow morning I'll try to 
understand your javascript better then...

Benjamin


From: "Murali Korrapati" <murali(_dot_)korrapati(_at_)reval(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: RE: [xsl] badly need xsl help(prob with accessing child nodes in 
the script)
Date: Mon, 13 Oct 2003 15:27:08 -0400

Hi guys,
  Lets try to forget everything that started b'cos of me. We are here to 
help each other, not to fight.
Try to be as friendly as possible and help each other.

I tried the namespace(you can see that in my modified code in init() 
function) as Michael Kay said. But I have no luck.
This is what I am exactly doing and want to do. Can anybody give any 
suggestions?

Thanks.

xml:

<data xmlns="urn:schemas.abcd-com:layers">
      <Rpt_Inven xmlns="urn:schemas.abcd-com:RepInven" >
              <Inven>
                      <EffectiveDate>2003-05-30</EffectiveDate>
              </Inven>
              <Inven>
                      <EffectiveDate>2003-07-30</EffectiveDate>
              </Inven>
      </Rpt_Inven>
      <RegionalOptions xmlns="urn:schemas.abcd-com:Static">
              <Date>
                      <Calendar>
                      <TwoDigitYear>79</TwoDigitYear>
                      </Calendar>
                      <Separator>/</Separator>
                      <ShortFormat>3</ShortFormat>
              </Date>
              <Currency>
                      <DecimalSymbol>.</DecimalSymbol>
                      <GroupingSymbol>,</GroupingSymbol>
                      <DigitGrouping>2</DigitGrouping>
              </Currency>
              <Locale>1033</Locale>
      </RegionalOptions>
</data>


xsl :

<xsl:stylesheet version="1.0" xmlns=""
xmlns:lay="urn:schemas.abcd-com:layers"
xmlns:rep="urn:schemas.abcd-com:RepInven"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:scrdt="urn:schemas.abcd-com:datatypes"
xmlns:stat="urn:schemas.abcd-com:Static">
      <xsl:output method="xml" encoding="UTF-8"/>
      <xsl:template match="lay:data">
              <_Ctrl>
                      <xsl:call-template name="callInit">
                              <xsl:with-param name="regOptNode" 
select="stat:RegionalOptions"/>
                      </xsl:call-template>
                      <xsl:for-each 
select="rep:Rpt_Inven/rep:Inven/rep:EffectiveDate">
                              <_Item>
                                      <xsl:call-template name="genItem">
                                              <xsl:with-param name="attrNode" 
select="."/>
                                      </xsl:call-template>
                              </_Item>
                      </xsl:for-each>
              </_Ctrl>
      </xsl:template>
       <xsl:template name="callInit">
               <xsl:param name="regOptNode"/>
               <xsl:value-of select="scrdt:init($regOptNode)"/>
               <xsl:value-of select="scrdt:getValue($attrNode)"/>
      </xsl:template>
      <xsl:template name="genItem">
              <xsl:param name="attrNode"/>
              <xsl:value-of select="scrdt:getValue($attrNode)"/>
      </xsl:template>
<msxsl:script language="JScript" implements-prefix="scrdt"><![CDATA[
var    _xDate_ShortFormat;
var    _xDate_Separator;

function init(RegOpts)
{
      if( !RegOpts.length) return "length 0";
      var dateNode = RegOpts[0].selectSingleNode("stat:Date");
      if(dateNode != null )
      {
              _xDate_ShortFormat = dateNode.selectSingleNode("ShortFormat");
              _xDate_Separator = dateNode.selectSingleNode("Separator");
      }
      else
      {
              //It seems it is always going here.
              _xDate_ShortFormat = 5;
              _xDate_Separator = "-";
      }

      return "";
}
function getValue(nodes)
{
try
{
      var node  = nodes[0];
      var val, def;
      switch (node.nodeType)
      {
      case 1:
              val = node.text;
              def = node;
              break;
      case 2:
              val = node.nodeValue;
              def = null;
              break;
      case 3:
              val = node.nodeValue;
              def = node.parentNode;
              break;
      }

      if (def!=null)
      {
              val = xVal.formatValue(val);
      }

      return val;
}
catch(e)
{
      return e.description;
}
}
function formatValue()
{

      var     dt;
      var fmt = new RegExp("^([0-9]{4})-([0-9]{2})-([0-9]{2})$","");
      var res = dStr.match(fmt);
      if (res!=null)
      {
              var y = parseInt(res[1],10);
              var m = parseInt(res[2],10);
              var d = parseInt(res[3],10);
              dt = new Date(y,m-1,d);
              if (dt.getFullYear()!=y || dt.getMonth()!=(m-1) || 
dt.getDate()!=d)
                      return "";
      }
      else
              return "";

      if (dt!=null)
      {
              var     y       =       dt.getFullYear();
              var     m       =       dt.getMonth()   +       1;
              var     d       =       dt.getDate();
              switch(_xDate_ShortFormat)
              {
              case 1:
                      return  m.toString() + _xDate_Separator +
                                                      d.toString() + 
_xDate_Separator +
                                                      y.toString();
              case 3:
                      return  (m<10?"0":"")   +       m.toString() + 
_xDate_Separator +
                                                      (d<10?"0":"")   +       
d.toString() + _xDate_Separator +
                                                      y.toString();
              case 2:
                      return  d.toString() + _xDate_Separator +
                                                      m.toString() + 
_xDate_Separator +
                                                      y.toString();
              case 4:
                      return  (d<10?"0":"")   +       d.toString() + 
_xDate_Separator +
                                                      (m<10?"0":"")   +       
m.toString() + _xDate_Separator +
                                                      y.toString();
              case 5:
                      return  y.toString() + _xDate_Separator +
                                                      (m<10?"0":"")   +       
m.toString() + _xDate_Separator +
                                                      (d<10?"0":"")   +       
d.toString();
              default:
                      return  m.toString() + _xDate_Separator +
                                                      d.toString() + 
_xDate_Separator +
                                                      y.toString();
              }
      }
      else
              return "";
}
      ]]></msxsl:script>

</xsl:stylesheet>

    With this xml and xsl, I am expecting my out some thing like

<_Ctrl xmlns="" xmlns:lay="urn:schemas.abcd-com:layers"
xmlns:rep="urn:schemas.abcd-com:RepInven"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:scrdt="urn:schemas.abcd-com:datatypes"
xmlns:stat="urn:schemas.abcd-com:Static">
      <_Item>
              05/30/2003
      </_Item>
      <_Item>
              07/30/2003
      </_Item>
</_Ctrl>

   But it is always producing something like

   <_Ctrl xmlns="" xmlns:lay="urn:schemas.abcd-com:layers"
xmlns:rep="urn:schemas.abcd-com:RepInven"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:scrdt="urn:schemas.abcd-com:datatypes"
xmlns:stat="urn:schemas.abcd-com:Static">
      <_Item>
              2003-05-30
      </_Item>
      <_Item>
              2003-07-30
      </_Item>
</_Ctrl>


      I think this b'cos I am not able to get access to the <Date/> node
in the script function. And whenever I say RegOpts(i).firstChild , I am
getting the result I want.

Any ideas about. I am very desperate for this.

thanks,

~Mur


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


_________________________________________________________________
Surf and talk on the phone at the same time with broadband Internet access. 
Get high-speed for as low as $29.95/month (depending on the local service 
providers in your area).  https://broadband.msn.com


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list