Accessing the attribute of a child node through a variable - xslt

I am using XLS through C# and I need to be able to access the attributes of a specific node, but whatever I do I get the same "eof with #" error. Am I doing something wrong? Is there something I am missing? This is a code sample:
<xsl:value-of select="$main[1]#index"/>
Error message:
System.Xml.Xsl.XslLoadException: 'Expected end of the expression, found '#'.
$main[1] -->#<-- index'

To select an attribute named index of the context element use #index. Use that in a separate step if you first select elements e.g. foo/#index selects the index attributes of all foo children of the context node.
$main[0] doesn't make much sense as in XPath the first item has the index 1 so perhaps $main[1]/#index is what you want, it depends on how the variable or parameter main has been bound to a value.

Related

Xquery statement to get a value for specific name

I am trying to write a xquery to get the Value for a specific name .Below is the request payload: i.e. if the Name ="ID" then get the
"Value" for that tag (i.e.1000000000.)If the Name="User" get the "Value" for that tag( ie."US").
<not:Items xmlns:v5="http://www.example.com"
xmlns:com="http://commom.com
xmlns:not="http://services.not.com"
xmlns:com1="http://common1.com">
<not:Array>
<com1:Item>
<v5:List>
<com:extensionsItem>
<com:Name>ID</com:Name>
<com:Value>1000000000</com:Value>
</com:extensionsItem>
<com:extensionsItem>
<com:Name>User</com:Name>
<com:Value>US</com:Value>
</com:extensionsItem>
</v5:List>
</com1:Item>
</not:Array>
</not:Items>
I tried the options below:
<ns2:ID>{fn:data($Items/not:Array/com1:Item/v5:List/com:extensionsItem[1]/com:Value)}<ID>
This statement works . But I cannot assure that ID will always come in first element in the array of List.So i want a statement that will work even if ID comes in any other
place in the array and I can retrieve the value.
Thanks in advance
I think you simply want to apply a predicate $Items/not:Array/com1:Item/v5:List/com:extensionsItem[com:Name = 'ID']/com:Value

Print value from a lua table if pattern matches

Okay, so I just recently got into lua and find myself stuck with the following:
I have the function peripheral.getNames() (which is a custom function)
it will return a table with the structure key,value, whereas key is always a number and starts from 1 and value will be what the function finds (it searches for devices connected to it)
In my example it creates a table which looks like this
1 herp
2 derp
3 monitor_1
4 morederp
I can print the values with the following
local pgn = peripherals.getNames()
for key,value in pairs(pgn) do
setCursorPos(1,key)
write(value)
end
end
this will output the corresponding value of the table at key on my display like this
herp
derp
monitor_1
morederp
now, I try to filter my results so it only prints something if value contains 'monitor'
I tried to achive this with
for key,value in pairs(pgn) do
if string.match(value, monitor) then
#dostuff
end
end
but it always returns 'bad argument: string expected, got nil'
so obviously string.match either does not accept 'value' or, value is not a string
so i tried converting value first
for key,value in pairs(pgn) do
value = tostring(value)
if ....
#dostuff
end
end
but it still throws the same error
Do any of you have an idea how i might either get string.match to accept 'value' or if there is another method to check the contents of 'value' for a pattern while in this for loop?
The error message is talking about the variable monitor, which is not defined and so has a nil value.
Try string.match(value, "monitor").

Construct Xpath

I have the following repeated piece of the web-page:
<div class="txt ext">
<strong class="param">param_value1</strong>
<strong class="param">param_value2</strong>
</div>
I would like to extract separately values param_value1 and param_value2 using Xpath. How can I do it?
I have tried the following constructions:
'//strong[#class="param"]/text()[0]'
'//strong[#class="txt ext"]/strong[#class="param"][0]/text()'
'//strong[#class="param"]'
none of which returned me separately param_value1 and param_value2.
P.S. I am using Python 2.7 and the latest version of Scrapy.
Here is my testing code:
test_content = '<div class="txt ext"><strong class="param">param_value1</strong><strong class="param">param_value2</strong></div>'
sel = HtmlXPathSelector(text=test_content)
sel.select('//div/strong[#class="param"]/text()').extract()[0]
sel.select('//div/strong[#class="param"]/text()').extract()[1]
// means descendant or self. You are selecting any strong element in any context. [...] is a predicate which restricts your selection according to some boolean test. There is no strong element with a class attribute which equals txt ext, so you can exclude your second expression.
Your last expression will actually return a node-set of all the strong elements which have a param attribute. You can then extract individual nodes from the node set (use [1], [2]) and then get their text contents (use text()).
Your first expression selects the text contents of both nodes but it's also wrong. It's in the wrong place and you can't select node zero (it doesn't exist). If you want the text contents of the first node you should use:
//strong[#class="param"][1]/text()
and you can use
//strong[#class="param"][2]/text()
for the second text.

XSLT 1 - find child node where node has case-insensitive value

I have the following selector, which works:
parent::node()/myNS:expField[myNS:Nam='NAMETOFIND']/myNS:Val
What I want is to do case-insensitive matching on the myNS:Nam value, so I would be able to select <Val> from any of the following:
<expField>
<Nam>NAMETOFIND</Nam>
<Val>the value I want</Val>
</expField>
<expField>
<Nam>NameToFind</Nam>
<Val>the value I want</Val>
</expField>
<expField>
<Nam>nametofind</Nam>
<Val>the value I want</Val>
</expField>
<expField>
<Nam>nAmEtOFInD</Nam>
<Val>the value I want</Val>
</expField>
I'm using XSLT 1, so I can't use lower-case().
translate() will do the job, it's not pretty but it works. If you know what language you want to process, that is.

How to test for missing attribute in XSL?

What I have is following:
<xsl:variable name="myvar" select=".//spss:category[((not #varName) or #varName=$colVarName) and #text=$series]/spss:cell/#text"/>
What it should do is select the text of the spss:cell's text-Attribute as long as it is a child of a spss:category that has
either a varName Attribute with a value equal to $colVarName
OR no varName Attribute at all
What is happening is following error message (sorry translating here, so just the gist of it):
Expected Token ')'. Found Token '#'.
.//spss:category[((not -->#<-- varName) or #varName=$colVarName...
Problem Solved! (See below)
OK, I think I found the mistake:
not must be used with parenthesis, so instead of
(not #varName) or #varName=$colVarName
it should have been
not(#varName) or #varName=$colVarName
indeed - not() is a function that returns the boolean opposite of whatever is between the parens. If necessary - it will cast its argument to a boolean. In this case, an empty node set casts automatically to false, so if #varName gives you an empty node set, not(#varName) will be true.