xpath select child and parent

Viewed 49

I started using xPath today, and am still trying how to implement it correctly. I'm trying to make an xPath that finds other tables that reference them using the COLUMN_FOREIGN_TABLE field. Then the xPath should both return the TABLE_NAME and the COLUMN_NAME. I've seen some other questions about multiple values from xpath, but none of the answers really explained what was happening, and the problems weren't anything like this.

The problem
I can currently find the TABLE_NAME and the COLUMN_NAME seperate, but not combined. I need some way to have both, so I know what column from what table is referencing to it.

What have I tried?
Currently I have this code:

$xml = file_get_contents('kingDbOverviewExpanded.xml'); // XML file location
$fullSchema = new SimpleXMLElement($xml); // Make XML element

$curr = $_GET['table']; // Gets table

$foundBacklinksTableName = $fullSchema->xpath('(//COLUMNS/COLUMN[COLUMN_FOREIGN_TABLE="' . $curr . '"])/../../TABLE_NAME'); // Gets the table names
$foundBacklinksColumnName = $fullSchema->xpath('(//COLUMNS/COLUMN[COLUMN_FOREIGN_TABLE="' . $curr . '"])/*[self::COLUMN_NAME]'); // Gets the column names

XML Structure
I currently have an XML file with the following structure:

<?xml version="1.0"?>
<TABLES>
  <TABLE>
    <TABLE_NAME>tabBtw</TABLE_NAME>
    <COLUMNS>
      <COLUMN>
        <COLUMN_NAME>BtwRksGID</COLUMN_NAME>
        <COLUMN_TYPE>integer</COLUMN_TYPE>
        <COLUMN_DEFAULT></COLUMN_DEFAULT>
        <COLUMN_INDEX_TYPE>FK</COLUMN_INDEX_TYPE>
        <COLUMN_FOREIGN_TABLE>tabRekeningSchema</COLUMN_FOREIGN_TABLE>
      </COLUMN>
    </COLUMNS>
  </TABLE>
</TABLES>

COLUMN_FOREIGN_TABLE can be empty, but also filled. The XML file is a file with 60k+ lines, and all COLUMN_FOREIGN_TABLE's can be found back in some other tables TABLE_NAME

So what would I have to do to return both the values? And why should I do that that way? I'm still trying to learn xpath, and just the correct answer doesn't teach me anything, so I'd really appreciate it to know why things are happening.

0 Answers
Related