my xml looks like
- <ItemMaster>
- <ItemMasterHeader>
+ <ItemID>
+ <ItemStatus>
+ <UserArea>
- <Classification Type="HOMOLOGATION CLASS">
- <Codes>
<Code>E</Code>
</Codes>
</Classification>
+ <Classification Type="LP">
+ <Classification>
- <Classification Type="BRAND">
- <Codes>
<Code>002</Code>
</Codes>
</Classification>
Yhe full xml is here http://www.speedyshare.com/MgCCA/download/ItemMaster-2.xml
I need to fetch the value of Classification with attribute TYPE= "BRAND" but with below code, it only fetchs the classification with attribute TYPE = "HOMOLOGATION CLASS" which I dont want since I am calling for "BRAND". I tried to apply LASTMOVE but dosent work. Please tell me where I am wrong.
I have to fetch other values also like codes inside the type -"LP" also.
DECLARE rResource REFERENCE TO InputRoot.XMLNSC.*:SyncItemMaster.*:DataArea.*:ItemMaster.*:ItemMasterHeader[1];
SET rowCnt = rowCnt+1;
DECLARE LineCount INTEGER 1;
WHILE LASTMOVE(rResource) = TRUE DO
SET OutputRoot.XMLNSC.root.row[rowCnt].product_Info.TyreBrandCd = THE (SELECT ITEM FIELDVALUE(T) FROM itemMaster.*:ItemMasterHeader[LineCount].*:Classification.*:Codes.*:Code AS T WHERE FIELDVALUE(itemMaster.*:ItemMasterHeader[LineCount].*:Classification.(XMLNSC.Attribute)Type) = 'BRAND');
SET LineCount = LineCount + 1;
MOVE rResource NEXTSIBLING REPEAT TYPE NAME;
END WHILE;
RETURN TRUE;
END;
Thanks
TRIED with below suggested code
Here are trace logs
2013-05-10 18:32:27.218385 7732 UserTrace BIP2537I: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': Executing statement ''SET temp = THE (SELECT T.Classification AS :Classification FROM myref AS T WHERE FIELDVALUE(T.Classification.(XMLNSC.Attribute)Type) = 'BRAND');'' at ('.WMB_9D1_PROD_SUB00_001.Main', '22.3').
2013-05-10 18:32:27.218393 7732 UserTrace BIP2538I: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': Evaluating expression ''THE (SELECT T.Classification AS :Classification FROM myref AS T WHERE FIELDVALUE(T.Classification.(XMLNSC.Attribute)Type) = 'BRAND')'' at ('.WMB_9D1_PROD_SUB00_001.Main', '22.14').
2013-05-10 18:32:27.218400 7732 UserTrace BIP2572W: Node: 'WMB_9D1_PROD_SUB00_001.9D1_PROD': ('.WMB_9D1_PROD_SUB00_001.Main', '22.14') : Finding one and only SELECT result.
2013-05-10 18:32:27.218427 7732 UserTrace BIP2539I: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': Evaluating expression ''myref'' at ('.WMB_9D1_PROD_SUB00_001.Main', '22.48'). This resolved to ''myref''. The result was ''ROW... Root Element Type=16777216 NameSpace='' Name='ItemMasterHeader' Value=NULL''.
2013-05-10 18:32:27.218437 7732 UserTrace BIP2539I: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': Evaluating expression ''XMLNSC.Attribute'' at ('.WMB_9D1_PROD_SUB00_001.Main', '22.94'). This resolved to ''XMLNSC.Attribute''. The result was ''1095266992384''.
2013-05-10 18:32:27.218446 7732 UserTrace BIP2540I: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': Finished evaluating expression ''FIELDVALUE(T.Classification.(XMLNSC.Attribute)Type)'' at ('.WMB_9D1_PROD_SUB00_001.Main', '22.65'). The result was '''HOMOLOGATION CLASS'''.
2013-05-10 18:32:27.218454 7732 UserTrace BIP2539I: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': Evaluating expression ''FIELDVALUE(T.Classification.(XMLNSC.Attribute)Type) = 'BRAND''' at ('.WMB_9D1_PROD_SUB00_001.Main', '22.117'). This resolved to '''HOMOLOGATION CLASS' = 'BRAND'''. The result was ''FALSE''.
2013-05-10 18:32:27.218461 7732 UserTrace BIP2569W: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': ('.WMB_9D1_PROD_SUB00_001.Main', '22.14') : WHERE clause evaluated to false or unknown. Iterating FROM clause.
2013-05-10 18:32:27.218469 7732 UserTrace BIP2570W: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': ('.WMB_9D1_PROD_SUB00_001.Main', '22.14') : There were no items in the FROM clause satisfying the WHERE clause.
2013-05-10 18:32:27.218503 7732 UserTrace BIP2567I: Node 'WMB_9D1_PROD_SUB00_001.9D1_PROD': Assigning NULL to ''temp'', thus deleting it.
Try this:
declare temp ROW;
SET temp = THE (SELECT T.Classification FROM rResource AS T WHERE FIELDVALUE(T.Classification.(XMLNSC.Attribute)Type) = 'BRAND');
OutputRoot.XMLNSC.root.row[rowCnt].product_Info.TyreBrandCd = temp.code;
I'm not sure what's the kind of mapping you're looking for. Assuming that what you want is for the (unique) 'Code', on the 'Classification' with the right attribute on each 'ItemMasterHeader', to be present in the output inside separate 'row' folders, here's the code:
CREATE PROCEDURE ExtractTyreCodes() BEGIN
DECLARE rOutput REFERENCE TO OutputRoot;
DECLARE rResource REFERENCE TO InputRoot.XMLNSC.*:SyncItemMaster.*:DataArea.*:ItemMaster;
CREATE FIELD OutputRoot.XMLNSC.root AS rOutput;
IF LASTMOVE(rResource) THEN
SET rOutput.row[] = SELECT
THE(SELECT C.*:Codes.*:Code AS TyreBrand
FROM T.*:Classification[] AS C
WHERE C.(XMLNSC.Attribute)Type = 'BRAND') AS product_Info
FROM rResource.*:ItemMasterHeader[] AS T;
END IF;
END;
Starting from this message:
<SyncItemMaster>
<DataArea>
<ItemMaster>
<ItemMasterHeader>
<ItemID/>
<ItemStatus/>
<UserArea/>
<Classification Type="HOMOLOGATION CLASS">
<Codes>
<Code>E</Code>
</Codes>
</Classification>
<Classification Type="LP"/>
<Classification/>
<Classification Type="BRAND">
<Codes>
<Code>002</Code>
</Codes>
</Classification>
</ItemMasterHeader>
<ItemMasterHeader>
<ItemID/>
<ItemStatus/>
<UserArea/>
<Classification Type="HOMOLOGATION CLASS">
<Codes>
<Code>F</Code>
</Codes>
</Classification>
<Classification Type="LP"/>
<Classification/>
<Classification Type="BRAND">
<Codes>
<Code>005</Code>
</Codes>
</Classification>
</ItemMasterHeader>
</ItemMaster>
</DataArea>
</SyncItemMaster>
You get this message:
<root>
<row>
<product_Info>
<TyreBrand>002</TyreBrand>
</product_Info>
</row>
<row>
<product_Info>
<TyreBrand>005</TyreBrand>
</product_Info>
</row>
</root>
This generates a 'row' folder for each 'ItemMasterHeader', puts inside of each a 'product_Info' folder, and inside of that one puts the code from the 'Classification' of (Attribute) 'Type' = 'BRAND'.
Hope this helps. Regards,
Related
I have written the below piece of code to pick a row from web table to process it. The code picks/opens the first row successfully, however, when it is trying to double click the second row, it errors out with an error "Unspecified error". The web table has 7 rows, nevertheless, it is failing. Can some one point out what could be the reason for the failure please?
Set objDesc = Description.Create
objDesc("micclass").Value = "WebTable"
objDesc("html tag").Value = "Table"
Set objList = Browser("3 - Employee Assign Benefits").Page("3 - Employee Assign Benefits").Frame("APPFRAMEWORK").ChildObjects(objDesc)
oCount = objList.Count
For k =0 To oCount - 1
colName = objList(k).GetRoproperty("cols")
If Trim(colName) = 21 Then 'This is the right table
objList(k).Highlight
rows= objList(k).GetROProperty("rows")
For i = 1 To rows
x = objList(k).ChildItem(i,1,"WebElement",0).GetROProperty("abs_x")
y = objList(k).ChildItem(i,1,"WebElement",0).GetROProperty("abs_y")
Wait(2)
Set obj = CreateObject("Mercury.DeviceReplay")
obj.MouseDblClick x,y,LEFT_MOUSE_BUTTON
Call ClickVerifyElement(Browser("3 - Employee Assign Benefits").Page("3 - Employee Assign Benefits").Frame("APPFRAMEWORK").WebEdit("ben_elig_dte"), "ben_elig_dte", "WebEdit", Parameter("elig_date"))
Call ClickVerifyElement(Browser("3 - Employee Assign Benefits").Page("3 - Employee Assign Benefits").Frame("APPFRAMEWORK").WebList("emp_state_withd_opt_decode"), "emp_state_withd_opt_decode", "WebList", Parameter("withhold_ind"))
Browser("3 - Employee Assign Benefits").Page("3 - Employee Assign Benefits").Frame("APPFRAMEWORK").WebButton("Save").Click
Next
Exit For
End If
Next
company_name = 'google'
browser.get('https://m.tianyancha.com/search?key=&checkFrom=searchBox')
ele = browser.find_element_by_xpath("//input[#id='live-search']")
ele.clear()
ele.send_keys(company_name, Keys.ENTER)
name = browser.find_element_by_xpath(
"//div[#class='new-border-bottom pt5 pb5 ml15 mr15'][1]//a[#class='query_name in-block']/span/em")
if name.text:
if name.text == company_name:
check = '1'
else:
check = '0'
else:
check = '0'
the error is :
NoSuchElementException: Message: no such element: Unable to locate
element: {"method":"xpath","selector":"//div[#class='new-border-bottom
pt5 pb5 ml15 mr15'][1]//a[#class='query_name in-block']/span/em"}
Your relative Xpath is wrong.
name = browser.find_element_by_xpath(
"//div[#class='new-border-bottom pt5 pb5 ml15 mr15'][1]//a[#class='query_name in-block']/span/em"
you cannot have // two times in your xpath. // means relative from the element you start with.
Check your Xpath for name.
I'm runnning the code that should parse xml and add it to my table. XML code is loaded from variable "s". Sample of my xml is:
<?xml version="1.0" encoding="utf-8"?><response>
<item>
<id>7294478</id>
<type>ad</type>
<stats list="true">
<period>
<day>2012-12-26</day>
<spent>132.00</spent>
<impressions>93315</impressions>
<clicks>4</clicks>
<reach>38039</reach>
</period>
<period>
<day>2012-12-27</day>
<impressions>7237</impressions>
<reach>4332</reach>
</period>
<period>
<day>2012-12-28</day>
<impressions>571</impressions>
<reach>452</reach>
</period>
</stats>
</item>
</response>
That's the part of code i'm trying to run:
MERGE INTO VK_AD_STATS r
USING (SELECT hmn.STATS_ID,
hmn.STATS_type,
items.STATS_day,
items.STATS_spent,
items.STATS_impressions,
items.STATS_clicks,
items.STATS_reach,
items.STATS_video_views,
items.STATS_video_views_half,
items.STATS_video_views_full,
items.STATS_video_clicks_site,
items.STATS_join_rate
FROM XMLTABLE('response/item' passing (SELECT xmltype(s) resp FROM dual)
COLUMNS STATS_ID NUMBER path '/item/id',
STATS_type VARCHAR2(2000) path '/item/type',
STATS_XML xmltype path '/item/stats/period'
)hmn ,
XMLTABLE('period' passing hmn.STATS_XML
COLUMNS STATS_day VARCHAR2(2000) path '/period/day',
STATS_spent NUMBER path '/period/spent',
STATS_impressions NUMBER path '/period/impressions',
STATS_clicks NUMBER path '/period/clicks',
STATS_reach NUMBER path '/period/reach',
STATS_video_views NUMBER path '/period/video_views',
STATS_video_views_half NUMBER path '/period/video_views_half',
STATS_video_views_full NUMBER path '/period/video_views_full',
STATS_video_clicks_site NUMBER path '/period/_video_clicks_site ',
STATS_join_rate NUMBER path '/period/join_rate'
) items) proc
ON (r.STATS_ID = proc.STATS_ID and r.STATS_day = proc.STATS_day)
WHEN MATCHED THEN UPDATE SET r.STATS_type = proc.STATS_type,
r.STATS_spent = proc.STATS_spent,
r.STATS_impressions = proc.STATS_impressions,
r.STATS_clicks = proc.STATS_clicks,
r.STATS_reach = proc.STATS_reach,
r.STATS_video_views = proc.STATS_video_views,
r.STATS_video_views_half = proc.STATS_video_views_half,
r.STATS_video_views_full = proc.STATS_video_views_full,
r.STATS_video_clicks_site = proc.STATS_video_clicks_site,
r.STATS_join_rate = proc.STATS_join_rate
WHEN NOT MATCHED THEN INSERT (r.STATS_ID,
r.STATS_type,
r.STATS_day,
r.STATS_spent,
r.STATS_impressions,
r.STATS_clicks,
r.STATS_reach,
r.STATS_video_views,
r.STATS_video_views_half,
r.STATS_video_views_full,
r.STATS_video_clicks_site,
r.STATS_join_rate
)
VALUES (proc.STATS_ID,
proc.STATS_type,
proc.STATS_day,
proc.STATS_spent,
proc.STATS_impressions,
proc.STATS_clicks,
proc.STATS_reach,
proc.STATS_video_views,
proc.STATS_video_views_half,
proc.STATS_video_views_full,
proc.STATS_video_clicks_site,
proc.STATS_join_rate
);
COMMIT;
But it raises ORA-01722: invalid number
Extract the spent column as text and then convert it specifying the NLS_NUMERIC_CHARCTERS to use as decimal separator:
SELECT hmn.STATS_ID,
hmn.STATS_type,
items.STATS_day,
TO_NUMBER(
items.STATS_spent,
'99999999999999999999D99',
'NLS_NUMERIC_CHARACTERS='',.'''
) AS stats_spent,
items.STATS_impressions,
items.STATS_clicks,
items.STATS_reach,
items.STATS_video_views,
items.STATS_video_views_half,
items.STATS_video_views_full,
items.STATS_video_clicks_site,
items.STATS_join_rate
FROM XMLTABLE(
'/response/item'
PASSING XMLTYPE(s)
COLUMNS STATS_ID NUMBER path '/item/id',
STATS_type VARCHAR2(2000) path '/item/type',
STATS_XML xmltype path '/item/stats/period'
) hmn,
XMLTABLE(
'/period'
PASSING hmn.STATS_XML
COLUMNS STATS_day VARCHAR2(2000) path '/period/day',
STATS_spent VARCHAR2(23) path '/period/spent',
STATS_impressions NUMBER path '/period/impressions',
STATS_clicks NUMBER path '/period/clicks',
STATS_reach NUMBER path '/period/reach',
STATS_video_views NUMBER path '/period/video_views',
STATS_video_views_half NUMBER path '/period/video_views_half',
STATS_video_views_full NUMBER path '/period/video_views_full',
STATS_video_clicks_site NUMBER path '/period/_video_clicks_site',
STATS_join_rate NUMBER path '/period/join_rate'
) items
Your problem isn't XML related at all - this:
<spent>132.00</spent>
doesn't parse as a number if your NLS settings use , as a decimal separator.
Change your NLS settings to use . as decimal separator, e.g. by running this PL/SQL block:
begin
DBMS_SESSION.SET_NLS ('NLS_LANGUAGE' ,'AMERICAN');
DBMS_SESSION.SET_NLS ('NLS_TERRITORY','AMERICA');
end;
and your code should work.
I am storing settings into a config field as:
schema "teams" do
field :owner_id, :integer
field :is_base_team, :boolean
field :config, :map
has_many :team_users, {"team_user", App.TeamUser}
end
So, I need to build query that reads parameter see_owner within config as:
teams_users =
from(t in Team, where: t.owner_id == ^user_id and fragment("?->>'see_owner' == ?", t.config, true))
|> Repo.all()
|> Repo.preload(:team_users)
However, I got error:
** (Postgrex.Error) ERROR (undefined_function): operator does not exist: text == boolean
(ecto) lib/ecto/adapters/sql.ex:395: Ecto.Adapters.SQL.execute_and_cache/7
(ecto) lib/ecto/repo/queryable.ex:127: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:40: Ecto.Repo.Queryable.all/4
(app) web/channels/user_socket.ex:67: App.UserSocket.get_team_users_ids/1
(app) web/channels/user_socket.ex:40: App.UserSocket.connect/2
(phoenix) lib/phoenix/socket/transport.ex:167: Phoenix.Socket.Transport.connect_vsn/6
(phoenix) lib/phoenix/transports/websocket.ex:73: Phoenix.Transports.WebSocket.init/2
(phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:12: Phoenix.Endpoint.CowboyWebSocket.init/3
(cowboy) src/cowboy_handler.erl:64: :cowboy_handler.handler_init/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
So, how would I add to my query a checking of config.see_owner where config is a jsonb where see_owner is a boolean property ?
I am using ecto 2.0.0-rc.5
EDIT
I have fixed the postgres query as:
from(t in Team, where: t.owner_id == ^user_id and fragment("?->> 'see_owner' = '?'", t.config, true))
in the log I, it was translated as:
SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE ((t0."owner_id" = $1) AND t0."config"->> 'see_owner' = 'TRUE') [3]
but the result is empty, however, there are teams with config equals to {"see_owner": true}
EDIT2
Checking with owner_id, I got error:
sql> SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE ((t0."owner_id" = $1) AND t0."config"->> 'see_owner' = 'TRUE') [3]
[2016-06-01 17:43:33] [42804] ERROR: cannot subscript type boolean because it is not an array
I removed the owner_id check, but I got empty result:
SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE (t0."config"->> 'see_owner' = 'TRUE')
[2016-06-01 17:44:09] 0 rows retrieved in 9ms (execution: 4ms, fetching: 5ms)
I removed the owner_id check, and I used small letters for 'see_owner' = 'TRUE' as 'see_owner' = 'true', I got the results without errors:
sql> SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE (t0."config"->> 'see_owner' = 'true')
[2016-06-01 17:46:40] 16 rows retrieved starting from 1 in 7ms (execution: 3ms, fetching: 4ms)
so, I think that Ecto needs to translate boolean type to postrgres native query without converting it to capital letters like TRUE or FALSE as it currently does.
also, I don't know why native query with owner_id got error, as I copied the resulted native query from logs, and it should be working correctly..
The problem here is your conditional, == is not valid in PostgreSQL.
Try this:
from(t in Team, where: t.owner_id == ^user_id and fragment("?->>'see_owner' = ?", t.config, true))
I'm trying to create a flow in Mule that allow queries in a table with a dynamic where-statement.
The idea is to send a map of parameters with zero or more entries and then build a query string from that.
I use hsqldb for testing and get some strange errors about unexpected token.
Here is the flow:
<script:transformer name="extractParameters">
<script:script file="extractParameters.groovy" engine="groovy"/>
</script:transformer>
<flow name="datafetch.flow">
<vm:inbound-endpoint address="vm://datafetch" exchange-pattern="request-response"/>
<enricher target="#[variable:where-statement]">
<transformer ref="extractParameters"/>
</enricher>
<jdbc:outbound-endpoint connector-ref="datasource"
queryKey="parameterized-select"
exchange-pattern="request-response"
mimeType="text/plain">
<jdbc:query key="parameterized-select"
value="select * from ${datasource.table} #[variable:where-statement]"/>
</jdbc:outbound-endpoint>
<json:object-to-json-transformer name="CaseInsensitiveHashMapToJson"/>
</flow>
The enricher is a groovy script that converts a json-structure to the where-statement string:
import groovy.json.JsonSlurper
def input = new JsonSlurper().parseText(payload)
def parameters = input?.get("parameters")
def result = ""
if(parameters==null) return result
def where = parameters.inject([]) { list, entry -> list << "${entry.key}=${entry.value}"}.join(" AND ")
if (where.isEmpty()) return result
result = "where " + where
return result
Sending empty parameters results in the enricher producing an empty string, and the errors are:
1. unexpected token: ? (org.hsqldb.HsqlException)
org.hsqldb.error.Error:-1 (null)
2. unexpected token: ?(SQL Code: -5581, SQL State: + 42581) (java.sql.SQLSyntaxErrorException)
org.hsqldb.jdbc.Util:-1 (null)
3. unexpected token: ? Query: select * from TABLE1 ? Parameters: [](SQL Code: -5581, SQL State: + 42581) (java.sql.SQLException)
org.apache.commons.dbutils.QueryRunner:540 (null)
Sending one parameter (C1=0) results in the enricher producing the string "where C1=0", and the errors are:
1. unexpected token: ? (org.hsqldb.HsqlException)
org.hsqldb.error.Error:-1 (null)
2. unexpected token: ?(SQL Code: -5581, SQL State: + 42581) (java.sql.SQLSyntaxErrorException)
org.hsqldb.jdbc.Util:-1 (null)
3. unexpected token: ? Query: select * from TABLE1 ? Parameters: [where C1=0](SQL Code: -5581, SQL State: + 42581) (java.sql.SQLException)
org.apache.commons.dbutils.QueryRunner:540 (null)
So, it looks like the query expects something else than a string where I have written #[variable:where-statement]
So, what do I need to feed into the where-statement variable to make this work?
Or, is there some other way to specify this?
I tryed this way and it works:
<scripting:transformer doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[def q ="select * from myTable where 1=1 ";
if(payload.containsKey('param1')) q = q + " And mycolumn = " + payload.param1;
payload.put('dynamicWhere', q);
return payload;]]></scripting:script>
</scripting:transformer>
<outbound-endpoint exchange-pattern="request-response" connector-ref="ConnectorDb" doc:name="Database" address="jdbc://#[payload.dynamicWhere]" />
...