J
johkar
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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl
utput 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: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-->
<xsl
therwise>
<xsl:value-of select="."/>
</xsl
therwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl
<!-- 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: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-->
<xsl
<xsl:value-of select="."/>
</xsl
</xsl:choose>
</xsl:template>
</xsl:stylesheet>