I would like to select an attribute based on its parent element.
One way to do it is the following:
<xsl:template match="#Name[name(..) = 'EntityType' and namespace-uri(..)= 'http://schemas.microsoft.com/ado/2008/09/edm']">
Is it possible to do it in a shorter way - something similar to (whic does not work)
#Name[../edm:EntiyType]
Namespace prefixes are already defined in the document (I can for example select //*/edm:EntityType)
Thanks,
Matra
Depending how you apply your templates the following should work
<xsl:template match="edm:EntiyType/#Name">
Related
Using xslt 1.0 (BizTalk 2016) I'm looking for a generic way to select the namespace of any valid xml document
For example, I have the following xml document:
<?xml version="1.0" encoding="utf-8"?>
<PortfolioActivation xmlns="http://www.random.com/bo/request/portfolioactivation">
<Portfolio>
<ExternalId>PRT-00000450</ExternalId>
<InternalId>c8b0239c-1e98-e911-a8b1-00224800449b</InternalId>
<Version>8627558</Version>
<Type>001</Type>
</Portfolio>
</PortfolioActivation>
Given that the Root element value could be anything, what would be the xpath to select the value of the namespace i.e http://www.random.com/bo/request/portfolioactivation
I had hoped "/*/#xmlns" would work but it doesn't.
The namespace of the outermost element can be found using namespace-uri(/*).
Alternatively, the default namespace that's in scope for the outermost element is /*/namespace::*[name()=''].
These aren't the same thing. Consider
<p:root xmlns="a.ns" xmlns:p="b.ns"/>
The first expression will give you "b.ns", the second will give you "a.ns". It's not clear from your question which you want.
Note that namespaces are not attributes in the XDM data model, so you never access them using the attribute axis. #xmlns will therefore never work.
I have the following XML:
<envelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="vendor">
<fizz buzz="3"/>
</payload>
</envelope>
I am trying to write an XPath expression that will pluck out vendor (value for the payload's class attribute) or whatever its value is.
My best attempts are:
/dataEnvelope/payload[#class="vendor"]#class
But this requires the expression to already know that vendor is the value of the attribute. But if the XML is:
<dataEnvelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="foobar">
<fizz buzz="3"/>
</payload>
</dataEnvelope>
Then I want the expression to pluck out the foobar. Any ideas where I'm going awry?
If you need #class value from payload node, you can use
/dataEnvelope/payload[#class]/#class
or just
/dataEnvelope/payload/#class
At first, your two XML files are out-of-sync - one references envelope and the other references dataEnvelope. So exchange one for the other, if necessary.
So, to get the attribute value of payload, you can use an XPath expression like this which uses a child's attribute value to be more specific:
/envelope/payload[fizz[#buzz='3']]/#class
Output is:
vendor
If the document element can/will change, then you can keep the XPath more generic and select the value of the class attribute from the payload element that is a child of any element:
/*/payload/#class
If you know that it will always be a child of envelope, then this would be more specific(but the above would still work):
/envelope/payload/#class
I have firexpath, and it doesn't seem to be working with xpath. Even something simple //div returns no results. Even if I click on an existing node, say "copy XPath" and then paste that XPath into filter input box, it says "no nodes found". //*[name()='div'] does work though. Am I missing a namespace or something? Here is what root tag looks like (it's a valid XHTML):
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" class="ff ff3">
I didn't find a support forum for FireXPath, so I'm posting it here.
In case you cannot register a namespace (for the default namespace) and then prefix every element name in the XPath expression with the registered prefix, then you can use an XPath expression like this:
/*[name()='x']/*[name()='y']/*[name()='z']
In case there are elements belonging to other namespaces (than the default namespace), you'll have to use a more specific XPath expression:
/*[name()='x' and namespace-uri()='http://www.w3.org/1999/xhtml']
/*[name()='y' and namespace-uri()='http://www.w3.org/1999/xhtml']
/*[name()='z' and namespace-uri()='http://www.w3.org/1999/xhtml']
If you could have registered the default namespace and the prefix was (say) "p", then the above would be equivalent to a now simpler expression:
/p:x/p:y/p:z
I have not used firexpath but it looks as though the default namespace xmlns="http://www.w3.org/1999/xhtml" is preventing xpath from finding the div as the div element inside the element which defies xmlns with be prefixed with that namespace.
You therefore would need to register the namespace with firexpath using some sort of method call??? then //div should work or your expression is fine as well, if you were wanting to consider namespaces in the expression you could include a check for the namespace like so
//*[name()='div' and namespace-uri()='http://www.w3.org/1999/xhtml']
EDIT:
I have downloaded firexpath which now is called firepath and it doesn't look possible to register a namespace so it looks like you will have to the name() method
I would like to be able to find an element named 'test' with no attributes at all.
How can I do that using XPATH?
If i just query for /test it finds me all the test elements even with the ones that contain attributes.
example:
<main>
<test id="test1">txt</test>
<test>txt2</test>
</main>
Querying for //test will find me both of the elements. I want only the one that contains no attributes. I can query for //test[not(#id)] but I was wondering if there is a command for an element with no attributes at all.
You almost had it. If you want to find the the test element(s) that have no attributes at all, use a wildcard match for the attribute name in your predicate filter:
//test[not(#*)]
Query for test.
http://www.w3schools.com/XPath/xpath_syntax.asp
<?xml version="1.0" encoding="windows-1250"?>
<test>
Oh hai
</test>
test = /test
Oh hai = /test/text()
Not sure about your actual intent.
<AgoraServersConfig>
<AgoraServers id="NYQ1">
<AgoraName>prod</AgoraName>
<TableName>someTable</TableName>
<Rule_ID>1</Rule_ID>
<Rule_ID>3</Rule_ID>
<Rule_ID>5</Rule_ID>
</AgoraServers>
<AgoraServers id ="QA03">
<AgoraName>dev</AgoraName>
<TableName>someTable</TableName>
<Rule_ID>1</Rule_ID>
<Rule_ID>2</Rule_ID>
<Rule_ID>5</Rule_ID>
</AgoraServers>
</AgoraServersConfig>
Given the above schema, I would like to know how to frame an Xpath query that returns the children of the node whose id is "QA03", for example.
Many Thanks in advance
Use:
/*/*[#id='QA03']/node()
It is also possible to use the standard XPath function id().
However, for this to work, there must be a DTD for the XML document that defines id as an "ID-type attribute".
Example: id('QA03')/node()
I'm not sure I fully understand your schema, but to return children of a node based on that node's id attribute would look something like
//*[#id='QA03']/*
The xpath would look like this:
/AgoraServersConfig/AgoraServers[#id='QA03']/*
If you wanted to do something a bit more dynamic, you could put the id into a variable, e.g.
<xsl:variable name="targetid">QA03</xsl:variable>
<xsl:for-each select="/AgoraServersConfig/AgoraServers[#id=$targetid]/*">
<xsl:copy-of select="."/>
</xsl:for-each>