Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 25th, 2017, 08:41 AM
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #2 (permalink)  
Old July 25th, 2017, 09:58 AM
mhkay's Avatar
Wrox Author
Points: 18,252, Level: 58
Points: 18,252, Level: 58 Points: 18,252, Level: 58 Points: 18,252, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,929
Thanks: 0
Thanked 281 Times in 276 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
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
DerbyNeal (July 25th, 2017)
Reply


Thread Tools
Display Modes

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

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

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 10:07 AM
How to copy new element at child level dishant XSLT 1 January 13th, 2012 02:24 AM
Copy element into complexType in XSLT 2BOrNot2B XSLT 3 December 20th, 2006 08:09 AM
Copy source-element to output tree der_bAUer XSLT 2 June 9th, 2006 08:17 AM



All times are GMT -4. The time now is 02:03 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.