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 November 9th, 2006, 07:23 AM
Registered User
 
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default transforming a XML on the basis of another XML

Hi everybody,

I'm looking for a special kind of XSLT.

This is the situation:

I'm receiving a 'default XML' from some component with an unknown format. I don't know what tags it has or how many.

Then another XML comes to me from another component. This second XML tells me how to transform the first. Let's call this second XML the 'changes XML'.
It might tell me something like this:

<DELETE xpath="/books/authors/murakami" />
<EDIT xpath="/configuration/compiler@language" newValue="python" />
<ADD xpath="//customers" nodeType="element" name="age" value="9 years" />

I still don't know what the 'default XML' looks like, except that it probably contains the nodes mentioned in the 'changes XML'.

The question is: is there a neat solution thinkable, using XSLT?

Personally I was thinking that the 'changes XML' could be made into a XSLT which then transforms the 'default XML'.

Any ideas are welcome?

 
Old November 9th, 2006, 08:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, this is actually quite a common design pattern. Think of your "changes XML" as being the specification of a transformation in some peculiar language. Then write a translator that converts this language to XSLT. The translator, of course, can be written in XSLT.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 9th, 2006, 08:45 AM
Registered User
 
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm wondering. What if my 'changes XML' would be empty, how would the XSLT for the 'default XML' look like?
Effectively this would be a XSLT that leaves the 'default XML' unchanged.
Since I'm new to XSLT, could someone show me what this 'minimalistic' XSLT should look like?

 
Old November 9th, 2006, 08:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Have a look at the identity XSLT on Google or this forum's old posts, it's basically:
Code:
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
You can add extra templates that apply to specific nodes to perform specialised tasks. E.g to delete all SOMETHING elements and their content add:
Code:
<xsl:template match="SOMETHING"/>
--

Joe (Microsoft MVP - XML)
 
Old November 9th, 2006, 09:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

To write an identity transform, the best way is to have a stylesheet containing the single template rule:

<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

This walks the tree copying every node as it goes. You can then add extra template rules to define customized behaviour for nodes that you want to modify.

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
Transforming XML From One Namespace to Another mail4kaja XSLT 1 November 10th, 2008 10:14 AM
Help needed transforming plist to XML by XSL Vartan XSLT 4 September 28th, 2007 03:11 PM
Transforming XML to XML using XSL sakreck XSLT 0 January 9th, 2007 11:48 AM
problem transforming XML using xslt micky3248 XSLT 7 August 18th, 2006 03:52 AM
Please help, xml transforming using xslt !!! daula7 XSLT 0 May 11th, 2006 01:29 PM





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