How to search . in XPath in SonarQube - xpath

My requirement is to search "using System.Resources;" in SonarQube. I can use the following XPath to search the keyword "Resources".
//*[#tokenValue='Resources']
But if I change to this pattern, it doesn't work. I think the reason is the dot, but I have no idea how to search it in XPath. Is there an escape character for it? I use "\", but not work.
//*[#tokenValue='System.Resources']
Update: I think the reason is 'System.Resources' are separated into 2 levels in XML. But still not know how to search the pattern by XPath.
<NAMESPACE_NAME tokenValue="System" tokenLine="3" tokenColumn="6">
<NAMESPACE_OR_TYPE_NAME tokenValue="System" tokenLine="3" tokenColumn="6">
<IDENTIFIER tokenValue="System" tokenLine="3" tokenColumn="6"/>
<DOT tokenValue="." tokenLine="3" tokenColumn="12"/>
<IDENTIFIER tokenValue="Resources" tokenLine="3" tokenColumn="13"/>
</NAMESPACE_OR_TYPE_NAME>
</NAMESPACE_NAME>

The string System.Resources is spread across three different elements in your input XML. That is why an expression like
//*[#tokenValue='Resources']
can never find it, because there is no single IDENTIFIER element that has both "System" and "Resources" as attribute values. So, you have to look for something else. Why not a NAMESPACE_OR_TYPE_NAME element? Use for example:
//NAMESPACE_OR_TYPE_NAME[IDENTIFIER/#tokenValue = 'System' and IDENTIFIER/#tokenValue = 'Resources']
I am not sure whether there is any significance to the dot .in between "System" and "Resources", but this expression just ignores it.
To illustrate all this, there is an XSLT stylesheet below.
XML Input
<?xml version="1.0" encoding="UTF-8"?>
<NAMESPACE_NAME tokenValue="System" tokenLine="3" tokenColumn="6">
<NAMESPACE_OR_TYPE_NAME tokenValue="System" tokenLine="3" tokenColumn="6">
<IDENTIFIER tokenValue="System" tokenLine="3" tokenColumn="6"/>
<DOT tokenValue="." tokenLine="3" tokenColumn="12"/>
<IDENTIFIER tokenValue="Resources" tokenLine="3" tokenColumn="13"/>
</NAMESPACE_OR_TYPE_NAME>
</NAMESPACE_NAME>
Stylesheet
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8"/>
<xsl:template match="NAMESPACE_OR_TYPE_NAME[IDENTIFIER/#tokenValue = 'System' and IDENTIFIER/#tokenValue = 'Resources']">
<bingo/>
</xsl:template>
</xsl:transform>
Output
<bingo/>

Related

XML sorted list of only certain nodes

I have the following XML file. I need to print a list of only selected nodes (Total) in ascending order. I tried to use sort function, but there were some mistakes I couldn't identify and it returned everything, including values of other nodes in the initial file.
XML input:
<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
<From>
<Name>Lucy</Name>
<Country>UK</Country>
</From>
<To>
<Name>John</Name>
<Country>US</Country>
</To>
<Items>
<Position>
<Name>Table</Name>
<Total>1</Total>
</Position>
<Position>
<Name>Chair</Nr>
<Total>4</Total>
</Position>
<Position>
<Name>Cup</Name>
<Total>5</Total>
</Position>
<Position>
<Name>Box</Name>
<Total>4</Total>
</Position>
</Items>
</Invoice>
How could I get the required output using?
Any help is greatly appreciated! Thank you!
One obvious approach to generate the desired output from the given input would be using an xsl:for-each also making use of xsl:sort:
<xsl:template match="/Invoice">
<SortedTotalList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:for-each select="Positions/Position">
<xsl:sort select="Total"/>
<xsl:copy-of select="Total" />
</xsl:for-each>
</SortedTotalList>
</xsl:template>
Output is:
<?xml version="1.0"?>
<SortedTotalList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Total>1</Total>
<Total>4</Total>
<Total>4</Total>
<Total>5</Total>
</SortedTotalList>

Conditional For-Each in XSLT

