I have around 100 plain xml files with repetetive static values. I would like to change them into dynamic by getting values from .properties file so that i can change that value in one place. Is it possible??
Example of current xmls I have :
<?xml version="1.0"?>
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
**desired change:**
**prop.properties.com**
bookName=Harry Potter
authorName=J K. Rowling
yearValue=2005
priceValue=29.99
<?xml version="1.0"?>
<bookstore>`enter code here`
//load properies file
<book category="children">
<title>${bookName}</title>
<author>${authorName}</author>
<year>${yearValue}</year>
<price>${priceValue}</price>
</book>
</bookstore>
I need to capture an XML message that is sent as part of a request from SOAPui to Wiremock Server. Upon capturing this file, I would then need to compare it against an XSD file. The hard bit is capturing the XML file, I'm not sure what methods wiremock server has that would enable this.
For example, this is the XML message I want to capture in my java application:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
What would I need to capture this?
I try to transform a XML-document which is a TCX-file from a Garmin Watch with XSL and the javax.xml.transform.Transformer. This is the document (shorten):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Running">
<Id>2014-08-23T15:36:15.000Z</Id>
<Lap StartTime="2014-08-23T11:50:34.000Z">
<TotalTimeSeconds>3181.0</TotalTimeSeconds>
<DistanceMeters>7620.0</DistanceMeters>
<MaximumSpeed>21.887998580932617</MaximumSpeed>
<Calories>386</Calories>
<AverageHeartRateBpm>
<Value>119</Value>
</AverageHeartRateBpm>
<MaximumHeartRateBpm>
<Value>167</Value>
</MaximumHeartRateBpm>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2014-08-23T11:50:34.375Z</Time>
<Position>
<LatitudeDegrees>48.17259333333333</LatitudeDegrees>
<LongitudeDegrees>10.191565</LongitudeDegrees>
</Position>
<AltitudeMeters>542.5505981445312</AltitudeMeters>
<DistanceMeters>0.0</DistanceMeters>
<HeartRateBpm>
<Value>70</Value>
</HeartRateBpm>
<SensorState>Absent</SensorState>
</Trackpoint>
[...]
</Track>
<Notes>Note</Notes>
</Lap>
<Notes>Note</Notes>
<Training VirtualPartner="false">
<Plan Type="Workout" IntervalWorkout="false">
<Name>Running</Name>
<Extensions/>
</Plan>
</Training>
<Creator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Device_t">
<Name>Polar V800</Name>
<UnitId>0</UnitId>
<ProductID>13</ProductID>
</Creator>
</Activity>
</Activities>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Application_t">
<Name>no application</Name>
</Author>
</TrainingCenterDatabase>
I use the javax.xml.transform.Transformer for this task and get a strange error. The transformer gives me the following error message:
Element "xmlns:xsi" cannot have "xmlns" as its prefix.
It belongs to the line:
<Creator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Device_t">
From my point of view, this line is valid. The given namespace is just bound to the prefix xsi. Is it necessary to configure the Transformer to detect it right or do I need to specify something in the xsl-file?
Thanks
For the following xml:
<books>
<book>
<author>Peter</author>
<title>Tales from Somewhere</title>
<data>
<version>1</version>
</data>
</book>
<book>
<author>Paul</author>
<title>Tales from Nowhere</title>
<data>
<version>2</version>
</data>
</book>
</books>
How can I get the <version> value of the book author 'Paul' above, using this type of notation for building a Java XPathExpression:
//*[local-name()='books']/*
?
I used the following question as a reference:
Get first child node in XSLT using local-name()
Thanks!
This XPath will get the version of a book where there is at an author element with the value "Paul":
//book[author="Paul"]/data/version
When run against this XML:
<books>
<book>
<author>Peter</author>
<title>Tales from Somewhere</title>
<data>
<version>1</version>
</data>
</book>
<book>
<author>Paul</author>
<title>Tales from Nowhere</title>
<data>
<version>2</version>
</data>
</book>
<book>
<author>Peter</author>
<author>Paul</author>
<title>How to write a book with a friend</title>
<data>
<version>7</version>
</data>
</book>
</books>
You get this result:
<version>1</version>
<version>7</version>
I have an xml of the following structure:
<Root>
<Sample>
<Materil material_class="book" />
<Book Name="harry" Price="8" />
<Book Name="small things" Price="9" />
<Book Name="snow" Price="10" />
</Sample>
<Commodity>
<Sample>
<Materil material_class="sub" />
<Book Name="sherin" Price="8" />
<Book Name="bigthings" Price="9" />
<Book Name="leopard" Price="10" />
</Sample>
<Commodity>
<Sample>
<Materil material_class="sub" />
<Book Name="azxcv" Price="86" />
<Book Name="ddddd" Price="79" />
<Book Name="qwert" Price="810" />
</Sample>
</Commodity>
<Commodity>
<Sample>
<Materil material_class="subtwo" />
<Book Name="ratnam" Price="86" />
<Book Name="shantharam" Price="99" />
<Book Name="da vinci" Price="10" />
</Sample>
</Commodity>
</Commodity>
</Root>
Is there a way to iterate this xml based on condition like,if the material_class = "sub", iterate the Book tag below that and store the #Name and #Price . If the material_class = "book", iterate the Book tag below that. Also i want to get the length of number of /Root/Commodity/Commodity tags (in this case , it is two). Any help is appreciated. I am new to XPath.
To get the Book #Name and #Price for #material_class='sub' or #material_class='book', use this XPATH
/Root[descendant-or-self::Sample or Sample[descendant-or-self::*]]//*[Materil[#material_class='sub' or #material_class='book']]/Book
OR
//Sample[Materil[#material_class='book' or #material_class='sub']]/Book
After loading this XPATH, to print the Name use #Name and Price use #Price
To get the length of number of /Root/Commodity/Commodity tags, XPATH is
/Root/Commodity/Commodity
OR
//Commodity/Commodity
JAVA:
NodeList node = (NodeList) xpath.evaluate("/Root/Commodity/Commodity", xml, XPathConstants.NODESET);
count = node.getLength(); // OUTPUTS: 2
For your information, actually rendering XML through XSLT is much better performance and easier to implement.
<xsl:apply-templates select="//Sample[Materil[#material_class='sub']]/Book"/>
<xsl:apply-templates select="//Sample[Materil[#material_class='book']]/Book"/>
I do not have access to an XSL editor right now, so have not tested the above, but it will give you an idea. Select the Book, and constrain the Materil with the condition you want.