2009-01-28 19 views
5

मैं डेटाटाइम को दिनांक प्रारूप yyyy-MM-dd में बदलने की कोशिश कर रहा हूं, क्योंकि मैं xsd.exe उपकरण का उपयोग कर रहा हूं xs: दिनांक डेटाटाइप स्वचालित रूप से डेटाटाइम डेटाटाइप में बदल जाते हैं, क्योंकि .NET Framework में कोई प्रकार नहीं है जो प्रकार xs से मेल खाता है: पूरी तरह से दिनांक।एक्सएसएलटी रूपांतरण डेटाटाइम टू डेट प्रारूप

लेकिन मैं इसे

<articles> 
     <article> 
      <articleid>48992</articleid> 
      <deliverydateasked>2009-01-29T00:00:00+01:00</deliverydateasked> 
     </article> 
     <article> 
      <articleid>48993</articleid> 
      <deliverydateasked>2009-01-30T00:00:00+01:00</deliverydateasked> 
     </article> 
</articles> 

<articles> 
     <article> 
      <articleid>48992</articleid> 
      <deliverydateasked>2009-01-29</deliverydateasked> 
     </article> 
     <article> 
      <articleid>48993</articleid> 
      <deliverydateasked>2009-01-30</deliverydateasked> 
     </article> 
</articles> 

लिए एक्सएमएल कन्वर्ट करने के लिए कोशिश कर रहा है काम करने के लिए वर्तमान में मैं इस XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:template match="/"> 
    <articles> 
     <xsl:apply-templates select="article"> 
     </xsl:apply-templates> 
      </articles> 
</xsl:template> 

<xsl:template name="FormatDate"> 

    <xsl:param name="DateTime" /> 
    <xsl:variable name="date"> 
     <xsl:value-of select="substring-before($DateTime,'T')" /> 
    </xsl:variable> 

    <xsl:if test="string-length($date) != 10"> 
     <xsl:value-of select="$DateTime"/> 
    </xsl:if> 
    <xsl:if test="string-length($date) = 10"> 
     <xsl:value-of select="$date"/> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="article"> 
     <xsl:call-template name="FormatDate"> 
      <xsl:with-param name="DateTime" select="deliverydateasked"/> 
     </xsl:call-template>  
</xsl:template>  

उपयोग कर रहा हूँ नहीं मिल सकता है

क्या कोई भी एक अच्छा एक्सएसएल जानता है टी परिवर्तन।

अग्रिम

धन्यवाद मेरी कोड के उत्पादन में परिणाम

<articles /> 

उत्तर

1

Stesoc और annakata के लिए धन्यवाद मैं यह पता लगा इस कोड को मैं अब का उपयोग कर रहा है और यह सही काम करता है

<xsl:template match="*"> 
    <xsl:param name="parentElm"> 
     <xsl:value-of select="name(..)" /> 
    </xsl:param> 
    <xsl:choose> 
     <xsl:when test="local-name() = 'deliverydateasked'"> 
      <xsl:element name="deliverydateasked"> 
       <xsl:call-template name="FormatDate"> 
        <xsl:with-param name="DateTime" select="."/> 
       </xsl:call-template> 
      </xsl:element> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:element name="{local-name()}"> 
       <xsl:copy-of select="@*" /> 
       <xsl:apply-templates select="@* | node()" /> 
      </xsl:element> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:template name="FormatDate"> 
    <xsl:param name="DateTime" /> 
    <xsl:variable name="date"> 
     <xsl:value-of select="substring-before($DateTime,'T')" /> 
    </xsl:variable> 

    <xsl:if test="string-length($date) != 10"> 
     <xsl:value-of select="$DateTime"/> 
    </xsl:if> 
    <xsl:if test="string-length($date) = 10"> 
     <xsl:value-of select="$date"/> 
    </xsl:if> 
</xsl:template>  

+0

जटिल "स्थानीय-नाम()" सामान क्यों करते हैं? – Tomalak

+0

क्या tomalak ने कहा - लीवरेज xsl: प्रतिलिपि – annakata

+0

अन्नकाटा मैंने आपके पहले सुझाव की कोशिश की है परिणाम के साथ: 200 9-01 -292009-01-30 या मैंने कुछ गलत किया? लेकिन एक समारोह से बचने के लिए स्थानीय नाम() है? अवधि का समय अब ​​15-30 एमएस है – freggel

