Get the node following a selected node - sql-server-2008

I have the following (cut down) xml in a sql column
<Values>
<Value>
<N>Test</N>
<V>FindMe</V>
</Value>
<Value>
<N>Test</N>
<V>NotMe</V>
</Value>
<Value>
<N>Other Value</N>
<V>NotMe</V>
</Value>
</Values>
I am trying to get the value of V, following the first N with a value of Test
I have tried using following-sibling, but that apparently is not supported in whatever version of XPATH Sql Server 2008 uses.
I have arrived at the following query, based on Gunther's answer here:
/Values/Value/V[. >> /Values/Value/N[. = 'Test'][1]][1]
Which when tried online, returns multiple rows, but when tried in the following SQL
SELECT
A.x,
A.x.query('/Values/Value/V[. >> /Values/Value/N[. = ''Test''][1]][1]')
FROM (SELECT CAST('<Values><Value><N>Test</N><V>FindMe</V></Value><Value><N>Other Value</N><V>NotMe</V></Value></Values>' AS XML) x) A
Has the following error
XQuery [A.x.query()]: '>>' requires a singleton (or empty sequence), found operand of type 'element(N,xdt:untyped) *'
I've tried the following variations on the query, but all have had the same error
/Values/Value/V[. >> /Values/Value/N[. = 'Test'][1]][1]
/Values/Value/V[. >> /Values/Value[N = 'Test'][1]][1]
/Values/Value/V[. >> /Values/Value[N = 'Test']/N[1]][1]

This should do it.
/Values/Value[N='Test'][1]/V

Related

Python 3 psycopg2 COPY from stdin failed: error in .read()

I am trying to apply the code found on this page, in particular part 'Copy Data from String Iterator' of the Table of Contents, but run into an issue with my code.
Since not all lines coming from the generator (here log_lines) can be imported into the PostgreSQL database, I try to filter the correct lines (here row) using itertools.filterfalse like in the codeblock below:
def copy_string_iterator(connection, log_lines) -> None:
with connection.cursor() as cursor:
create_staging_table(cursor)
log_string_iterator = StringIteratorIO((
'|'.join(map(clean_csv_value, (
row['date'],
row['time'],
row['cs_uri_query'],
row['s_contentpath'],
row['sc_status'],
row['s_computername'],
...
row['sc_substates'],
row['s_port'],
row['cs_version'],
row['c_protocol'],
row.update({'cs_cookie':'x'}),
row['timetakenms'],
row['cs_uri_stem'],
))) + '\n')
for row in filterfalse(lambda line: "#" in line.get('date'), log_lines)
)
cursor.copy_from(log_string_iterator, 'log_table', sep = '|')
When I run this, cursor.copy_from() gives me the following error:
QueryCanceled: COPY from stdin failed: error in .read() call
CONTEXT: COPY log_table, line 112910
I understand why this error happens, it is because in the test file I use there are only 112909 lines that meet the filterfalse condition. But why does it try to copy line 112910 and throw the error and not just stop?
Since Python doesn't have a coalescing operator, add something like:
(map(clean_csv_value, (
row['date'] if 'date' in row else None,
:
row['cs_uri_stem'] if 'cs_uri_stem' in row else None,
))) + '\n')
for each of your fields so you can handle any missing fields in the JSON file. Of course the fields should be nullable in the db if you use None otherwise replace with None with some default value for that field.

how to tokenize a text by nltk python

