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 July 25th, 2017, 08:41 AM
Registered User
 
Join Date: Jul 2017
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Copy all with an element exception

Hi,

This is my first posting on this site and hopefully I've understood the posting requirements correctly. My XML example below is a small extract of a more complicated file. I need to copy the XML via XSLT except for 1 element (StoreNumber) that is based on other factors.

XML example:

Code:
<Store>
	<StoreHeader>
		<StoreNumber>1234</StoreNumber>
		<StoreCountry>AUS</StoreCountry>
	</StoreHeader>
	<Transaction>
		<Item>12123</Item>
		<Code>02341CD</Code>
		<Sale>-10.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>17652</Item>
		<Code>02342CH</Code>
		<Sale>-50.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>00947</Item>
		<Code>02343CD</Code>
		<Sale>-50.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>16467</Item>
		<Code>02344CD</Code>
		<Sale>-79.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>01184</Item>
		<Code>02345CH</Code>
		<Sale>-7.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
</Store>

Exception Rules:

A.
If ALL ‘Sale’ elements have a negative value (a ‘-‘ prefix) then the StoreNumber value must change to 1234#1

Code:
<StoreNumber>1234#1</StoreNumber>

B.
If the value of the ‘StoreCountry’ element is ‘AUS’ then the StoreNumber value must change to 1234AUS

Code:
<StoreNumber>1234AUS</StoreNumber>
C.
If ALL ‘Sale’ elements have a negative value AND the ‘StoreCountry’ value is ‘AUS’ then the StoreNumber value should be 1234#1AUS

Code:
<StoreNumber>1234#1AUS</StoreNumber>

Otherwise the StoreNumber value remains the same.


My 2 basic XSLT approaches:

1
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" encoding="utf-8" indent="yes"/>

    <!-- Identity template : copy all text nodes, elements and attributes -->   
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:when test="name()='StoreNumber'">
		
		<!-- Code needed  -->
		
    </xsl:when>

</xsl:stylesheet>

OR

2
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
	
  <xsl:template match="*">
	  <xsl:choose>
		<xsl:when test="name()='StoreNumber'">
			<xsl:element name="StoreNumber">
				
				<!-- Code needed  -->
				
			</xsl:element>
		</xsl:when>
		<xsl:otherwise>
			<!-- -->
			<xsl:call-template name="copy"/>
			<!-- -->
		</xsl:otherwise>
	</xsl:choose>
  </xsl:template>
  
  <!-- Copy an element and all of its attributes  -->
  <xsl:template name="copy">
    <xsl:copy>
      <xsl:for-each select="@*">
        <xsl:copy/>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
In this example the XML output needs to be similar to the original file with the one exception (StoreNumber):

XML output file
Code:
<Store>
	<StoreHeader>
		<StoreNumber>1234#1AUS</StoreNumber>
		<StoreCountry>AUS</StoreCountry>
	</StoreHeader>
	<Transaction>
		<Item>12123</Item>
		<Code>02341CD</Code>
		<Sale>-10.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>17652</Item>
		<Code>02342CH</Code>
		<Sale>-50.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>00947</Item>
		<Code>02343CD</Code>
		<Sale>-50.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>16467</Item>
		<Code>02344CD</Code>
		<Sale>-79.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
	<Transaction>
		<Item>01184</Item>
		<Code>02345CH</Code>
		<Sale>-7.000</Sale>
		<Date>2017-04-20</Date>
	</Transaction>
</Store>
Maybe my approach is incorrect but I don’t know how to access other elements during the name()=’StoreNumber’ condition (I have also tried a recursive template). Is it possible to use the exception rule here or do I need to transform the whole XML? Can anyone assist please?

Again, my XML is a simplified example consisting of many other elements.

Thanks in advance,

Last edited by DerbyNeal; July 25th, 2017 at 08:48 AM..
 
Old July 25th, 2017, 09:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're almost there. You need an identity template rules that by default copies things unchanged. Then you add higher-priority template rules for any exceptions. For example, to delete StoreNumber you would add the rule

Code:
<xsl:template match="StoreNumber"/>
Your other rules translate directly to template rules in the same way, but with more complex patterns. For example

If the value of the ‘StoreCountry’ element is ‘AUS’ then the StoreNumber value must change to 1234AUS

becomes

Code:
<xsl:template match="StoreNumber[../StoreCountry='AUS']">
  <StoreNumber>1234AUS</StoreNumber>
</xsl:template>
and

If ALL ‘Sale’ elements have a negative value (a ‘-‘ prefix) then the StoreNumber value must change to 1234#1

becomes

Code:
<xsl:template match="StoreNumber[not(//Sale[number(.)>=0])]">
  <StoreNumber>1234#1</StoreNumber>
</xsl:template>
You may need to add priority attributes to indicate which rule takes precedence when more than one applies.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
DerbyNeal (July 25th, 2017)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy xml element content to another javi_h XSLT 0 March 19th, 2015 04:40 PM
copy-of without the element tag 4everJang XSLT 1 January 7th, 2014 11:07 AM
How to copy new element at child level dishant XSLT 1 January 13th, 2012 03:24 AM
Copy element into complexType in XSLT 2BOrNot2B XSLT 3 December 20th, 2006 09:09 AM
Copy source-element to output tree der_bAUer XSLT 2 June 9th, 2006 08:17 AM





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