Need your help to validate if below XSLT code is correct.
My input XML would look like below.
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<Batch_number>1234</Batch_number>
<Assignment>
<type>A</type>
<name>New York</name>
</Assignment>
<Assignment>
<type>A</type>
<name>Boston</name>
</Assignment>
<Assignment>
<type>A</type>
<name>Boston</name>
</Assignment>
<Assignment>
<type>A</type>
<name>Boston</name>
</Assignment>
<Assignment>
<type>B</type>
<name>Chicago</name>
</Assignment>
</Worker>
</Workers>
My output file should look like below after transformation
<?xml version="1.0" encoding="UTF-8"?>
<Update_Data>
<Records>
<Batch_number>1234</Batch_number>
<Record_Location>
<Location>
<order>1</order>
<name>New York</name>
</Location>
<Location>
<order>2</order>
<name>Boston</name>
</Location>
<Location>
<order>3</order>
<name>Boston</name>
</Location>
<Location>
<order>4</order>
<name>Boston</name>
</Location>
</Record_Location>
<Record_Location>
<Location>
<order>1</order>
<name>Chicago</name>
</Location>
</Record_Location>
</Records>
</Update_Data>
I have written following XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Update_Data>
<Records>
<Batch_number><xsl:value-of select="Workers/Worker/Batch_number"/></Batch_number>
<Record_Location>
<xsl:for-each select="Workers/Worker/Assignment[type='A']">
<Location>
<order>
<xsl:value-of select="position()"/>
</order>
<name>
<xsl:value-of select="name"/>
</name>
</Location>
</xsl:for-each>
</Record_Location>
<xsl:for-each select="Workers/Worker/Assignment[type='B']">
<Record_Location>
<Location>
<order>
<xsl:value-of select="position()"/>
</order>
<name>
<xsl:value-of select="name"/>
</name>
</Location>
</Record_Location>
</xsl:for-each>
</Records>
</Update_Data>
</xsl:template>
</xsl:stylesheet>
Requirements for transformation.
If value of is same and it's appearing multiple times in input XML, Location elements(under Record_Location tag) need to be created as many times as is exists in the file with respective values.
for .e.g. <type> is A appearing 4 times in input xml, so four <Location> elements need to be created tag should hold sequenced value.
<type> is B appearing only once, so only one element needs to be present in the output file.
Number of <Record_Location> elements in the output should be same as unique values of <type>. In my example, only two unique values are present i.e. A and B, so my output should have only two <Record_Location>
XSLT code that i have written yields expected result. But, I'm not sure if this is the efficient way to write the code for my requirement. Is there a way to consolidate the for-each conditions by not specifying type='A' or type='B' etc.. Input XML can contain multiple different types. Or is it even possible to write a code without a for-each loop
Can someone please take a look at this and suggest best approach please?
-thanks
Anoop
After incorporating Martin's suggestion in XSLT.. I got something like below.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Update_Data>
<Records>
<Batch_number>
<xsl:value-of select="Workers/Worker/Batch_number"/>
</Batch_number>
<xsl:for-each-group select="Workers/Worker/Assignment" group-by="type">
<Record_Location>
<xsl:apply-templates select="current-group()"/>
</Record_Location>
</xsl:for-each-group>
</Records>
</Update_Data>
</xsl:template>
<xsl:template match="Assignment">
<Location>
<order>
<xsl:value-of select="position()"/>
</order>
<name>
<xsl:apply-templates select="name"/>
</name>
</Location>
</xsl:template>
</xsl:stylesheet>

Select XML nodes based on matching values in other nodes

Given the following xml
<dsQueryResponse>
<Proposals>
<Rows>
<Row ID="1"/>
<Row ID="2"/>
<Row ID="3"/>
</Rows>
</Proposals>
<ProposalReviewers>
<Rows>
<Row ID="1" ProposalID="1"/>
<Row ID="2" ProposalID="1"/>
<Row ID="3" ProposalID="2"/>
</Rows>
</ProposalReviewers>
</dsQueryResponse>
What xpath expression, or XSLT transform (Xslt 1.0), will give me the following output, based on the values of attribute ProposalID?
<Rows>
<Row ID="1"/>
<Row ID="2"/>
</Rows>
I know if I'm running inside of a for-each I can use current(), but I am hoping to do this outside the for-each.
Your question can be read in many ways, and I am mostly guessing here. Still, the only logical way to return only the two Rows from the input that has three (or six), seems to be this:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="proposal" match="Proposals/Rows/Row" use="#ID" />
<xsl:template match="/dsQueryResponse">
<Rows>
<xsl:copy-of select="key('proposal', ProposalReviewers/Rows/Row/#ProposalID)"/>
</Rows>
</xsl:template>
</xsl:stylesheet>
To understand how this works, see: https://www.w3.org/TR/xslt/#key

XSL Transform extracting min and max dates using XSLT 2.0

I have the repeating structure of XML and I want to use the max() and min() function present in the xslt 2.0 to fetch the maximum and minimum dates.
I am using like max(Detail/Startdate) and it is returning me NaN as the result. Could anyone help me out here?
<Info dataSource="source">
<Detail>
<StartDate>20121211</StartDate>
<EndDate>20130112</EndDate>
</Detail>
<Detail>
<StartDate>20121211</StartDate>
<EndDate>20130112</EndDate>
</Detail>
<Detail>
<StartDate>20121211</StartDate>
<EndDate>20130112</EndDate>
</Detail>
<Detail>
<StartDate>20121218</StartDate>
<EndDate>20130114</EndDate>
</Detail>
</Info>
For the sample you posted and the XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:template match="Info">
<xsl:value-of select="max(Detail/StartDate), max(Detail/EndDate), min(Detail/StartDate), min(Detail/EndDate)"/>
</xsl:template>
</xsl:stylesheet>
Saxon 9.5 gives me 2.0121218E7 2.0130114E7 2.0121211E7 2.0130112E7 and not NaN. Obviously treating those date values as doubles is not the ideal approach so I would suggest to use something alike
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf">
<xsl:function name="mf:date" as="xs:date">
<xsl:param name="date" as="xs:string"/>
<xsl:sequence select="xs:date(concat(substring($date, 1, 4), '-', substring($date, 5, 2), '-', substring($date, 7)))"/>
</xsl:function>
<xsl:template match="Info">
<xsl:value-of select="max(Detail/StartDate/mf:date(.)), max(Detail/EndDate/mf:date(.)), min(Detail/StartDate/mf:date(.)), min(Detail/EndDate/mf:date(.))"/>
</xsl:template>
</xsl:stylesheet>
to convert to xs:date first and then to compute a max or min date and not a max or min number.
If you still have problems then post enough information for us to reproduce the problem.

