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 September 24th, 2007, 02:15 AM
Registered User
 
Join Date: Sep 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help on Processing two xml and updating one a

Hi,

The doubt i have is:

XPaths of the src.xml are given in the Report.xml as the value (PCData) of the node <change>. Task is to get the xpath(value of change node) from Report.xml and then try to find a match in src.xml.
once the match is found, then we need to generate a new xml(say output.xml). The node in src.xml that matched Xpath in Report.xml has to be appended with an attribute "Change" and the value of this will be from Report.xml.

Briefly saying, we need to take the value (xpath) of the node "change" and value of the attribute "type" from Report.xml and then update the src.xml with these values.

i have given an example of Src.xml and Report.xml and the expected output.xml also.


src.xml

<?xml version="1.0" encoding ="ISO-8859-1"?>
<Products>
<product name="OS - Windows 2000 " >
<region name="south" sales="20M"/>
<region name="north" sales="10M"/>
</product>
<product name="OS - Windows NT" >
<region name="south" sales="10M"/>
<region name="north" sales="15M"/>
</product>
<product name="OS - Windows XP" >
<region name="south" sales="35M"/>
<region name="north" sales="15M"/>
</product>
</Products>

Report.xml

<xpath xmlns:saxon="http://saxon.sf.net/ "
xmlns:deltaxml="http://www.deltaxml.com/ns/well-formed-delta-v1 ">
<change type="WFmodify" >Products/product[1]/region[1]</change>
<change type="unchanged" >Products/product[3]/region[1]</change>
</xpath>

The output.xml should look like,

<?xml version="1.0" encoding ="ISO-8859-1"?>
<Products>
<product name="OS - Windows 2000 " >
<region name ="south" sales="20M" Change=" WFmodify"/>
<region name="north" sales="10M"/>
</product>
<product name="OS - Windows NT" >
<region name="south" sales="10M"/>
<region name="north" sales="15M"/>
</product>
<product name="OS - Windows XP" >
<region name= "south" sales="35M" Change="unchanged"/>
<region name="north" sales="15M"/>
</product>
</Products>

Waiting for the reply

Thanks in Advance


Regards,
Ananda Kumar M
 
Old September 24th, 2007, 03:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

To evaluate an XPath that is constructed dynamically or read dynamically from a source document, you need a product that offers an extension such as saxon:evaluate() or the EXSLT dyn:evaluate(). It can't be done using standard XSLT, either 1.0 or 2.0.

