How to query database using saxon sql:query where db.id='value of xml attribute' - sql

I have a requirement where i need to query database using saxon sql;query by applying where clause, where database_table.ProductID should match with incoming xml input productId
Here is what i tried so far:
<sql:query connection="$sql.conn" table="table_name" column="Product_ID" row-tag="row" column-tag="col" where="Product_ID="<xsl:value-of select="ProductItem/ProductItemId/text()"/>"" />
I am getting following Exception:
SXXP0003: Error reported by XML parser: Element type "sql:query" must be followed by either attribute specifications, ">" or "/>".
I am finding it difficult to format the where clause in XPath, can any one suggest what would be correct format. Thanks in Advance.

Try to use to use an attribute value template where you put the XPath expression in curly braces {...}, as in
<sql:query connection="$sql.conn" table="table_name" column="Product_ID" row-tag="row" column-tag="col" where="Product_ID="{ProductItem/ProductItemId}""/>

Related

Extract data from XML string in Hive Table without using XPath

I am trying to use a view to extract a string(value) from a large XML string that sits in a single column in a hive table. I need to get the associated FOO_STRING_VALUE for COMPANY_ID, SALE_IND, and CLOSING_IND.
<Message>
<Header>
<FOO_STRING>
<FOO_STRING_NAME>COMPANY_ID</FOO_STRING_NAME>
<FOO_STRING_VALUE>44-1235</FOO_STRING_VALUE>
</FOO_STRING>
<FOO_STRING>
<FOO_STRING_NAME>SALE_IND</FOO_STRING_NAME>
<FOO_STRING_VALUE>Y</FOO_STRING_VALUE>
</FOO_STRING>
<FOO_STRING>
<FOO_STRING_NAME>CLOSING_IND</FOO_STRING_NAME>
<FOO_STRING_VALUE>Y</FOO_STRING_VALUE>
</FOO_STRING>
</Header>
</Message>
The XML file can have up to 50 "FOO_STRINGS" and there is no guarantee in what order they will be in so I can not use XPATH unless I have 50 xpath_string calls for each Name/Value pair and matched them up later. I am using xpath like this .....
xpath_string(xml_txt, '/Message/Header/FOO_STRING[1]/FOO_STRING_VALUE') AS String_Val_1
xpath_string(xml_txt, '/Message/Header/FOO_STRING[2]/FOO_STRING_VALUE') AS String_Val_2
xpath_string(xml_txt, '/Message/Header/FOO_STRING[3]/FOO_STRING_VALUE') AS String_Val_3
However, if the order changes than it doesn't work. I'm wondering if there is a quick way to get to find the FOO_STRING_NAME needed the and get the corresponding Value using regexp_extract() or some other way? I am not familiar with Regex so any help or suggestions would be helpful, Thank you a ton
" if the order changes than it doesn't work "
Don't use position, then.
xpath_string(xml_txt, '/Message/Header/FOO_STRING[FOO_STRING_NAME="COMPANY_ID"]/FOO_STRING_VALUE') AS String_Val_1
xpath_string(xml_txt, '/Message/Header/FOO_STRING[FOO_STRING_NAME="SALE_IND"]/FOO_STRING_VALUE') AS String_Val_2
xpath_string(xml_txt, '/Message/Header/FOO_STRING[FOO_STRING_NAME="CLOSING_IND"]/FOO_STRING_VALUE') AS String_Val_3

Allow custom attribute to a script tag element?

I can't validate script tag with attribute nomodule.
I am using odoo framework which is a python backend. It is using lxml to validate xml views or pages. I am building a view with a script tag like:
<script src="src.js" nomodule></script>
It returns an error
lxml.etree.XMLSyntaxError: Specification mandate value for attribute nomodule
However this should be valid according to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
Is there a way so that I can make the parser ignore this new attribute or I can bypass such as special data or character.
That's possibly because XML != HTML. And as you can see in the error, it's an XML error.
Is an xml attribute without a value, valid? --> your attribute isn't valid.
You need to specify the attribute value always in xml. Odoo uses xml to produce html, so you need to comply with xml rules. You can do it in this case by specifying an empty value for xml attribute like this:
<script src="src.js" nomodule=””></script>

Field value query with special character and unfiltered search returning unexpected results?

