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 October 16th, 2014, 08:11 AM
Registered User
 
Join Date: Oct 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Autonumber

i have this following xml

<abc xmlns="http://ACORD.org/Standards/Life/2">
<CDS id="cd1">
<Vendor>
<VendorNumber>1910def</VendorNumber>
</Vendor>
<Vendor>
<VendorNumber>1910abc</VendorNumber>
</Vendor>
<Vendor>
<VendorNumber>1910dcd</VendorNumber>
</Vendor>
</CDS>
</abc>

and i need to add numbering to vendor tag with attribute id like v1,v2,v3 shown below

<abc>
<CDS id="cd1">
<Vendor id="v1">
<VendorNumber>1910def</VendorNumber>
</Vendor>
<Vendor id="v2">
<VendorNumber>1910abc</VendorNumber>
</Vendor>
<Vendor id="v3">
<VendorNumber>1910dcd</VendorNumber>
</Vendor>
</CDS>
</abc>

i have this following xslt i have written

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="CDS/Vendor">
<Vendor id="{format-number(count(preceding::Vendor)+1,'v0')}">
<xsl:apply-templates select="@*|node()"/>
</Vendor>
</xsl:template>

</xsl:stylesheet>

i'm not able to get the result as desired if there is namespace for root element <abc>

Can anyone help me out. Thanks in advance!!!
 
Old October 16th, 2014, 08:21 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

In the stylesheet bind a prefix to the namespace e.g.
Code:
<xsl:stylesheet ... xmlns:lf="http://ACORD.org/Standards/Life/2" exclude-result-prefixes="lf">
then use that prefix anywhere you qualify element names e.g.
Code:
<xsl:template match="lf:CDS/lf:Vendor">
        <Vendor  id="{format-number(count(preceding::lf:Vendor)+1,'v0')}">
            <xsl:apply-templates select="@*|node()"/>
        </Vendor>
    </xsl:template>
That should show you how to change your code to work with namespaces, I would also suggest to use xsl: number to count and number the elements.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 16th, 2014, 10:28 AM
Registered User
 
Join Date: Oct 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin for the response,but right now i'm getting below xml while transforming

<abc xmlns="http://ACORD.org/Standards/Life/2">
<CDS id="cd1">
<Vendor xmlns="" id="v1">
<VendorNumber xmlns="http://ACORD.org/Standards/Life/2">1910def</VendorNumber>
</Vendor>
<Vendor xmlns="" id="v2">
<VendorNumber xmlns="http://ACORD.org/Standards/Life/2">1910abc</VendorNumber>
</Vendor>
<Vendor xmlns="" id="v3">
<VendorNumber xmlns="http://ACORD.org/Standards/Life/2">1910dcd</VendorNumber>
</Vendor>
</CDS>
</abc>


i do not want xmlns="" for vendor and vendorNumber tags. can you tell a solution for that
 
Old October 16th, 2014, 11:38 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Put the namespace declaration into the stylesheet:
Code:
<xsl:stylesheet ... xmlns="http://ACORD.org/Standards/Life/2" xmlns:lf="http://ACORD.org/Standards/Life/2" exclude-result-prefixes="lf">
that way the new elements that you create are in the right namespace.
Or use
Code:
<xsl:template match="lf:CDS/lf:Vendor">
  <xsl:copy>
    <xsl:attribute name="id">
      <xsl:value-of select="format-number(count(preceding::lf:Vendor)+1,'v0')"/>
    </xsl:attribute>
    <xsl:apply-templates select="@* | node()"/>
  ...
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 17th, 2014, 08:10 AM
Registered User
 
Join Date: Oct 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanq.. It worked...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Autonumber Query Brendan Bartley Access 2 July 16th, 2006 02:14 AM
AutoNumber causes problem. myself Classic ASP Professional 1 July 10th, 2006 03:35 PM
AutoNumber Hell somissac Access VBA 5 March 22nd, 2005 04:05 AM
regarding Autonumber... MuthuAL Classic ASP Databases 2 December 8th, 2004 08:04 AM
Access Autonumber chall90909 Access ASP 1 November 21st, 2004 10:26 AM





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