I have an XML that looks like below. As of now if both A and B nodes are coming in the XML, I can easily map their attribute i.e. type="A-type" and type="B-type".
I'm stuck at a scenario when <B> node may not be present in the XML and then too, I need to get <A> node attribute i.e. type="A-type", but current logic returns me no rows. Please help how to achieve this.
Please note that <B> can come any number of times under <A>, that's why we have to use the path Root/A/B. Since occurrence of <B> is not predicted, I can't change the path.
Example 1 (<B> is present):
DECLARE @myXML XML =
'<Root>
<A type="A-type">
<B type="B-type"></B>
</A>
</Root>'
SELECT
N.value('(../@type)[1]','VARCHAR(100)'),
N.value('(@type)[1]','VARCHAR(100)')
FROM @myXML.nodes('Root/A/B') AS X(N)

Example 2 (<B> is not present):
DECLARE @myXML XML =
'<Root>
<A type="A-type">
</A>
</Root>'
SELECT
N.value('(../@type)[1]','VARCHAR(100)'),
N.value('(@type)[1]','VARCHAR(100)')
FROM @myXML.nodes('Root/A/B') AS X(N)

Expected Output:
