 |
| 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
|
|
|
|

February 23rd, 2005, 01:44 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

February 23rd, 2005, 02:17 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

February 23rd, 2005, 02:43 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

February 23rd, 2005, 03:08 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

February 23rd, 2005, 03:26 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|
 |