The fact that the Saxon namespace appears in one of your XML documents (even though it's unused) suggests you are using Saxon, in which case you can use saxon:evaluate().

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 25th, 2007, 01:28 AM
Registered User
 
Join Date: Sep 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I am new to XSLT. I know nothing about saxon. please, help me on this.

Regards,
Ananda Kumar M
 
Old September 25th, 2007, 04:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I don't have time to help you. I can't tell where you are stuck, other than right at the beginning, and you're trying to do something which isn't exactly "hello world". I could code it for you but that's not a good way of teaching. All I can suggest is you start at the beginning, learn the language with the help of a good book, do the exercises starting at the simple ones, and then come back when you have a specific question.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 25th, 2007, 06:46 AM
Registered User
 
Join Date: Sep 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Actually, i tried with my hand. I will exactly say where i am stuck

Following is the xslt i am trying with.

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://whatever" version = "2.0">
<xsl:output method="xml"/>
<xsl:variable name="tempdoc" select="document('Report.xml')/xpath" />

    <xsl:template match="/">

       <xsl:for-each select="$tempdoc/change">
             <xsl:variable name="xpath" select="." />
             <xsl:variable name="xvalue" select="@type"/>
             <xsl:call-template name="refer"/>
        </xsl:for-each>

    </xsl:template>
        <xsl:template name="refer" match="* | @*">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="$xpath">
              <xsl:element name="{name()}">
               <xsl:copy-of select="@*"/>
               <xsl:attribute name="Change">$xvalue</xsl:attribute>

            </xsl:element>
            </xsl:template>

</xsl:stylesheet>


I am trying to get the value (xpath) from Report.xml and try to match it with the Src.xml by giving the <xsl:template match="$xpath">. i am getting error on this, saying, Unexpected token in pattern, found "$".

Logic i have is, for every value(xpath) from Report.xml, I am trying to find a match in Src.xml. If i find a match then i have to append the attribute accordingly.


Regards,
Ananda Kumar M
 
Old September 25th, 2007, 10:29 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

As Michael pointed out you can't evaluate XPath strings without going outside standard XSLT. Instead of having a template that matches $xpath, an invalid syntax, you can read the XPath from the change element as you are doing and then use saxon:evaluate($xpath) to store the result of executing the XPath on the main document. You will need to use the Saxon processor and include the Saxon namespace bound to the 'saxon' prefix.

--

Joe (Microsoft MVP - XML)
 
Old September 25th, 2007, 02:14 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There are a few things wrong here.

      <xsl:variable name="xpath" select="." />
      <xsl:variable name="xvalue" select="@type"/>
      <xsl:call-template name="refer"/>

You declare two variables here and don't actually use them. You can't refer to variables declared in one template in a different one. You need to use parameters:

   <xsl:call-template name="refer">
      <xsl:with-param name="xpath" select="." />
      <xsl:with-param name="xvalue" select="@type"/>
   </xsl:call-template>

and then declare the params in the called template using <xsl:param>. Except that "." is passed to a called template implicitly, so you don't really need these params, because you can refer to their values in the called template as "." and "@type" respectively.

<xsl:template match="$xpath">

There's a temptation to imagine that wherever you use any construct in XSLT or XPath, you can replace it with a variable. I've even seen people try to write <$x>...</$x> to create an element whose name is in a variable. That's not the way it works. You can only use a variable in a place where you could write a value - for example a string in quotes, like 'abc', or a number like 7. In particular, you can't hold expressions or patterns in variables. If you have a variable containing a string and the string is actually an XPath expression, then you can evaluate the expression using saxon:evaluate() or similar constructs in other processors. There's no equivalent for match patterns.

<xsl:attribute name="Change">$xvalue</xsl:attribute>

In XSLT 2.0 you can write

<xsl:attribute name="Change" select="$xvalue"/>

In 1.0 it has to be

<xsl:attribute name="Change">
  <xsl:value-of select="$xvalue"/>
</xsl:attribute>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 12th, 2007, 05:35 AM
Registered User
 
Join Date: Sep 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I tried the way you said.
I am getting some error.
First, pls let me know the way i wrote the code is correct.

The xslt is:
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://whatever" version = "2.0">
<xsl:output method="xml"/>
<xsl:variable name="tempdoc" select="document('TempXPath.xml')/xpath" />

 <xsl:template match="/">
     <xsl:for-each select="$tempdoc/change">
          <xsl:call-template name="call">
             <xsl:with-param name="xpath" select="."/>
             <xsl:with-param name="xvalue" select="@type"/>
         </xsl:call-template>
     </xsl:for-each>
 </xsl:template>

 <xsl:template name="call">
     <xsl:param name="xpath"/>
     <xsl:param name="xvalue"/>
     <xsl:call-template name="refer"/>
 </xsl:template>

<xsl:template name="refer" match="* | @*">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>


<xsl:template match="saxon:evaluate($xpath)">
       <xsl:param name="xvalue"/>
        <xsl:element name="{name()}">
              <xsl:attribute name="change" select="$xvalue"/>
           <xsl:copy-of select="@*"/>
           <xsl:apply-templates/>
     </xsl:element>
      </xsl:template>
</xsl:stylesheet>


Following is the error I am getting,
XTSE0340: XSLT Pattern syntax error at char 0 on line 34 in {saxon:evaluate($}:
    The only functions allowed in a pattern are id() and key()

Pls, help me on this.

Regards,
Ananda Kumar M





Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing ENTITY and NOTATION lines in xml mrame XSLT 2 August 1st, 2008 01:24 PM
Processing MS Dynamics XML Envelope Neal XSLT 2 May 22nd, 2008 05:57 AM
Updating an XML Schema johnnycorpse ASP.NET 1.0 and 1.1 Professional 0 April 5th, 2007 11:18 AM
updating an xml file Yustme C# 0 June 1st, 2006 02:58 PM
Need Help in Updating XML file. hbcontract XML 2 October 28th, 2003 04:34 PM





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