Xpath query and time

Hi, this is the content of my XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<mainNode>
<sub time="08:00">
<status id="2">On</status>
<status id="3">Off</status>
</sub>
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
<sub time="20:00">
<status id="4">Off</status>
<status id="7">On</status>
</sub>
<sub time="23:59">
<status id="4">On</status>
<status id="7">On</status>
</sub>
</mainNode>
My program gets the current time:
if I get 15.59, I must retrieve all the status id of the next sub time (16.00):
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
With a second XPath query I must get all the status id of the previous sub time (13.00).
How to do it? I know SQL but I'm quite new to XPath. I accept urls to serious XPath resources too, if any. Thanks!
Here are two solutions:
I. XPath 1.0
This is one pair of XPath 1.0 expressions that select the required nodes:
/*/*
[translate(#time, ':','')
>
translate('15:59',':','')
][1]
selects the first sub node with time later than 15:59.
/*/*
[translate(#time, ':','')
<
translate('15:59',':','')
][last()]
selects selects the first sub node with the previous than 15:59 sub time.
We can include these in an XSLT transformation and check that the really wanted result is produced:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
First time after 15:59:
<xsl:copy-of select=
"/*/*
[translate(#time, ':','')
>
translate('15:59',':','')
][1]
"/>
First time before 15:59:
<xsl:copy-of select=
"/*/*
[translate(#time, ':','')
<
translate('15:59',':','')
][last()]
"/>
</xsl:template>
</xsl:stylesheet>
When the above transformation is applied on the originally provided XML document:
<mainNode>
<sub time="08:00">
<status id="2">On</status>
<status id="3">Off</status>
</sub>
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
<sub time="20:00">
<status id="4">Off</status>
<status id="7">On</status>
</sub>
<sub time="23:59">
<status id="4">On</status>
<status id="7">On</status>
</sub>
</mainNode>
the wanted result is produced:
First time after 15:59:
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
First time before 15:59:
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
Do note the following:
The use of the XPath translate() function to get rid of the colons
The use of the last() function in the second expression
There is no need to convert the time to seconds before the comparison
When used as part of an XML document (such as an XSLT stylesheet, the < operator must be escaped.
II. XPath 2.0
In XPath 2.0 we can use the following two expressions to produce select the desired nodes:
/*/*[xs:time(concat(#time,':00'))
gt
xs:time('15:59:00')
][1]
selects the first sub node with time later than 15:59.
/*/*[xs:time(concat(#time,':00'))
lt
xs:time('15:59:00')
][last()]
selects selects the first sub node with the previous than 15:59 sub time.
We can include these in an XSLT 2.0 transformation and check that the really wanted result is produced:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
First time after 15:59:
<xsl:copy-of select=
"/*/*[xs:time(concat(#time,':00'))
gt
xs:time('15:59:00')
][1]
"/>
First time before 15:59:
<xsl:copy-of select=
"/*/*[xs:time(concat(#time,':00'))
lt
xs:time('15:59:00')
][last()]
"/>
</xsl:template>
</xsl:stylesheet>
When the above transformation is applied on the originally provided XML document (the same as in the first solution), the same wanted result is produced.
Do note the following:
In XPath 2.0 xs:time is a native data type. However, in order to construct an xs:time() from the values in the xml document, we have to concat to them the missing seconds part.
In XPath 2.0 xs:time values can be compared with the "atomic-value comarison operators" such as lt or gt.
Here is the ugly Xpath 1.0 solution:-
sub[number((substring-before(#time, ':')) * 60 + number(substring-after(#time, ':'))) > 959][1]
Note 959 = 15 * 60 + 59 which I'm sure you can do in your calling code.
Give that node the previous node can be accessed as:-
preceding-sibling::sub[1]
However a pragmatic, common sense solution would be to load the XML data into a set of data structures and use a language more suited to this task to look the items up.
Well as long as the time is HH:MM something like the following should work:
(I must excuse my syntax since I'm just dabbling without running, consider this pseudo-xpath):
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
//sub[fn:compare(#time,'12:59') > 0][1]/status
This should select all the elements where time is greater than 12:59 and then select the first of those elements.
You could also pass the value '12:59' as an external parameter into the xpath evaluation.
If you generate the xml yourself, you could change the way you store the time attribute using an integer value (ticks, for example), then you could do an easy numerical comparison using something like
//sub[#time > 1389893892]

Resources