Q:

Searching in XML using another XML data

I have those two tables:
Table1:
IDt1    |    XML
1123         xml
1457         xml

Table2:
ID     |    Name    |    XML
01           A           xml
02           B           xml

I want to search in table1 XML and if the data exist, then use the IDt1 and search with it in table2 XML if it exist, then return the ID and Name from table2
here is the XMLs for each table.
Table1 xml:
<t>
  <ts>
    <sc>
      <rn>445</rn>
      <am>Name1</am>
    </sc>
    <sc>
      <rn>555/rn>
      <am>Name2</am>
    </sc>
  </ts>
</t>

Table2 xml:
<t>
  <ts>
    <d>
      <n>01123</n>
    </d>
    <d>
      <n>04344</n>
    </d>
  </ts>
</t>

data I want like:
ID   |   Name
01        a

I was able to retrieve the IDs from table 1 that contain what I'm looking for but then I was not able to use these IDs to search in the other xml.
Here is what I have:
SELECT IDt1 as table1IDs
  FROM [Table1]
  WHERE [XML].exist ('/t/ts/sc/am[contains(., "Name1")]') > 0

it returns a table of IDs that contain the Name1 now the question how to use the IDs and search with them in the xml of table2 and then return Name and ID of table2
the issue is now I have to use xml.exist [contain table1IDs ] which cannot accept variables 
Is there a way to do that? 

A:

After several testing I think this does the job
DECLARE @searchFor NVARCHAR(max)=N'Name1'
SELECT t2.ID as ID, t2.Name as Name
FROM table1 AS t1 
JOIN table2 AS t2 ON t2.xml.query(N'/t/ts/d/n').value('.', 'VARCHAR(max)') = CAST(t1.ID as varchar(max))
WHERE t1.xml.exist(N'/t/ts/sc/am[text()[1]=sql:variable("@searchFor")]')=1 order by ID

returns 
ID   |   Name
01        a

