Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2005, 01:44 PM
Authorized User
 
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help with a search,convert and replace

I'm new to xml and I'm trying to do a search, convert date format from 02-Jan-2005 to 01022005, then insert the converted date back into the original xml document as a final result. I've been using a <xsl:choose> statement to test for the date string, which works.. and testing for the string value of the date node gets the results I want, but I can't get my final output right (template priorities seem to be the issue). Any cut and dried methods for this type of routine out there?

 
Old February 23rd, 2005, 02:17 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you show us your code, then we can tell you where it's wrong.

Though why anyone would choose to use a date format of mmddyyyy completely baffles me! (You must come from the only country that still uses feet and inches...)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 23rd, 2005, 02:43 PM
Authorized User
 
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply.... yes, I know the date thing is crazy, but the application we are submitting this document to is funny about date format. Here's what I have so far... the NEED_DATE contains the date to be converted and I'm importing copysource.xsl to copy the original documents contents, (although it's not taking precedence over the following templates like I read it would). My output is nothing but the converted data in one long string.




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

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









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

<xsl:template match="MRP_DETAIL">



<xsl:choose>
        <xsl:when test="substring(NEED_DATE,4,3)
='Jan'">
                                                 <xsl:value-of select
="'01'"/>

                                     </xsl:when>

                                     <xsl:when test="substring(NEED_DATE,4,3)
='Feb'">
                                                 <xsl:value-of select
="'02'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Mar'">
                                                 <xsl:value-of select
="'03'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Apr'">
                                                 <xsl:value-of select
="'04'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='May'">
                                                 <xsl:value-of select
="'05'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Jun'">
                                                 <xsl:value-of select
="'06'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Jul'">
                                                 <xsl:value-of select
="'07'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Aug'">
                                                 <xsl:value-of select
="'08'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Sep'">
                                                 <xsl:value-of select
="'09'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Oct'">
                                                 <xsl:value-of select
="'10'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Nov'">
                                                 <xsl:value-of select
="'11'"/>
                                     </xsl:when>
                                     <xsl:when test="substring(NEED_DATE,4,3)
='Dec'">
                                                 <xsl:value-of select
="'12'"/>
                                     </xsl:when>


                                                              </xsl:choose>

            <xsl:value-of select="substring(NEED_DATE,1,2)"/>

            <xsl:value-of select="substring(NEED_DATE,8,4)"/>


</xsl:template>


</xsl:stylesheet>

 
Old February 23rd, 2005, 03:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's not clear what your source tree actually looks like, but I'm guess that the problem is that when you process an MRP_DETAIL element you not only want to convert the NEED_DATE element, you want to do some other things as well...

Perhaps you want to copy the MRP_DETAIL element and its other children (this is wild guessing...)

Try putting the date conversion logic in a template that matches NEED_DATE rather than MRP_DETAIL - which means that substring(NEED_DATE, x, y) becomes substring(., x, y) - and perhaps putting an xsl:copy in the template so that you get some tags around the converted data.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 23rd, 2005, 03:26 PM
Authorized User
 
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a snippet of what I'm trying to transform... it's a purchase order file. I found a recursive, find and replace routine...I'm thinking I can just specify the node I want to target and call a replace template. It's the mechanics that are getting me... I must be missing something simple. Thanks for the input.

Original document excerpt:

<MRP_DATA><MRP_DETAIL><REV>A</REV><DOC_TYPE>PURORD</DOC_TYPE><PO_NUMBER>K7949242</PO_NUMBER><ITEM_NUMBER>1</ITEM_NUMBER><SCHEDULE_LINE>1</SCHEDULE_LINE><ITEM_PRICE>0.06</ITEM_PRICE><ITEM_UOM>1</ITEM_UOM><QTY>228000</QTY><CURRENCY>USD</CURRENCY><AMT>13680.00</AMT><NEED_DATE>13-Jan-2005</NEED_DATE><DOCK_DATE>03-Jan-2005</DOCK_DATE><MRP_ACTION>CONFIRM</MRP_ACTION><BUYER_COMMENT></BUYER_COMMENT><BUYER_ACTION></BUYER_ACTION><SUPPLIER_ACTION></SUPPLIER_ACTION><SUPPLIER_COMMENT></SUPPLIER_COMMENT><SUPPLIER_RESCHED_DATE></SUPPLIER_RESCHED_DATE><DOWNLOAD_TIMESTAMP>07-Dec-2004 16:33</DOWNLOAD_TIMESTAMP></MRP_DETAIL><MRP_DETAIL><REV>A</REV><DOC_TYPE>PURORD</DOC_TYPE><PO_NUMBER>K7999811</PO_NUMBER><ITEM_NUMBER>1</ITEM_NUMBER><SCHEDULE_LINE>1</SCHEDULE_LINE><ITEM_PRICE>0.06</ITEM_PRICE><ITEM_UOM>1</ITEM_UOM><QTY>64000</QTY><CURRENCY>USD</CURRENCY><AMT>3840.00</AMT><NEED_DATE>17-Jan-2005</NEED_DATE><DOCK_DATE>07-Jan-2005</DOCK_DATE><MRP_ACTION>DEFER</MRP_ACTION><BUYER_COMMENT></BUYER_COMMENT><BUYER_ACTION></BUYER_ACTION><SUPPLIER_ACTION></SUPPLIER_ACTION><SUPPLIER_COMMENT></SUPPLIER_COMMENT><SUPPLIER_RESCHED_DATE></SUPPLIER_RESCHED_DATE><DOWNLOAD_TIMESTAMP>07-Dec-2004 16:33</DOWNLOAD_TIMESTAMP></MRP_DETAIL><MRP_DETAIL></MRP_DATA>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace a text in XML file AyatKh XML 17 May 23rd, 2012 09:05 PM
Search and Replace Using SQL Oracle i9 Corey Beginning PHP 1 April 28th, 2007 09:58 AM
Global Search&Replace Text in all fields in sql db buddyz SQL Server 2000 10 September 14th, 2006 08:24 AM
Need help with a search,convert and replace mhkay XSLT 1 February 23rd, 2005 05:09 PM
RTF Search & Replace PC User Access 0 June 16th, 2004 01:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.