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 6th, 2005, 11:46 PM
Registered User
 
Join Date: Jul 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT read through XML to transform another XML

Hi all,

Is there any way to write a XSLT that go through the whole xml (element, attribute) then create another xml? (Cannot specific any xml tag name on the xslt)

Thanks!!
Den

XML sample:

<customers>
  <customer>
    <firstname>hello</firstname>
    <lastname>world</lastname>
    <profile_info>
      <profile id="1" number="0" endDate="2005-07-07" />
      <profile id="2" number="0" endDate="2005-07-08" />
      <profile id="3" number="0" endDate="2005-07-09" />
    </profile_info>
  </customer>
</customers>

 
Old July 7th, 2005, 12:07 AM
Authorized User
 
Join Date: May 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to meetnaveen4u
Default

Please provide what output.xml u will be looking for. and sample xslt u have tried.

Thanks and Regards
Naveen.
 
Old July 7th, 2005, 12:30 AM
Registered User
 
Join Date: Jul 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi meetnaveen4u,

The output for now i want to be excatly the same as the sample i given. But in the future, i will add more optional element or attribute in XSLT so when the xml transform to another xml the optional field will exist.

Thanks!

 
Old July 7th, 2005, 01:41 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need the identity transform. There's an example of it here:
http://p2p.wrox.com/topic.asp?TOPIC_ID=21082
and it show an element being modified (monkey to new:monkey. You can also remove or add elements.



--

Joe (Microsoft MVP - XML)
 
Old July 7th, 2005, 02:49 AM
Registered User
 
Join Date: Jul 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi joefawcett,

Thank you for your information. That's what i need!!! One more question, after copy the whole original. I added the "optionalField" under the node "unsignedData", i have "Field_1" and "Field_2" under "optionalField". How can i assign the data under those fields? I cannot use <xsl:value-of select"Field_1">, because the variable is not exist in original xml.

THANKS!!!!

My Code:

<-- transform all node from original xml to new xml -->
<xsl:template match="node()|@*"/>
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<-- add "optionalField" under "unsignedData" node -->
<xsl:template match="unsignedData">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    <xsl:element name="optionalField">
      <xsl:element name="Field_1"/>
      <xsl:element name="Field_2"/>
    </xsl:element>
  </xsl:copy>
</xsl:template>


 
Old July 7th, 2005, 03:23 AM
Registered User
 
Join Date: Jul 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it!!! Thanks for all !!

 
Old July 7th, 2005, 05:05 AM
Registered User
 
Join Date: Jul 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi All,

I got another question. How can i remove one of the element after transform? Let said the element is called "profile_info"?

Thanks!!

XML Code:

<customers>
  <customer>
    <firstname>hello</firstname>
    <lastname>world</lastname>
    <profile_info>
      <profile id="1" number="0" endDate="2005-07-07" />
      <profile id="2" number="0" endDate="2005-07-08" />
      <profile id="3" number="0" endDate="2005-07-09" />
    </profile_info>
  </customer>
</customers>


XSLT Code:

<-- transform all node from original xml to new xml -->
<xsl:template match="node()|@*"/>
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>



 
Old July 7th, 2005, 05:21 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Add an empty template that matches "profile_info":
Code:
<xsl:template match="profile_info"/>
Obviously all children will be removed as well.

--

Joe (Microsoft MVP - XML)
 
Old July 7th, 2005, 08:18 PM
Registered User
 
Join Date: Jul 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe!

You Guyz are PRO!!!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
XML/XSLT Transform Error in ASP kwilliams Classic ASP XML 2 October 9th, 2007 02:26 AM
Transform SQL server 2005 XML with xslt bonekrusher XSLT 0 July 11th, 2007 07:45 PM
Can 1 xslt transform an xml doc into 2 text files Raju Sarode XSLT 7 November 3rd, 2006 04:10 PM
Transform XML with XSLT in JavaScript Mozilla Spankmaster79 Javascript 3 January 18th, 2006 07:31 AM
Transform XML to XML using XSLT Mr.D XSLT 2 September 7th, 2004 02:13 PM





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