Field value query is giving unexpected results when any special character(#,=,#,$,%,^,*) is passed.
please find the 4 sample docs I have inserted in to ML.
<root>
<journalTitle>Dinesh</journalTitle>
<sourceType>JA</sourceType>
<title>title1</title>
<volume>volume0</volume>
</root>
<root>
<journalTitle>Gayari</journalTitle>
<sourceType>JA</sourceType>
<title>title1</title>
<volume>volume0</volume>
</root>
<root>
<journalTitle>Dixit</journalTitle>
<sourceType>JA</sourceType>
<title>title1</title>
<volume>volume0</volume>
</root>
<root>
<journalTitle>Singla</journalTitle>
<sourceType>JA</sourceType>
<title>title1</title>
<volume>volume0</volume>
</root>
CTS Query :
cts:search(
fn:doc(),
cts:field-value-query("Sample","######*()", ("unwildcarded")),
"unfiltered"
)
On running this query I am getting all the documents.
As per my understanding, it should return an empty sequence.
please find below the field I have created.
Field (in XML format) :
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://marklogic.com/xdmp/database">
<field-name>Sample</field-name>
<field-path>
<path>/root/journalTitle</path>
<weight>1.0</weight>
</field-path>
<word-lexicons/>
<included-elements/>
<excluded-elements/>
<tokenizer-overrides/>
</field>
Index setting:
If I will add any alphabet(s) in the search string it will give me the correct results.
Like:
##$%F
=====S
df===$d
Please help me to resolve this issue?
Try passing "exact" as an option to cts:field-value-query:
cts:search(
fn:doc(),
cts:field-value-query("Sample","######*()", ("exact")),
"unfiltered"
)
MarkLogic has an index for exact values to help in cases like this. Note it's only on when you have both case sensitive and diacritic sensitive indexes enabled (which you do). I know this works for cts:element-value-query so I expect it will for cts:field-value-query as well.
Use instead the 'exact' option in the field-value-query.
This requires the fast diacritic- and case-sensitive options, but you already have those enabled.
You can also try xdmp:plan before and after using 'exact' to see the effect on the query plan.
In the 'tokenizer overrides' option for your field, add these special character(#,=,#,$,%,^,*) as words (select 'word').
These special characters are not considered for matching by default. You need to override the default tokenizer to include them as words.
May I know what output are you expecting on passing this cts:element-word-query(xs:QName("journalTitle"),"=====S") for the above given for xmls.
Changing the one character searches to true in database config, resolves the issue in element-word-query.

Using Polymer conditional attributes with HAML

According to the documentation for Polymer expressions, you can bind data to conditionally assign an attribute using the conditional attribute syntax:
Conditional attributes
For boolean attributes, you can control whether or not the attribute
appears using the special conditional attribute syntax:
attribute?={{boolean-expression}}
That's great, but I'm using HAML where attributes are assigned to elements like this:
%element{attribute: "value"}
I can't add a question mark before that colon without HAML giving me a syntax error.
So how can I use Polymer's conditional attributes (or a functional equivalent) when I'm using HAML to generate my HTML?
One potential solution is to use the :plain filter to insert raw HTML into your HAML file:
:plain
<element attribute?={{boolean-expression}}></element>
A bit ugly, but it seems to work.
If you need to enclose some HAML-generated tags in one of these plain HTML tags, you'll need to use the :plain filter twice; once for the opening tag, and once for the closing tag.
:plain
<element attribute?={{boolean-expression}}>
-# HAML Content Here
:plain
</element>
Be sure not to indent your HAML code after the opening tag, otherwise it will become part of the "raw HTML" output and get sent as plain text to the browser instead of being processed as HAML.
The current version of HAML (4.0.6) supports conditional attributes:
%core-menu{hidden?: '{{!globals.current_series_detail}}'}
Make sure you're not putting a space before the question mark.

EclipseLink MOXy #XmlPath is returning the incorrect data for contains

I am attempting to unmarshal an XML data stream with EclipseLink MOXy with an XPath using the contains function.
When I apply the sample XPath directly to the sample XML data stream I get the correct value returned (Ref_number_1). However, when I unmarshal this using MOXy, the value that is set for refNumber1 is "Ref_number_2".
Does MOXy not support this type of XPath? It would appear that it at least is understanding it because it's not throwing an error, just setting the wrong value.
Anyone have any experience with this sort of thing? Know of a better approach?
Thanks for any help.
Marshal code:
String s = //xml stream from restful service (see xml example below);
StringReader sr = new StringReader(s);
ReferenceNumber refNum = (ReferenceNumber)marshaller.unmarshal(
new StreamSource(sr));
Member annotation:
#XmlPath("Header/ReferenceNumbers/ReferenceNumber[contains(ReferenceNumberType, \"REF_NUMBER_TYPE_1\")]/ReferenceNumber/text()")
private String refNumber1;
XML data:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Header>
<ReferenceNumbers>
<ReferenceNumber>
<ReferenceNumber>Ref_number_1</ReferenceNumber>
<ReferenceNumberType>REF_NUMBER_TYPE_1</ReferenceNumberType>
</ReferenceNumber>
<ReferenceNumber>
<ReferenceNumber>Ref_number_2</ReferenceNumber>
<ReferenceNumberType>REF_NUMBER_TYPE_2</ReferenceNumberType>
</ReferenceNumber>
</ReferenceNumbers>
</Header>
</Document>
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Currently MOXy does not support XPaths of that form. Currently a conditional check must be on an attribute contained within the element such as (see: http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html).
#XmlPath("personal-info/name[#type='first']/text()")
String firstName;
I have entered the following bug so that we improve the validation that we do on our #XmlPath annotation:
http://bugs.eclipse.org/397101
I am also interested in the use case described in your question. Would you mind entering an enhancement requires against the MOXy component for this:
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink