Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML 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 September 14th, 2006, 06:03 AM
Authorized User
 
Join Date: Sep 2006
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copy one node to another in XSL

Hi i´m sorry if the subject is not very correct

Here my XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Order xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OrderReferences>
    <BuyersOrderNumber>32761</BuyersOrderNumber>
  </OrderReferences>
  <Supplier>
    <SupplierReferences>
      <TaxNumber>500336512</TaxNumber>
    </SupplierReferences>
  </Supplier>
  <OrderLine>
    <LineNumber>1</LineNumber>
    <Product>
      <TradeUnitCode>8645028</TradeUnitCode>
    </Product>
    <Quantity>
      <Amount UOMCode="PCE">5.0000</Amount>
    </Quantity>
  </OrderLine>
  <OrderLine>
    <LineNumber>2</LineNumber>
    <Product>
      <TradeUnitCode>8645022</TradeUnitCode>
    </Product>
    <Quantity>
      <Amount UOMCode="PCE">2.0000</Amount>
    </Quantity>
  </OrderLine>
</Order>

My XSL:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version = "1.0"
    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="Order"><xsl:text/>
<xsl:value-of select="normalize-space(./OrderReferences)"/><xsl:text/>

<xsl:for-each select="./OrderLine">

<xsl:value-of select="normalize-space(LineNumber)"/><xsl:text/>
<xsl:value-of select="normalize-space(Product)"/><xsl:text/>
<xsl:value-of select="normalize-space(Quantity)"/>#013;<xsl:text/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

And my OUTPUT is:

32761186450285.0000
286450222.0000

What i want is that the node (OrderReferences - i think that is a node) is copy for all lines below.
It should be like this:

32761186450285.0000
32761286450222.0000

I´m sorry if the question is silly but i´m not very good with xsl.
Thank you very much.

 
Old September 14th, 2006, 06:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Put the value in a variable and use it repeatedly withing the for-each loop:

<xsl:template match="Order"><xsl:text/>
<xsl:variable name="ref" select="normalize-space(./OrderReferences)"/><xsl:text/>

<xsl:for-each select="./OrderLine">
<xsl:value-of select="$ref"/>
<xsl:value-of select="normalize-space(LineNumber)"/><xsl:text/>
<xsl:value-of select="normalize-space(Product)"/><xsl:text/>
<xsl:value-of select="normalize-space(Quantity)"/>#013;<xsl:text/>
</xsl:for-each>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 14th, 2006, 06:20 AM
Authorized User
 
Join Date: Sep 2006
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks mhkay you save my life.

 
Old September 14th, 2006, 09:01 AM
Authorized User
 
Join Date: Sep 2006
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version = "1.0"
    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="Order"><xsl:text/>
<xsl:variable name="ref" select='format-number(./OrderReferences, "#00")'/>
<xsl:variable name="ref1" select="normalize-space(./Supplier/SupplierReferences)"/>


<xsl:for-each select="./OrderLine">
<xsl:value-of select="$ref"/>
<xsl:value-of select="$ref1"/>
<xsl:value-of select="normalize-space(LineNumber)"/><xsl:text/>
<xsl:value-of select="normalize-space(Product)"/><xsl:text/>
<xsl:value-of select="normalize-space(Quantity)"/>#013;<xsl:text/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

mhkay sorry to bother you again how do i format this node OrderReferences to:

0032761186450285.0000
0032761286450222.0000

I only can put 0 on the right side and not in the left side.
Thank you



 
Old September 14th, 2006, 09:28 AM
Authorized User
 
Join Date: Sep 2006
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<xsl:variable name="ref" select="concat('00',normalize-space(./OrderReferences))"/>

I did it, but if there is a better way , please tell me
Thank you.

 
Old September 14th, 2006, 09:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can force OrderReferences to six digits using

format-number(OrderReferences, '000000')

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
Copy parent node and not its children bonekrusher XSLT 4 August 29th, 2007 08:44 AM
Copy all contents except current node in XSLT 2BOrNot2B XSLT 2 December 19th, 2006 06:44 PM
xsl using variable on a node Debiprasad XSLT 1 June 22nd, 2006 12:20 PM
xsl:copy copies everywhere [email protected] XSLT 1 November 11th, 2005 07:44 AM





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