Swastik
Karthik Shiraly wrote:Also, what exactly do you mean by B.xml is dynamic? Are the tags different in each B.xml, or are the values different?
Ulf Dittmer wrote:I think the element names are different. While that doesn't rule out using XSLT, it makes it more difficult because a different stylesheet would be needed for each different version of B.xml - that's why I think a DOM approach of some kind would be simpler.
Karthik Shiraly wrote:As an alternative solution, you can look into XSLT (Xml Stylesheet Transformation). It's a technique to transform XML data from one schema to another.
For example, if I've understood the discussion correctly, what you want to do is convert B.xml like
to A.xml with a different schema, like:
Is this what you are looking for? If so, XSLT would be a code reducing alternative to manipulating DOM and XPath in code.
Also, what exactly do you mean by B.xml is dynamic? Are the tags different in each B.xml, or are the values different?
It would be very easy to answer precisely, if you can show us the exact B.xml file(or files) you have (not the "simplified" one...that's just adding to the confusion right now) and the exact A.xml you want out of them. By exact, I mean a well formed valid XML file (like in my examples above).
Karthik Shiraly wrote:I'm still not sure what you mean by "dynamic", but looking at B.xmls in above posts, I infer that Ulf is right - the schema of each input file may be different.
If so, then as already pointed out, "...your code needs to know somehow what the relevant element names are in B, and to which elements in A they correspond".
If you don't want your code or XSLT to change in future, then you need to have with you all the possible schemas that your input files can take, go through those schemas
and see what tricks to apply to extract information.
For example, if you find that every input file you know of has an <Event> element with a single <*_id> element, and that <*_id> element value is always the event id, then
you know your logic should look for tags that match <*_id>.
Every possible input schema will require an "adapter" which has the logic to
1) Either recognize the schema and decide whether it can handle this schema, or pass on the responsibility to next adapter. Or if the app knows the source of the input xml, it can directly invoke the appropriate adapter.
2) adapter searches for required information in input file
3) adapter converts them to output schema
You can implement these adapters in 2 ways:
1) write java code to extract information using XPath APIs, and write them out using DOM APIs
or
2) write XSLT to transform information (it too uses XPath constructs) and write them out
Whichever way you choose, implementing a separate adapter for each and every input schema would be high effort - coding and maintenance - because things like the
output schema would get hardcoded into each and every adapter, either by java code or by XSL code.
So, you'll have to look at tricks to search what is common to all these input files. Some possible tricks you can use:
1. If you know that id is always in <event_id>, <asset_id>, <e_id> or some other limited set of tags, use the "or" operator:
2. To search for all tags that match regular expression like <*_id>, use the matches() operator:
3. Since output schema is same regardless of input, it would be best to have it in a single place and not duplicate it .
So, if doing all this the java way, keep the output schema writing logic in a separate module and use the above regex/OR type queries to minimize
input processing.
If doing all this using XSLT, then <xsl:include> will help to keep the output schema and input rules separated. Each input schema gets its own set of templates and
these are applied only if the node matches. Use the same XPath tricks above to minimize number of rules.
Or if the XSLT invoking program/servlet knows the source of each input file, it can pass in an approriate XSL paramter and help the transformer rules that way. The input templates can then apply
their rules only if that parameter has expected value.
1) which one is efficient - Using Java Code with Xpath or XSLT?
2) What you mean adapter is this XML adapter?
3)if i using xslt means can i able to compare tags ?
3a) how to use regular expression in xslt?
3b)can i able to send multiple parameters from java to xslt?
3c)what are all tags i need for that?
Karthik Shiraly wrote:Sorry, just realised that even the java code can be made more concise by using XPath all the way through, like this:
>
Karthik Shiraly wrote:Unfortunately, looks like you have totally misunderstood my post.
What I have shown in my post are *examples*. They were meant only to demonstrate and compare 2 *alternate* techniques of transforming one schema to another schema. Please note - they are *alternate* techniques. I don't see any benefit in combining them.
You should go through the post, understand the differences, and then apply the concepts to your own XMLs. It's not code meant to directly solve your specific problem.
In fact, I think you should stop coding, and instead analyze the problem - specifically, how many different types of input schemas you have and come up with ideas to process them. Once the ideas are clear, you can then prototype both and see which of the above method will be better for your specific problem.
Durga Roobini wrote:
Karthik Shiraly wrote:Sorry, just realised that even the java code can be made more concise by using XPath all the way through, like this:
>
Thanks alot,but i struck with some place in above coding,i write codings based on my xml and transform it to xml but it not get nodevalue my output is like this now,
<?xml version="1.0" encoding="UTF-8"?>
my java codings is
>
What's that smell? Hey, sniff this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|