7

सच कहूं, यह मेरे लिए के बारे में सही लग रहा है है - कभी कभी एक साधारण-स्ट्रिंग काफी अच्छा है।

हालांकि, अगर आप नेट देश में रहे हैं और आप वास्तव में अतिरिक्त कार्यक्षमता नेट की आवश्यकता होगी, कर रहे है XSLT Extension Objects


संपादित करें: oic, आप एक बुनियादी लागू-टेम्पलेट्स वैचारिक समस्या मिल गया है। कोशिश यह (कॉपी और जड़ टेम्पलेट मैच पर ध्यान दें):

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

<xsl:template match="*"> 
    <xsl:copy><xsl:apply-templates /></xsl:copy> 
</xsl:template> 

<xsl:template match="deliverydateasked"> 
    <xsl:copy> 
     <xsl:call-template name="FormatDate"> 
      <xsl:with-param name="DateTime" select="."/> 
     </xsl:call-template>  
    </xsl:copy> 
</xsl:template> 

<xsl:template name="FormatDate"> 

     <xsl:param name="DateTime" /> 
     <xsl:variable name="date"> 
       <xsl:value-of select="substring-before($DateTime,'T')" /> 
     </xsl:variable> 

     <xsl:if test="string-length($date) != 10"> 
       <xsl:value-of select="$DateTime"/> 
     </xsl:if> 
     <xsl:if test="string-length($date) = 10"> 
       <xsl:value-of select="$date"/> 
     </xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

टेम्पलेट्स एक कठिन अवधारणा को जानने के लिए है, तो आप और अधिक सरल for-each के साथ शुरू से बेहतर हो सकता है, और/या यह आप कुछ के साथ कर सकता लगता है एक्सएसएलटी ट्यूटोरियल/किताबें।

+1

"प्रत्येक के लिए अधिक सरल उपयोग करने" पर: http://gregbeech.com/blogs/tech/archive/2006/08/17/using-xsl-for-each-is-almost-always-wrong.aspx ।मैं शुरुआत से इसे ठीक से करने के लिए वोट देता हूं। :) – Tomalak

+0

एक्सएसएलटी को नए शौक में पढ़ाने के व्यक्तिगत अनुभव से मैं दृढ़ता से असहमत हूं - प्रत्येक के लिए एक अनुवाद योग्य और आसानी से समझ लिया गया अवधारणा है, और यदि कुछ भी टेम्पलेट अवधारणा को समझाने के लिए एक आसान वसंतबोर्ड है "अब आपको यह मिल गया है, देखें यह कर सकता है! " – annakata

+1

लिंक टोमालक ने ग्रेग बीच लेख को दिया है लिंक बदल गया है; अब यह http://gregbeech.com/blog/using-xsl-for-each-is-almost-always-wrong – Val

6

प्रारूपण XPath में आसान एक बहुत मिल जाएगा 2.0, जिसे माइक्रोसॉफ्ट ने पिछले 8 सालों से समर्थन देने से इनकार कर दिया है। के बाद से स्वरूपण मुद्दा वास्तव में केवल नेट में XSLT के लिए लगातार है मैं एक कस्टम समारोह है, जो का उपयोग करना चाहते क्लीनर & आसान:

xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:user="http://www.tempuri.org/User"> 

    <msxsl:script implements-prefix="user" language="C#"> 
     <![CDATA[ 
      public string FormatCurrency(string amount) 
      { 
      return decimal.Parse(amount).ToString("C0"); 
      } 

      public string FormatDate(string dateValue) 
      { 
      return DateTime.Parse(dateValue).ToString("MM/dd/yyyy hh:mm"); 
      } 
      ]]> 
     </msxsl:script> 

उपयोग::

<xsl:value-of select="user:FormatDate(@transactionDate)"/> 
<xsl:value-of select="user:FormatCurrency(@amount)"/> 
स्वरूपण समारोह के साथ

XSLT

जब आप निष्पादित नेट में अपने XSLT यह बताने के लिए है कि यह विश्वास किया है (ताकि msxsl सुनिश्चित करें:। स्क्रिप्ट ब्लॉक चलेंगे

XslCompiledTransform.Load(reader, XsltSettings.TrustedXslt, null); 
संबंधित मुद्दे