I would like to extract only the child node "body" from a given XML file and then save it again as an XML file. How can I achieve this?
The XML file looks like this:
<books>
<book date="2019-01-04" xml:lang="de">
<title>...</title>
<body>...</body>
<copyright>...</copyright>
<categories>
<category>
<topic d="0">...</topic>
<topic d="1" label="True">...</topic>
</category>
</categories>
<authors>...</authors>
<published>...</published>
<isbn>...</isbn>
<url>...</url>
</book>
...
</books>
I already tried the function saveXML but I don't know how I can choose only one node.
I expect a XML file like this:
<books>
<book date="2019-01-04" xml:lang="de">
<body>...</body>
</book>
...
</books>
Related
Team, I need your help /expertise to retrieve node value by traversing an xml response. I would like to use this an integration middleware.
Input file sample:
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xml:base="https://api12preview.sapsf.eu:443/odata/v2/">
<title type="text">PerEmail</title>
<id>https://api12preview.sapsf.eu:443/odata/v2/PerEmail</id>
<updated>2022-11-09T13:58:27Z</updated>
<link href="PerEmail" rel="self" title="PerEmail"/>
<entry>
<id>https://api12preview.sapsf.eu:443/odata/v2/PerEmail(emailType='54139',personIdExternal='GI00152188')</id>
<title type="text"/>
<updated>2022-11-09T13:58:27Z</updated>
<author>
<name/>
</author>
<link href="PerEmail(emailType='54139',personIdExternal='GI00152188')"
rel="edit"
title="PerEmail"/>
<category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"
term="SFOData.PerEmail"/>
<content type="application/xml">
< properties>
<d:personIdExternal>GI00152188</d:personIdExternal>
<d:emailAddress>someone#test_boehringer.com</d:emailAddress>
</m:properties>
</content>
</entry>
<entry>
<id>https://api12preview.sapsf.eu:443/odata/v2/PerEmail(emailType='54139',personIdExternal='GI00453224')</id>
<title type="text"/>
<updated>2022-11-09T13:58:27Z</updated>
<author>
<name/>
</author>
<link href="PerEmail(emailType='54139',personIdExternal='GI00453224')"
rel="edit"
title="PerEmail"/>
<category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"
term="SFOData.PerEmail"/>
<content type="application/xml">
<m:properties>
<d:personIdExternal>GI00453224</d:personIdExternal>
<d:emailAddress>someone#test_boehringer.com</d:emailAddress>
</m:properties>
</content>
</entry>
<link href="https://api12preview.sapsf.eu:443/odata/v2/PerEmail?$select=emailAddress,personIdExternal&$filter=emailType%20eq%2054139&$skiptoken=eyJzdGFydFJvdyI6MTAwMCwiZW5kUm93IjoyMDAwfQ=="
rel="next"/>
</feed>
Out of this response or xml Xquery should run through all 'entry' node and pick values of node 'personIdExternal' and I'm expecting result like this
<element>
<personIdExternal>GI00152188</personIdExternal>
<personIdExternal>GI00453224</personIdExternal>
</element>
I have tried something below code earlier but it's not working here, and I suspect this is due to namespace in the source xml. My knowledge is limited in XQuery - Please help
{let $input:= /entry
for $i in $input/properties
return
<element>
<personIdExternal>{i/personIdExternal/text()}</personIdExternal>
</element>}
/entry doesn't select anything because the entry elements aren't at the top level, and they're in a namespace.
$input/properties is wrong because the properties element isn't a child of entry and it's in a namespace.
i doesn't select anything, it should be $i
personIdExternal doesn't select anything because it's in a namespace.
You just need
<element>{//*:personIdExternal}</element>
I am tryng to upload a picture and store it in exist-db but i get the next error when opening the stored picture:
Cannot open specified file: Could not recognize image encoding.
I have tryed the next code with a small adjustment for normal txt files and it works fine but not with pictures.
picture.xhtml
<?xml-model href="http://www.oxygenxml.com/1999/xhtml/xhtml-xforms.nvdl" schematypens="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<head>
<title/>
<xf:model>
<xf:instance xmlns="">
<data>
<image xsi:type="xs:base64Binary"/>
</data>
</xf:instance>
<xf:submission id="save" action="save.xquery" method="post"/>
</xf:model>
</head>
<body>
<xf:upload ref="image">
<xf:label>Upload Photo:</xf:label>
</xf:upload>
<br/>
<xf:submit submission="save">
<xf:label>Save</xf:label>
</xf:submit>
</body>
</html>
save.xquery
xquery version "3.1";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $login:=xmldb:login('xmldb:exist:///db/apps/places','admin','admin')
(: The small adjusment i refer is just to change file extension from .jpeg to .txt :)
return xmldb:store("/db/apps/places/",concat("pic",".jpeg"), util:base64-decode(request:get-data()//image))
If you want to store images to the eXist-db you should probably replace xmldb:store() with xmldb:store-as-binary().
I'm trying to format a Wordpress WXR file using XSLT so I can import it into Drupal.
I'm aware of modules for Drupal that will import WXR files but I need the flexibility that the Feeds module can give as the imported data will be imported against different content types and I'll be pulling images and other attachments into the newly created Drupal pages. With this in mind the standard WordPress Migrate just won't cut it.
So, the WXR format has Wordpress posts and attachments as separate items within the feed and links the posts an attachments using an id. Attachments can be images, files (pdf,doc etc) and are found at the xpath wp:postmeta/wp:meta_key and have values of _thumbnail_id, _wp_attached_file
What I'd like to do is take various nodes from items of type attachment and put them within the cooresponding post item, where the id links them together
A fragment of the xml to be transformed... First item is post second is attachment. The
<item>
<title>Some groovy title</title>
<link>http://example.com/groovy-example</link>
<wp:post_id>2050</wp:post_id>
<wp:post_type>page</wp:post_type>
...
...
...
<wp:postmeta>
<wp:meta_key>_thumbnail_id</wp:meta_key>
<wp:meta_value>566</wp:meta_value>
</wp:postmeta>
</item>
...
...
...
<item>
<title>My fantastic attachment</title>
<link>http://www.example.com/fantastic-attachment</link>
<wp:post_id>566</wp:post_id>
<wp:post_type>attachment</wp:post_type>
...
...
...
<wp:attachment_url>http://www.example.com/wp-content/uploads/2012/12/fantastic.jpg</wp:attachment_url>
<wp:postmeta>
<wp:meta_key>_wp_attached_file</wp:meta_key>
<wp:meta_value>2012/12/fantastic.jpg</wp:meta_value>
</wp:postmeta>
</item>
After the transform I would like
<item>
<title>Some groovy title</title>
<link>http://example.com/groovy-example</link>
<wp:post_id>2050</wp:post_id>
<wp:post_type>page</wp:post_type>
...
...
...
<wp:postmeta>
<wp:meta_key>_thumbnail_id</wp:meta_key>
<wp:meta_value>566</wp:meta_value>
<wp:meta_url>http://www.example.com/wp-content/uploads/2012/12/fantastic.jpg</wp:attachment_url>
</wp:postmeta>
</item>
Maybe, there is a better approach? Maybe merge post and attachment where the id create a link between the nodes?
I'm new to XSLT and have read a few posts on identity transforms and I think thats the correct direction but I just don't have the experience to pull of what i need, assistance would be appreciated.
It looks like I've managed to sort out a solution.
I used a number of indexes to organise the attachments. My requirements changed a little on further inspection of the XML, as there was
I changed my resulting output to be in the format of...
<item>
<title>Some groovy title</title>
<link>http://example.com/groovy-example</link>
<wp:post_id>2050</wp:post_id>
<wp:post_type>page</wp:post_type>
...
...
...
<thumbnail>
<title>Spaner</title>
<url>http://www.example.com/wp-content/uploads/2012/03/spanner.jpg</url>
</thumbnail>
<attachments>
<attachment>
<title>Fixing your widgets: An idiots guide</title>
<url>http://www.example.com/wp-content/uploads/2012/12/fixiing-widgets.pdf</url>
</attachment>
<attachment>
<title>Do It Yourself Trepanning</title>
<url>http://www.example.com/wp-content/uploads/2013/04/trepanning.pdf</url>
</attachment>
</attachments>
</item>
So using the following xsl gave me the desired result. The conditions on the indexes ensured I was selecting the correct files.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wp="http://wordpress.org/export/1.2/">
<xsl:output indent="yes" cdata-section-elements="content"/>
<!-- Setup indexes -->
<!-- Index all main posts -->
<xsl:key
name="mainposts"
match="*/item[wp:post_type[text()='post']]"
use="wp:post_id" />
<!-- Index all sub posts (posts within posts)-->
<xsl:key
name="subposts"
match="*/item[wp:post_type[text()='post'] and category[#nicename = 'documents']]"
use="category[#domain = 'post_tag']" />
<!-- Index all image thumbs -->
<xsl:key
name="images"
match="*/item[wp:post_type[text()='attachment'] and wp:postmeta/wp:meta_key[text()='_wp_attachment_metadata']]"
use="wp:post_parent" />
<!-- Index all files (unable to sort members file at the moment)-->
<xsl:key
name="attachments"
match="*/item[wp:post_type[text()='attachment'] and not(wp:postmeta/wp:meta_key = '_wp_attachment_metadata')]"
use="wp:post_parent" />
<xsl:key
name="thumbnails"
match="*/item[wp:post_type[text()='attachment']]"
use="wp:post_id" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/item/wp:post_parent[text()= 0]">
<wp:post_parent>
<xsl:value-of select="." />
</wp:post_parent>
<xsl:for-each select="key('thumbnails', ../wp:postmeta[wp:meta_key[text()='_thumbnail_id']]/wp:meta_value)">
<thumbnail>
<title><xsl:value-of select="title" /></title>
<url><xsl:value-of select="wp:attachment_url" /></url>
</thumbnail>
</xsl:for-each>
<xsl:for-each select="key('subposts', ../category[#domain = 'post_tag'])">
<attachments>
<xsl:for-each select="key('images', wp:post_id)">
<file>
<title><xsl:value-of select="title" /></title>
<url><xsl:value-of select="wp:attachment_url" /></url>
</file>
</xsl:for-each>
<xsl:for-each select="key('attachments', wp:post_id)">
<file>
<title><xsl:value-of select="title" /></title>
<url><xsl:value-of select="wp:attachment_url" /></url>
</file>
</xsl:for-each>
</attachments>
</xsl:for-each>
</xsl:template>
I'm new to xquery and is trying to read different references on using the tool. I've been trying to play around testing and trying to generate some xml format messages but this one puzzles me. Here's my xQuery code:
Sample XQuery
declare variable $requestBody as element() external;
declare function VerifyOrderDetailTransformation($requestBody as element())
as element() {
<msg>
<header>
<headtitle>This is the title</headtitle>
</header>
<dbody>
{GenerateEquipmentListNodes($requestBody)}
</dbody>
</msg>
};
declare function GenerateEquipmentListNodes($requestBody as element())
as element()* {
let $titleList := (
for $e in $requestBody//bookstore//book
let $dTitle := $e/title/text()
return
<theTitle>{$dTitle}</theTitle>
)
return
<dTitleList>
{$titleList}
</dTitleList>
};
VerifyOrderDetailTransformation($requestBody)
Sample XML
<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>
And here's the output generated by running xQuery on the XML:
Current output
<msg>
<head>
<title>This is the title</title>
</head>
<body>
<dTitleList/>
</body>
</msg>
Expected Output
<msg>
<head>
<title>This is the title</title>
</head>
<body>
<dTitleList>
<theTitle>Everyday Italian</theTitle>
<theTitle>Harry Potter</theTitle>
<theTitle>XQuery Kick Start</theTitle>
<theTitle>Learning XML</theTitle>
<dTitleList/>
</body>
</msg>
My question is, what could I have probably missed?
There is some problem with your input: You're querying this XML:
<bookstore>
<book>
<!-- snip -->
</book>
<!-- snip -->
</bookstore>
The first part of your XPath query, namely $queryBody//bookstore, looks for all descendant elements which have an element <bookstore/> below - what return an empty result. $queryBody//bookstore won't do either, as the context already is on the <bookstore/> element.
For this reason, ommit //bookstore, so your this should be $queryBody//book.
Use this function with the changed XPath in it:
declare function local:GenerateEquipmentListNodes($requestBody as element())
as element()* {
let $titleList := (
for $e in $requestBody//book
let $dTitle := $e/title/text()
return
<theTitle>{$dTitle}</theTitle>
)
return
<dTitleList>
{$titleList}
</dTitleList>
};
One more remark: You should put your own functions into local: function namespace or define your own one. Using the default namespace is discouraged and not compatible with all processors. I changed it to the local:-namespace.
i have here an xml file from my local drive.
my question is how can I convert this xml file to datatable?
can anypone help me? thanks in advance.
string filePath = "../../Sample.xml";
DataTable dt = new DataTable("Sample");
//columns
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(string));
dt.ReadXml(filePath);
GridView1.DataSource = dt;
GridView1.DataBind();
what if the XML has subitems like this one.
<table1>
<Sample>
<Sample_1 Code="1" Desc="xxx">
<Detail>
<Detail Desc1="01" Desc2="aaa" Desc3="+++" />
<Detail Desc1="02" Desc2="bbb" Desc3="--" />
<Detail Desc1="03" Desc2="ccc" Desc3=",,," />
</Details>
<Sample>
<Sample>
<Sample_2 Code="2" Desc="yyy">
<Details>
<Detail Desc1="01" Desc2="aaa" Desc3="+++" />
<Detail Desc1="02" Desc2="bbb" Desc3="--" />
<Detail Desc1="03" Desc2="ccc" Desc3=",,," />
</Details>
<Sample>
</table1>
The current version of jqGrid has supportfor this. See this example
http://www.ok-soft-gmbh.com/jqGrid/XmlWithAttributes.htm
UPDATE
Please see below the code to showing an XML file into jsGrid
HTML
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/redmond/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.3.1/css/ui.jqgrid.css" />
<style type="text/css">
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.3.1/js/i18n/grid.locale-en.js"></script>
<script type="text/javascript">
$.jgrid.no_legacy_api = true;
$.jgrid.useJSON = true;
</script>
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.3.1/js/jquery.jqGrid.src.js"></script>
<script type="text/javascript">
//<![CDATA[
/*global $ */
/*jslint browser: true, plusplus: true */
$(function () {
'use strict';
$("#ourunittb").jqGrid({
url: 'Sample.xml',
datatype: "xml",
height: 'auto',
colModel: [
{ name: 'Author', width: 150,
xmlmap: function (obj) {
return $(obj).find("author").first().text();
}
},
{ name: 'Title', width: 300, xmlmap: function (obj) {
return $(obj).find("title").first().text();
}
}
],
xmlReader: {
root: "catalog",
row: "book",
repeatitems: false
},
loadonce: true,
rowNum: 1000
});
});
//]]>
</script>
</head>
<body>
<table id="ourunittb"><tr><td/></tr></table>
</body>
</html>
SAMPLE XML FILE
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
OUTPUT