Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - Identitity Template Problem - Best Practices - What order to makechanges

 
Thread Tools Search this Thread
Old 10-19-2009, 12:17 AM   #1
Default Identitity Template Problem - Best Practices - What order to makechanges


I have source XML that is 5-10 MB in size. I am transforming it from
XML to XML. I am having some issues with overwriting changes that I
have already made. I have come to the realization when using template
match you need to do things in a certain order so as to not overwrite
changes already made. For example if you first remove all currency
formatting on the entire document and then copy nodes containing
formatted currency to a new level...the currency formatting is back.

There are five things I need to do with the XML...at various levels
and in more than one place...nothing too complicated:

1) Replace element names
2) Make elements child elements by adding a brand new parent node.
3) Remove elements entirely
4) Add new elements/values
5) Remove all currency formatting

My questions are:
1) Do I work from the root element and go deeper so that I don't
overwrite...in general what is the best strategy?
2) Do I have one identity template and one primary template match that
I put all my apply-templates within so that nothing is copied to the
output until all changes are made?
3) How can I use a common template to strip currency formatting for
any section of the XML that I might also be doing other changes too?
There is probably an easier template remove formatting than the one I
am using below.

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


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

<!-- REMOVE CURRENCY FORMATTING -->
<xsl:template match="text()[starts-with(.,'$') or starts-with(.,'($')
or starts-with(.,'-$') or contains(.,',')]">

<!-- REPLACE LEADING PARENTHESIS WITH MINUS SIGN AND REMOVE COMMAS --
>


<xsl:variable name="txt" select="translate(translate
(.,',',''),'(','-')"/>

<xsl:choose>
<!--REMOVE ALL CHARACTERS EXCEPT NUMBERS, DECIMALS AND MINUS SIGNS--
>

<xsl:when test="starts-with($txt,'$') or starts-with($txt,'-$')">
<xsl:value-of select="translate($txt,translate
($txt,'0123456789.-',''),'')"/>
</xsl:when>
<!--FOR FIELDS WITHOUT DOLLAR SIGNS, TEST TO SEE IF NUMBER-->
<xsl:when test="number($txt) = number($txt)">
<xsl:value-of select="$txt"/>
</xsl:when>
<!--PASS THROUGH TEXT BECAUSE IT IS NOT A NUMBER-->
<xsltherwise>
<xsl:value-of select="."/>
</xsltherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>


johkar
  Reply With Quote
Old 10-19-2009, 02:01 AM   #2
Joe Kesselman
 
Posts: n/a
Default Re: Identitity Template Problem - Best Practices - What order tomake changes
johkar wrote:
> 1) Do I work from the root element and go deeper so that I don't
> overwrite...in general what is the best strategy?


You can't "overwrite" using XSLT. Things are output in the order, and
nesting, that the stylesheet outputs them in. Focus on what you want to
output and where you want to draw the information from. If the output
document will follow roughly the same structure as the input document, a
simple recursive tree walk via the xsl:apply-template operation and a
template for each node that needs special handling (typically with the
identity template as a catch-all for those nodes which can be copied
through unchanged) is best practice. In those places where the output
diverges from that structure, write the stylesheet logic "pull-style" --
generate the appropriately nested elements/attributes/text, with
xsl:value or attribute value templates used to explicitly retrieve the
information.

> 2) Do I have one identity template and one primary template match that
> I put all my apply-templates within so that nothing is copied to the
> output until all changes are made?


XSLT does not "make changes". It generates a new output document, which
is typically (but not guaranteed to be) written to the output as
stylesheet processing takes place. If that isn't acceptable, I'd suggest
you have the stylesheet write to a buffer of some variety -- eg, build a
DOM tree -- which you can then process atomically.

> 3) How can I use a common template to strip currency formatting for
> any section of the XML that I might also be doing other changes too?


See the xsl:call-template operation, which essentially lets you use a
named template as a subroutine for other templates.


Joe Kesselman
  Reply With Quote
Old 10-19-2009, 03:40 AM   #3
johkar
 
Posts: n/a
Default Re: Identitity Template Problem - Best Practices - What order to makechanges
On Oct 18, 8:01*pm, Joe Kesselman <keshlam.cat.nos...@verizon.net>
wrote:
> johkar wrote:
> > 1) Do I work from the root element and go deeper so that I don't
> > overwrite...in general what is the best strategy?

>
> You can't "overwrite" using XSLT. Things are output in the order, and
> nesting, that the stylesheet outputs them in. Focus on what you want to
> output and where you want to draw the information from. If the output
> document will follow roughly the same structure as the input document, a
> simple recursive tree walk via the xsl:apply-template operation and a
> template for each node that needs special handling (typically with the
> identity template as a catch-all for those nodes which can be copied
> through unchanged) is best practice. In those places where the output
> diverges from that structure, write the stylesheet logic "pull-style" --
> generate the appropriately nested elements/attributes/text, with
> xsl:value or attribute value templates used to explicitly retrieve the
> information.
>
> > 2) Do I have one identity template and one primary template match that
> > I put all my apply-templates within so that nothing is copied to the
> > output until all changes are made?

>
> XSLT does not "make changes". It generates a new output document, which
> is typically (but not guaranteed to be) written to the output as
> stylesheet processing takes place. If that isn't acceptable, I'd suggest
> you have the stylesheet write to a buffer of some variety -- eg, build a
> DOM tree -- which you can then process atomically.
>
> > 3) How can I use a common template to strip currency formatting for
> > any section of the XML that I might also be doing other changes too?

>
> See the xsl:call-template operation, which essentially lets you use a
> named template as a subroutine for other templates.


Thanks for the reply. I think my main issue is that I was using push
style ALL the time and using separate templates out of the XML
structure sequence. I think I need to mix in push style when
appropriate. I am familiar with call-template but still have some
confusion on how one would use it to remove the currency formatting on
any text() that meets the criteria. Can you provide any more
guidance?



johkar
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kodak Easyshare - manual order probs John Bailey General Help Related Topics 0 01-08-2009 04:37 PM
Why are TV shows on DVD Out Of Order? mhadley DVD Video 3 09-20-2007 11:56 PM
Re: storage problem SilverSurfer A+ Certification 6 06-09-2005 07:58 PM
Re: Virus Problem ** Help!** David BlandIII A+ Certification 1 03-02-2004 06:00 PM
Re: Serious Computer Problem hootnholler A+ Certification 1 11-24-2003 12:18 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46