I need a help in solving an issue with Xquery.
I am using below Xquery to get the "value" of attributes. However there is one attribute "Email" which has further child attributes. SO with below Xquery I am getting data for all the "Value"
i.e. truetruebc#hghh.gff, truetruexyn#qwe.com, 39919741
However I need only "abc#hghh.gff $# xyn#qwe.com, 39919741" in my output.
{/root/*/attributes/* [ov='true']/fn:concat(fn:data(value),',')}
<root>
<_1>
<attributes>
<Email>
<ov>true</ov>
<value>
<Email>
<ov>true</ov>
<value>abc#hghh.gff</value>
</Email>
</value>
</Email>
<Email>
<ov>true</ov>
<value>
<Email>
<ov>true</ov>
<value>xyn#qwe.com</value>
</Email>
</value>
</Email>
<UniqueId>
<ov>true</ov>
<value>39919741</value>
</UniqueId>
</attributes>
</_1>
</root>
I think you want to take the concatenation of those value descendant elements that don't have any child elements so you can use e.g. string-join(/root/*/attributes/*[ov='true']//value[not(*)], ',').
In XQuery 3.1 you could also use the innermost() function with e.g. string-join(innermost(/root/*/attributes/*[ov='true']//value), ',').
Try:
fn:string-join(/root/attributes[ov='true']/*/Value/string(), ",")
Your XML document isn't well-formed. (XML element names are case-sensitive, so you can't start with <Value> and end with <value>, for example.) So I fixed that.
Then, I found that your query yields nothing, because it's looking for an element ('*') between root and attributes, which doesn't exist in the document. So I changed /*/ to just /.
At that point, I got the result of ENtity1_Value, ENtity2_Value, ENtity3_Value,. So who knows where ChildValue1, ChildValue2, came from. Possibly your real query had a // in it.
(When you ask a question on StackOverflow, you should ensure that your example behaves as described.)
Related
I am new to R and I cannot find example of extracting specific attribute in an empty element, most example i found are extracting data value of a child nodes.
In a nutshell, how to extract an attribute using xmlEventParse() for XML like this:
<elements>
<element attribute1="value" attribute2="value"/>
<element attribute1="value" attribute2="value"/>
</elements>
Assuming I want to get attribute1 on the 2nd element.
Thanks in advance.
Update: Found the solution. It is xmlAttrs(root[[2]])[['attribute1']]
here is my sample XML :
<Entries>
<Entry>
<id>1</id>
.
.
</Entry>
<Entry>
<id>2</id>
<assets>
<TextAssetInfo>
<Name>Alpha</Name>
<TagName>alphaName</TagName>
<value>Harambe</value>
</TextAssetInfo>
</assets>
</Entry>
<Entries>
So my goal is simply to extract the value of Alpha and store it in a variable in my XSLT for further computation.
I am able extract and print the value. But not assign in to a variable.
Following part of XSL code works:
<T_AlphaName><xsl:value-of select="./assets/*[TagName = 'alphaName']/value"/></T_AlphaName>
However, I can't get the below to work :
<xsl:variable name="myVar" select="./assets/*[TagName = 'alphaName']/value"/>
I have tried many other combinations as well but no luck. What am I doing wrong? In my XSL I have part to test it and get a weird result
<TestVar><xsl:value-of select="$myVar"/></TestVar>
Result : <TestVar/>
Additional info :
Using XML/XSL version 1.0.
AlphaName is not the only TextAssetInfo inside the assets. There are many more.
basically I want to put information into a balloon in Maps API, this is the KML file, the data is stored using SimpleData tags, and I am trying to access to it from the BalloonStyle text tag.
But it doesn't work, in the baloon is displayed simply $[something]. After some research, I discovered Entity replacement may not be supported anymore by SimpleData tags.
So how do I manage the data? I got the data from ogr2ogr conversion from a shapefile and I don't know how to manage its output to make it use ExtendedData and Data tags.
Thank for your help.
You can replace <SchemaData><SimpleData> with <Data><value> elements with a text editor preferably one that can perform regular expression replacements on searches such as NotePad++.
You start with this:
<ExtendedData>
<SchemaData schemaUrl="#biblioteche">
<SimpleData name="INDIRIZZO">VIA SAN VITTORE, 21</SimpleData>
<SimpleData name="TIPOLOGIA">BIBLIOTECHE</SimpleData>
...
<SimpleData name="ID">0</SimpleData>
</SchemaData>
</ExtendedData>
And need to convert to this form:
<ExtendedData>
<Data name="INDIRIZZO">
<value>VIA SAN VITTORE, 21</value>
</Data>
<Data name="TIPOLOGIA">
<value>BIBLIOTECHE</value>
</Data>
...
<Data name="ID">
<value>0</value>
</Data>
</ExtendedData>
Globally make the following replacements (in this order):
#
Find what
Replace with
1.
<SchemaData schemaUrl="#biblioteche">
2.
</SchemaData>
3.
<SimpleData
<Data
4.
(<Data name=".*?">)
\1<value>
5.
</SimpleData>
</value></Data>
Steps 1 and 2 have an empty target such that you delete the element.
Step 4 is the only step that needs to be done as a regular expression.
working example
My XSLT is shown below.
aic is a namespace.
What is cstyle?
is it a built-in XSLT element/function?
Or an element within the expected input xml?
<xsl:stylesheet exclude-result-prefixes="aic"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aic="http://ns.adobe.com/AdobeInCopy/2.0/" >
<xsl:template match="/">
</xsl:template>
<xsl:template match="aic:cstyle[contains(#name,'bold')]">
</xsl:template>
</xsl:stylesheet>
It is an element within the expected input XML. The XPaths in an XSLT's match attributes are generally applied to contents from the input XML.
Exactly as in my answer to your previous question, aic:cstyle is a selector that matches elements whose local name is cstyle and whose namespace URI is http://ns.adobe.com/AdobeInCopy/2.0/ (the URI bound to the aic prefix in the xsl:stylesheet element). Thus
<xsl:template match="aic:cstyle[contains(#name,'bold')]">
is a template that will apply to any {http://ns.adobe.com/AdobeInCopy/2.0/}cstyle element that has a name attribute that contains the substring bold. (So, to answer your question directly: the expression in question will match elements in the input streams for which the stylesheet was written.)
As with any new programming language, I would strongly recommend that you find a decent tutorial and work through that to get comfortable with the syntax and idioms of the language through simple examples before you start trying to decode a large and complex XSLT that you've inherited from elsewhere.
Relating to this question, is it possible to use variables inside xsl:key? I want to do smth like this:
<xsl:key name="ChargesKey" match="$ChargesForDisplay/charge" use="Name"/>
I'm using XSLT 1.0 with ASP.Net
I believe I can safely assume that you are referring to dynamically-generated node-set variables (as opposed to those selected from the source DOM, which are trivial), and yes it is possible to perform a key-match on the contents of a dynamically generated node-set variable (as I demonstrate for this question).
Assuming you have a variable like this:
<xsl:variable name="ChargesForDisplay">
<charge>
<Name>Name1</Name>
</charge>
<charge>
<Name>Name2</Name>
</charge>
<charge>
<Name>Name1</Name>
</charge>
<charge>
<Name>Name3</Name>
</charge>
</xsl:variable>
You would define the key like this:
<xsl:key name="ChargesKey" match="charge" use="Name"/>
And then you can apply it like this:
<xsl:template match="/">
<xsl:apply-templates select="msxsl:node-set($ChargesForDisplay)" />
</xsl:template>
<xsl:template
match="charge[generate-id(.)=generate-id(key('ChargesKey',Name)[1])]">
<xsl:variable name="matchingItems" select="key('ChargesKey', Name)" />
...
</xsl:template>
Of course, if the variable contains a selection of nodes from the source XML DOM, then it's just the same approach, except you don't need to use msxsl:node-set().
I suspect that having a key on a node-name that's also present in the source XML document or multiple dynamically-generated node-set variables may cause grouping to produce unexpected results(because the key() function would locate nodes from both the variable and the source document). For this reason, I'd suggest defining keys on nodes that would only be present in one particular variable and nowhere else.
The match attribute of xsl:key must be a valid pattern and $x/y is not a valid pattern. So, the answer is no. Now tell us what you are trying to achieve and we can help you achieve it. (JLRishe makes some wild guesses, which s/he calls "wild assumptions", and which may well be right; but I don't know where the guesses come from).