Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML 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 May 1st, 2006, 10:24 PM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath API and DOM Document object

Hi All,
Does anybody have an idea about the following problem that I came across.
If we add an element to the DOM document object and later in the code using an XPath expression if we search through for the same new element added in the same DOM document object by XPath API;it does not resolve the expression and returns null otherwise DOM document has that new element added before. I think this is an XPath API bug...

 
Old May 2nd, 2006, 02:28 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well you don't say what XML parser you're using so it's hard to tell but it sounds like it would be a bug.

If you are using MSXML then it's unlikely, don't you think that this has been tried before and would have been spotted by now?

Show a reproducible sample of what you are trying to achieve.

--

Joe (Microsoft MVP - XML)
 
Old May 2nd, 2006, 04:08 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Either your code is incorrect, in which case we are hardly likely to be able to spot the bug without seeing the code; or there is a bug in the product, in which case we are hardly likely to be able to help without knowing what product you are using.

(PLEASE, everyone, THINK before posting)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 2nd, 2006, 09:49 AM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am sorry if I am not being precise to my question.

Below is the program I am using to to my stuff...
Please let me know if you have any questions regarding below sample code... its pretty much straight forward... I have commented in the code where I am having problem in...


package test;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.transform.TransformerException;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.SAXParser;
import org.apache.xml.utils.PrefixResolver;
import org.apache.xpath.XPath;
import org.apache.xpath.XPathAPI;
import org.apache.xpath.XPathContext;
import org.apache.xpath.objects.XObject;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * @author nandalrx
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class ElementAttributeTest
{
    /**
     * @param xPathString a String containing the Xpath of the element to be
     * found
     * @returns the corresponding Nodes as and array.
     * Returns null if there is no such node
     * @throws XPathException
     * @throws DOMException
     */
    public static XObject getNodesAsList(
        PrefixResolver prefixResolver,
        XPathContext xPathContext,
        XPath xPath,
        int ctxtNode)
        throws TransformerException, DOMException
    {
        return xPath.execute(xPathContext, ctxtNode, prefixResolver);
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception
    {
        DOMParser domParser = new DOMParser();
        SAXParser saxParser = new SAXParser();
        InputSource is = new InputSource(new FileReader(args[0]));
        domParser.parse(is);
/*Get the document to be modified...*/
        Document document = domParser.getDocument();
/* get the parent node to be inserted with new child node...*/
        Node node = XPathAPI.selectSingleNode(document, args[1]);
/* Create an element to be inserted... */
        Element t_n = document.createElement("lndocmeta:smi");
        Attr atr = document.createAttribute("lnsmi");
        t_n.setAttribute("lnsmi", "ravi");
        Node fc = node.getFirstChild();
        fc = fc.getNextSibling();
        fc = fc.getNextSibling();
/*inserting new element...*/
        node.insertBefore(t_n, fc);
/*This is just for self verification of the modified DOM object...*/
        String processedString =
            convertDOMToString(document, new StringWriter(), "xxx");
// System.out.println(processedString);
/* if I un comment below lines... it works fine... */
// is = new InputSource(new StringReader(processedString));
// domParser.parse(is);
/* This way it finds the element added... */
        NodeList list = document.getElementsByTagName("lndocmeta:smi");
//Node smi = XPathAPI.selectSingleNode(document, "/*/lndocmeta:docinfo/lndocmeta:smi");
for(int i = 0; i < list.getLength(); i++)
{
    Node n1 = list.item(i);
    System.out.println(n1.getNodeName());
    NamedNodeMap nnm = n1.getAttributes();
    for(int j = 0; j < nnm.getLength(); j++)
    {
        Node n2 = nnm.item(j);
        System.out.println(n2.getNodeName() + " : " + n2.getNodeValue());
    }
}
/* it find the an element that is around an element added... and I can traverse to element added to...*/
Node smi = XPathAPI.selectSingleNode(document, "/*/lndocmeta:docinfo/lndocmeta:lnlni");
if(smi != null)
{
    System.out.println(smi.getNodeName());
    smi = smi.getNextSibling();
    System.out.println(smi.getNodeName());
}
/* Here it sucks it does not find the new element added in the Modified DOM document object... it returns null... don't know why???
I personally think it is a bug...*/
smi = XPathAPI.selectSingleNode(document, "/*/lndocmeta:docinfo/lndocmeta:smi");

if(smi != null)
{
    System.out.println("++++++ " + smi.getNodeName());
}
    }
}


 
Old May 2nd, 2006, 10:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you don't get a reply from anyone else, try a DOM or Xerces list.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 2nd, 2006, 10:17 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I don't understand the Java API well enough to be sure but where do you define the namespace to prefix matching, e.g. lndocmeta?

Can you show a portion of the XML you start with so I can picture what's going on?

--

Joe (Microsoft MVP - XML)
 
Old May 2nd, 2006, 10:27 AM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Below is an entire xml doc..

************************************************** ************************************************** *********
<?xml version="1.0" encoding="IBM1047"?><!DOCTYPE NEWSITEM PUBLIC "//LN//NEWSITEMv01-000//EN" "D:/XOE/xml/internal/fabrication/newsitem-v01/newsitem-v01.dtd">
<NEWSITEM>
<lndocmeta:docinfo>
<lndocmeta:lnlni lnlni="42R2-B950-00SM-30GX-00000-04"/>
<lndocmeta:lnminrev lnminrev="00005"/>
<lndocmeta:dpsi lndpsi=""/>
<lndocmeta:dsi lndsi="4"/>
<lndocmeta:lndoctype lndoctypename="NEWSITEM"/>
<lndocmeta:lndoctypeversion lndoctypeversionmajor="01" lndoctypeversionminor="000"/>
<lndocmeta:lndoctypelang lndoctypelang="EN"/>
<lndocmeta:lnfilenum lnfilenum="001"/>
<lndocmeta:lnerpt lnerpt="B5A0D877"/>
<lndocmeta:lntp lntp="B5A0D8770001E89A"/>
<lndocmeta:lnadf lnadf="no"/>
<lndocmeta:lnadfnomod lnadfnomod="no"/>
</lndocmeta:docinfo>
<lnv:COPYRIGHT>
<lnv:PUB-COPYRIGHT>Copyright
<lnvxe:copyright.year year="2001">2001</lnvxe:copyright.year>
<lnvxe:copyright.holder>The Financial Times Limited</lnvxe:copyright.holder>
</lnv:PUB-COPYRIGHT>
</lnv:COPYRIGHT>
<lnv:PUB typestyle="smcaps">061030110000129999<nl/> Financial <nl/>061030110000130000</lnv:PUB>
<lnv:ARL-DEL-DATE/><lnv:ARL-RST-DATE/><lnv:PUB-DATE><lnvxe:date day="03" year="2001" month="04">April 3, 2001 Tuesday</lnvxe:date></lnv:PUB-DATE><lnv:EDITION><lnvxe:desc>Surveys CRE1</lnvxe:desc></lnv:EDITION><lnv:SECTION-INFO><lnvxe:position.section>SURVEY - CREATIVE BUSINESS </lnvxe:position.section><lnvxe:position.sequence>Pg . 8 </lnvxe:position.sequence></lnv:SECTION-INFO><lnv:LENGTH/><lnv:HEADLINE><lnvxe:hl1>SURVEY - CREATIVE BUSINESS: Only the brand is tangible </lnvxe:hl1></lnv:HEADLINE><lnv:SPEC-LIB> <lnvni:cos>#COSFTL# #INDSNP# #SICP2711# </lnvni:cos> <lnvni:nsi>%ADM01000% %ADM01040% %AD000000% </lnvni:nsi> #C100# #C102# #NENT# #TN02# #NMAR# #MNWS#</lnv:SPEC-LIB><lnv:BYLINE><lnvxe:bydesc>By PHIL ELY </lnvxe:bydesc></lnv:BYLINE><lnv:BODY-1><p>In the intangible world of digital media, the only thing that appears tangible is the brand. This may seem a bold statement, but if its true, theres a lot the virtual world and TV programme-makers can learn from the branding of fast moving consumer goods. I wasnt fortunate enough to be at Design Bridge when the UEFA Champions League brand was created nearly 10 years ago, but this was the one example of onscreen branding that attracted me to joining them. A programme brand created in 1992 that still looks good? Its a great example of an FMCG marketing mindset and a client with immense vision, because 10 years ago there was no commercial internet or interactive TV. The brief was to exploit the brand in as many ways as possible trophies, hoardings, merchandise and TV without losing sight of its core values. Designing a strong brand presence for a programme and its spin-offs is not necessarily a long and arduous process. Here Id like to introduce the notion of FMPG fast moving programme goods. The programme concept and content still needs to be of the highest quality and as compelling as ever, but these can be packaged as quickly and efficiently as fast moving consumer goods. Stick a creative mind a mind which has crafted wine labels or created new products for the childrens snacks market on to a programme identity and Ill guarantee some sparky results. Some programmes have become hugely successful even without a strong brand identity. Who Wants to be a Millionaire?, some would argue, has great brand presence. But the moment you stick the logo on a display 30mm square, the identity struggles. It is simply too complicated those swirls and pound signs dont work close up. By contrast, childrens TV has been creating strong TV brand identities for decades. A quick poll around our office revealed that practically everyone was able to describe the Blue Peter logo, which was so memorable that it could quite literally be transferred on to a badge. With the badge came other recognisable spin-offs and brand extensions, all supporting the values that make Blue Peter so successful. Yet hard-won brand values can be easily destroyed, particularly by a programmes sponsors. Coronation Street may be one of the most popular programmes on TV, but its recently lost all its brand heritage to its advertising owner, Cadburys, simply by virtue of a redesigned TV title sequence. The ubiquitous Coronation Street sign now seems almost apologetic of its red-brick roots a classic case of the sponsor trying to maximise its investment at the expense of the programme brand. Another example is Popstars. By the stage it launched, inconsistencies in its online branding, within the programme itself and in its poster campaigns were already apparent, exacerbated by an over-enthusiastic sponsor. Compulsive viewing is no longer a question of linear TV programming. No programme is attractive just on its own. What networks are hoping for are ideas that can be repackaged in as many forms as possible. And while I dont agree that every TV programme idea has the potential to be stretched beyond its original linear format, those programmes that can, and are interactive or appear via other digital media, need to be treated like every other branded product that hits the shelves and talks to consumers. Making attractive programme packages today is therefore no different from making one toothpaste more desirable than the next. There are emotional responses to an identity that are the same for TV and for FMCG brands: Is this the type of programme that feels like me? Or rather Is this something I want to be associated with? There is some opposition to all this brand-speak. Some even think that viewers and consumers have had enough. Naomi Kleins best-seller, No Logo, is the shining torch of the anti-brand world. The problem is that the visual language that weve acquired, the reliance on instant recognition and attraction from the identity of a product, is with us for good. Without it, we cannot make decisions about one product over another. So where does all this lead? While TV advertisers recognise the value of their brands, programme-makers do not. Soon enough, those advertisers will want to make their own style of branded programming because they understand it better. A brand manager responsible for soft cheese is brand-aware; they think in 360 degrees. Its time programmes hit back. ) <lnvxe:email><remotelink hrefclass="mailto">[email protected]</remotelink></lnvxe:email> Phil Ely is the director of digital media at the Design Bridge. He was formerly the creative director of Granada Broadband</p></lnv:BODY-1><lnv:LANGUAGE><lnvxe:lang.english iso639-1="EN">ENGLISH </lnvxe:lang.english></lnv:LANGUAGE><lnv:PUBLICATION-TYPE><lnvxe:desc>Newspaper</lnvxe:desc></lnv:PUBLICATION-TYPE><lnv:LN-SUBJ><lnvni:term-item><lnvni:term termSource="SUB">MARKETING</lnvni:term><lnvni:score>90%</lnvni:score></lnvni:term-item><lnvni:term-item><lnvni:term termSource="SUB">BRANDING</lnvni:term><lnvni:score>90%</lnvni:score></lnvni:term-item><lnvni:term-item><lnvni:term termSource="SUB">INTERACTIVE TELEVISION</lnvni:term><lnvni:score>76%</lnvni:score></lnvni:term-item><lnvni:term-item><lnvni:term termSource="SUB">ADVERTISING</lnvni:term><lnvni:score>74%</lnvni:score></lnvni:term-item><lnvni:term-item><lnvni:term termSource="SUB">OUTDOOR ADVERTISING</lnvni:term><lnvni:score>74%</lnvni:score></lnvni:term-item><lnvni:term-item><lnvni:term termSource="SUB">VIRTUAL REALITY</lnvni:term><lnvni:score>71%</lnvni:score></lnvni:term-item><lnvni:term-item><lnvni:term termSource="SUB">TELEVISION PROGRAMMING</lnvni:term><lnvni:score>71%</lnvni:score></lnvni:term-item></lnv:LN-SUBJ><lnv:PUB-SUBJECT><lnlit:term><lnlit:code>CN02</lnlit:code>Company Management</lnlit:term><lnlit:term><lnlit:code>CN</lnlit:code>Company News</lnlit:term><lnlit:term><lnlit:code>CN25</lnlit:code>Strategy</lnlit:term></lnv:PUB-SUBJECT><lnv:LN-CO><lnvni:term-item><lnvni:term termSource="COS">FINANCIAL TIMES LTD</lnvni:term><lnvni:score>93%</lnvni:score></lnvni:term-item><lnvni:term-item><lnvni:term termSource="COS">CADBURY SCHWEPPES PLC</lnvni:term><lnvni:score>59%</lnvni:score></lnvni:term-item></lnv:LN-CO><lnv:PUB-COMPANY><lnlit:term><lnlit:code>CADBS00000</lnlit:code>Cadbury Schweppes PLC</lnlit:term></lnv:PUB-COMPANY><lnv:LN-ORG/><lnv:LN-TS><lnvni:term-item><lnvni:term termSource="COS">CSG (NASDAQ)</lnvni:term><lnvni:score>59%</lnvni:score></lnvni:term-item></lnv:LN-TS><lnv:LN-IND><lnvni:term-item><lnvni:term termSource="COS">SIC2711 NEWSPAPER PUBLISHERS</lnvni:term></lnvni:term-item></lnv:LN-IND><lnv:PUB-INDUSTRY><lnlit:term><lnlit:code>N5418</lnlit:code>Advertising &amp; Related Services</lnlit:term></lnv:PUB-INDUSTRY><lnv:LN-PROD/><lnv:LN-PERSON/><lnv:LN-CITY><lnvni:term-item><lnvni:term termSource="COS">LONDON, ENGLAND, UK</lnvni:term><lnvni:score>75%</lnvni:score></lnvni:term-item></lnv:LN-CITY><lnv:LN-ST/><lnv:LN-COUNTRY><lnvni:term-item><lnvni:term termSource="COS">EUROPE</lnvni:term><lnvni:score>75%</lnvni:score></lnvni:term-item></lnv:LN-COUNTRY><lnv:PUB-REGION><lnlit:term><lnlit:code>GB</lnlit:code>United Kingdom</lnlit:term><lnlit:term><lnlit:code>EU</lnlit:code>European Union</lnlit:term><lnlit:term><lnlit:code>XG</lnlit:code>Europe</lnlit:term><lnlit:term><lnlit:code>XJ</lnlit:code>Western Europe</lnlit:term></lnv:PUB-REGION><lnv:DOC-ID>#A200104034C3-161-FT,0,XML,FTI 20010403S108.903#<lnv:BATCH kw="1048903507144"/></lnv:DOC-ID><lnv:EXTRACTED-TERMS/><lnv:SYS-AUDIT> #COSX030331XPX49XNI# #ENTX030331XPX34XNI# #SUBX030331XPX01XNI# #100T030331S100A01# #100T030331S102A01# #027T030331SENTA02# #027T030331SN02A02# #028T030331SMARA02# #028T030331SNWSA02#</lnv:SYS-AUDIT><lnv:REPORT-NO>FINTME2001</lnv:REPORT-NO><lnv:LOAD-DATE><lnvxe:date day="02" year="2001" month="04">April 2, 2001</lnvxe:date></lnv:LOAD-DATE></NEWSITEM>
************************************************** ************************************************** *************

 
Old May 2nd, 2006, 10:31 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Okay, and where do you set the prefix-namespace mappings in your code?

--

Joe (Microsoft MVP - XML)
 
Old May 2nd, 2006, 10:35 AM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what namespace mapping you are talking about is part of tag name I believe... I guess that should not be an issue...

Let me know if I misunderstood something...

Thanks,

 
Old May 2nd, 2006, 11:07 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

When you have an element such as lndocmeta:smi then the lndocmeta prefix is a shorthand version of the actual namespace.
When selecting these elements you usually need to let the parser know what namespace is. Similarly when creating elements like this you need to specify the full namespace somewhere.
As I said I've never used this particular XML parser so I can't swear this is the problem.

As Michael suggested perhaps amore specialised list might help.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to Click Element in DOM document [email protected] C# 2005 1 October 18th, 2007 03:38 AM
How to Click Element in DOM document [email protected] C# 1 October 18th, 2007 03:03 AM
Click event in DOM document [email protected] C# 2005 0 October 18th, 2007 12:43 AM
How to .... Dom Document namitjung PHP How-To 4 July 7th, 2005 12:17 AM
Browsing DOM in Java - Xpath? holdmykidney XML 2 October 4th, 2004 09:08 AM





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