i have a text like this:
Exception in org.baharan.dominant.dao.core.nonPlanAllocation.INonPlanAllocationRepository.getAllGrid()
with cause = 'org.hibernate.exception.SQLGrammarException: could not extract ResultSet'
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
i tokenize this text with word_tokenize in python and output is:
Exception
org.baharan.dominant.dao.core.nonPlanAllocation.INonPlanAllocationRepository.getAllGrid
cause
'org.hibernate.exception.SQLGrammarException
could
extract
ResultSet'
Caused
java.sql.SQLSyntaxErrorException
ORA-00942
table
view
exist
But as you can see, the second line outputs several words that are dotted together. How to separate these as a Word?!
i use this python code:
>>> f = open('001.txt')
>>> text = [w for w in word_tokenize(f.read()) if w not in stopwords]
and In fact, I want all words to be separated like this:
Exception
org
baharan
dominant
dao
core
nonPlanAllocation
INonPlanAllocationRepository
getAllGrid
cause
'org
hibernate
exception
SQLGrammarException
could
extract
ResultSet'
Caused
java
sql
SQLSyntaxErrorException
ORA-00942
table
view
exist
f = "Exception in org.baharan.dominant.dao.core.nonPlanAllocation.INonPlanAllocationRepository.getAllGrid() \
with cause = 'org.hibernate.exception.SQLGrammarException: could not extract ResultSet' \
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist'"
s = ''
f_list = f.replace('.', ' ').split(' ')
for item in f_list:
#print(item)
s = s + ' ' + ''.join(item)+'\n'
print(s)
output
Exception
in
org
baharan
dominant
dao
core
nonPlanAllocation
INonPlanAllocationRepository
getAllGrid()
with
cause
=
'org
hibernate
exception
SQLGrammarException:
could
not
extract
ResultSet'
Caused
by:
java
sql
SQLSyntaxErrorException:
ORA-00942:
table
or
view
does
not
exist'
i found a simple way that use of RegexpTokenizer of nltk.tokenize like this:
>>> from nltk.tokenize import RegexpTokenizer
>>> tokenizer = RegexpTokenizer(r'\w+')
The output after considering remove stopwords is as follows:
Exception
org
baharan
dominant
dao
core
nonPlanAllocation
INonPlanAllocationRepository
getAllGrid
cause
org
hibernate
exception
SQLGrammarException
could
extract
ResultSet
Caused
java
sql
SQLSyntaxErrorException
ORA-00942
table
view
exist

neo4j: How to load CSV using conditional correctly?

What I am trying to do is to import a dataset with a tree data structure inside from CSV to neo4j. Nodes are stored along with their parent node and depth level (max 6) in the tree. So I try to check depth level using CASE and then add a node to its parent like this (creating a node just for 1st level so far for testing purpose):
export FILEPATH=file:///Example.csv
CREATE CONSTRAINT ON (n:Node) ASSERT n.id IS UNIQUE;
USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS
FROM {FILEPATH} AS line
WITH DISTINCT line,
line.`Level` AS level,
line.`ParentCodeID_Cal` AS parentCode,
line.`CodeSet` AS codeSet,
line.`Category` AS nodeCategory,
line.`Type` AS nodeType,
line.`L1code` AS l1Code, line.`L1Description` AS l1Description, line.`L1Name` AS l1Name, line.`L1NameAb` AS l1NameAb,
line.`L2code` AS l2Code, line.`L2Description` AS l2Description, line.`L2Name` AS l2Name, line.`L2NameAb` AS l2NameAb,
line.`L3code` AS l3Code, line.`L3Description` AS l3Description, line.`L3Name` AS l3Name, line.`L3NameAb` AS l3NameAb,
line.`L1code` AS l4Code, line.`L4Description` AS l4Description, line.`L4Name` AS l4Name, line.`L4NameAb` AS l4NameAb,
line.`L1code` AS l5Code, line.`L5Description` AS l5Description, line.`L5Name` AS l5Name, line.`L5NameAb` AS l5NameAb,
line.`L1code` AS l6Code, line.`L6Description` AS l6Description, line.`L6Name` AS l6Name, line.`L6NameAb` AS l6NameAb,
codeSet + parentCode AS nodeId
CASE line.`Level`
WHEN '1' THEN CREATE (n0:Node{id:nodeId, description:l1Description, name:l1Name, nameAb:l1NameAb, category:nodeCategory, type:nodeType})
ELSE
END;
But I get this result:
WARNING: Invalid input 'S': expected 'l/L' (line 17, column 3 (offset:
982)) "CASE level " ^
I'm aware there is a mistake at syntax.
I'm using neo4j 3.0.4 & Windows 10 (using neo4j shell running it with D:\Program Files\Neo4j CE 3.0.4\bin>java -classpath neo4j-desktop-3.0.4.jar org.neo4j.shell.StartClient).
You have several syntax errors. For example, a CASE clause cannot contain a CREATE clause.
In any case, you should be able to greatly simplify your Cypher. For example, this might suit your needs:
USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS
FROM {FILEPATH} AS line
WITH DISTINCT line, ('l' + line.Level) AS prefix
CREATE (:Node{
id: line.CodeSet + line.ParentCodeID_Cal,
description: line[prefix + 'Description'],
name: line[prefix + 'Name'],
nameAb: line[prefix + 'NameAb'],
category: line.Category,
type: line.Type})

sqldf query returns 0 values

I am having an issue running a basic query on a sample dataset(link below)
http://kbcdn.tableausoftware.com/data/Superstore.xls
using R.
I have attached my code below.
#read file with XLConnect
path <- file.path("/Users/petergensler/Desktop/Sample - Superstore Sales.xls")
superstore <- readWorksheetFromFile(path, sheet= "Orders")
#Query
test <- sqldf("SELECT * FROM superstore WHERE 'Product Sub-Category' = 'Appliances'",)
test
The query executes fine, but it returns the following results:
[1] Row.ID Order.ID Order.Date Order.Priority Order.Quantity Sales
[7] Discount Ship.Mode Profit Unit.Price Shipping.Cost Customer.Name
[13] Province Region Customer.Segment Product.Category Product.Sub.Category Product.Name
[19] Product.Container Product.Base.Margin Ship.Date
<0 rows> (or 0-length row.names)
Is there something wrong with my attached packages that would be causing the query to run wrong, or is it something with my data? the column I am querying on seems to be fine, as it is a type character, and specifying a literal string should match the values(unless their is trailing whitespace), correct?
I am running R on Mac OS X 10.11.5 with the following session info:
session_info()
Session info -------------------------------------------------------------------------------------------
setting value
version R version 3.3.0 (2016-05-03)
system x86_64, darwin13.4.0
ui RStudio (0.99.896)
language (EN)
collate en_US.UTF-8
tz America/Chicago
date 2016-06-08
I have also attached my packages attached to the current session as well.
https://drive.google.com/open?id=0Bxhxg_yftHNubEc4NUZTUVoxa0E
Thanks for your help!
Following up on what G. Grothndieck said (use brackets for column names) I ran this and it worked for me:
#Query
test <- sqldf(x = "SELECT * FROM superstore WHERE [Product.Sub.Category] = 'Appliances'")
test
Most methods of reading in Data frames change spaces and hyphens in column names into . so you need to update that part of it.

xquery exception [XPTY0019]

I am using the following xml file (users_doc.xml)
<users>
<user trusted="false">
<userid>vsony7#vt.edu</userid>
<password>sony</password>
</user>
<user trusted="false">
<userid>shivi</userid>
<password>shivi</password>
</user>
<user trusted="false">
<userid>xyz</userid>
<password>xyz</password>
</user>
</users>
I am running the following xquery: (Here $doc_name=users_doc, $userid=xyz)
declare variable $doc_name as xs:string external;
declare variable $userid as xs:string external;
let $users_doc := doc($doc_name)/users
return delete node $users_doc/user/userid=$userid/..
I am trying to find a given node <userid>xyz</userid> and if the user exists I would like to delete its parent node
<user trusted="false">
<userid>xyz</userid>
<password>xyz</password>
</user>
But, when I run this query I get the following exception:
Exception in thread "main" java.io.IOException: Stopped at line 5, column 51:
[XPTY0019] Context node required for ..; xs:string found.
How do I fix this ?
Thanks,
Sony
From http://www.w3.org/TR/xquery/#ERRXPTY0019
err:XPTY0019
It is a type error if the result of a step (other than
the last step) in a path expression
contains an atomic value.
Let's look at your expression:
$users_doc/user/userid=$userid/..
The left term of the last step is a node set comparison:
$users_doc/user/userid=$userid
So, it will result in true or false boolean value (an atomic value). Of course, you can't select the parent::node() of true or false...
You want this expression:
$users_doc/user[userid=$userid]