 |
| 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
|
|
|
|

January 28th, 2010, 01:19 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So does it have to do with the tool that I am using to transform?
|
|

January 28th, 2010, 01:20 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
That second document has a namespace - the xmlns="http://www.xxx.com" is its namespace. You'll need to define that in the XSLT file and use it as Martin illustrated above.
|
|

January 28th, 2010, 01:32 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Sam,
I modified, Still did not work. See below for the latest version I have:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://www.xxx.com/"
>
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:param name="dataurl" select="'books.xml'"/>
<xsl:variable name="bookdata" select="document($dataurl)"/>
<xsl:key name="k1" match="ex:book" use="ex:ID"/>
<xsl:template match="/|@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<xsl:variable name="id" select="ID"/>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:for-each select="$bookdata">
<xsl:apply-templates select="key('k1', $id)/*[starts-with(local-name(), 'NewField')]"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="NewField1">
<DateClosed>
<xsl:value-of select="."/>
</DateClosed>
</xsl:template>
</xsl:stylesheet>
Structure of the second input doc:
<books xmlns=" xmlns="http://www.xxx.com/">
<book>
<ID>1</ID>
<NewField1>2007-10-01T00:00:00.000</NewField1>
<NewField2>2007-10-01T00:00:00.000</NewField2>
<NewField3>2007-10-01T00:00:00.000</NewField3>
</book>
<book>
<ID>2</ID>
<NewField1>2007-10-01T00:00:00.000</NewField1>
<NewField2>2007-10-01T00:00:00.000</NewField2>
<NewField3>2007-10-01T00:00:00.000</NewField3>
</book>
</books>
|
|

January 28th, 2010, 01:37 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
I am afraid what you have posted as the second input doc, the
<books xmlns=" xmlns="http://www.xxx.com/">
is not even well-formed XML.
And what is the
<xsl:template match="NewField1">
in the stylesheet supposed to do? If the elements are in a namespace then that match="NewField1" will not match them as it matches elements in no namespace.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

January 28th, 2010, 01:44 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
This isn't even well-formed XML
Code:
<books xmlns=" xmlns="http://www.xxx.com/">
and it certainly doesn't tally with your statement that you removed the namespace.
Try to do some active debugging yourself rather than just bouncing back and saying things don't work. Try playing around with the code to see which parts work and which don't.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

January 28th, 2010, 01:55 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://www.xxx.com/"
>
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:param name="dataurl" select="'books.xml'"/>
<xsl:variable name="bookdata" select="document($dataurl)"/>
<xsl:key name="k1" match="ex:book" use="ex:ID"/>
<xsl:template match="/|@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<xsl:variable name="id" select="ID"/>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:for-each select="$bookdata">
<xsl:apply-templates select="key('k1', $id)/*[starts-with(local-name(), 'NewField')]"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Structure of the second input doc:
<books xmlns="http://www.xxx.com/">
<book>
<ID>1</ID>
<NewField1>2007-10-01T00:00:00.000</NewField1>
<NewField2>2007-10-01T00:00:00.000</NewField2>
<NewField3>2007-10-01T00:00:00.000</NewField3>
</book>
<book>
<ID>2</ID>
<NewField1>2007-10-01T00:00:00.000</NewField1>
<NewField2>2007-10-01T00:00:00.000</NewField2>
<NewField3>2007-10-01T00:00:00.000</NewField3>
</book>
</books>
|
|

January 28th, 2010, 02:02 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sam and Martin,
Sorry to bug you..but I really need to get this out today..so that is why I am trying hard.
Thanks for all your help
|
|

January 28th, 2010, 02:04 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Mike,
Sorry! here is my updated version with all the changes that Martin and Sam suggested:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://www.xxx.com/"
>
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:param name="dataurl" select="'books.xml'"/>
<xsl:variable name="bookdata" select="document($dataurl)"/>
<xsl:key name="k1" match="ex:book" use="ex:ID"/>
<xsl:template match="/|@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<xsl:variable name="id" select="ID"/>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:for-each select="$bookdata">
<xsl:apply-templates select="key('k1', $id)/*[starts-with(local-name(), 'NewField')]"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Structure of the second input doc:
<books xmlns="http://www.xxx.com/">
<book>
<ID>1</ID>
<NewField1>2007-10-01T00:00:00.000</NewField1>
<NewField2>2007-10-01T00:00:00.000</NewField2>
<NewField3>2007-10-01T00:00:00.000</NewField3>
</book>
<book>
<ID>2</ID>
<NewField1>2007-10-01T00:00:00.000</NewField1>
<NewField2>2007-10-01T00:00:00.000</NewField2>
<NewField3>2007-10-01T00:00:00.000</NewField3>
</book>
</books>
|
|

January 28th, 2010, 02:05 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Does the first XML input also use a namespace? That way the xsl:template match="book" would never be applied.
But you keep us guessing. Post three well-formed, minimal but complete code samples of the two XML input documents and the XSLT stylesheet so that we can run them and reproduce the result you get and then fix the stylesheet.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

January 28th, 2010, 02:08 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
In your original first input XML document 'Book' is uppercase, but in this latest incarnation of the XSLT you have <xsl:template match="book"> which is lowercase.
Also, you might have a deadline, but we don't. The less work you put in to try and debug and come up with sensible questions the less likely we are too try to be helpful, so posting as fast as you can is not necessarily always the best approach.
|
|
 |