问题
Good day everyone.
I'm working on a very simple application and I want to implement something that will allow the user to choose a tool from a drop down menu, and after clicking the submit button it will show the information of the chosen tool by extracting the information from the XML File.
I created the dropdown menu by extracting the tool names from the XML file:
<form method="POST" action="showspecifictool.php">
<select id="choose" name="choose">
<?php
$tools = simplexml_load_file('tools.xml');
foreach($tools as $tool) {
echo "<option value='test'>".$tool->name."</option>";
}
?>
</select>
<input type="submit" name="submit" add="choose">
The next step would be to use XPath to show all the information of the tool chosen from the dropdown menu, however I'm not quite sure how to approach that, and I'd really appreciate the help!
My tools.xml looks something like this:
<tool type="learning" web-based="True" free="True">
<name>...</name>
<description>...</description>
<url>...</url>
<subjects>...</subjects>
<creators>...</creators>
<category>...</category>
<price>...</price>
</tool>
回答1:
Xpath expression allow you to fetch nodes from a DOM using location paths and conditions. You need an unique value in the data. An id would work best but lets assume the name is unique.
$_REQUEST['tool'] = 'Tool A';
$selectedToolName = str_replace(['"', "'"], '', $_REQUEST['tool'] ?? '');
$tools = new SimpleXMLElement(getXML());
?>
<form method="GET" action="showspecifictool.php">
<select id="choose" name="choose">
<?php
foreach($tools as $tool) {
printf(
'<option value="%1$s"%2$s>%1$s</option>',
htmlspecialchars($tool->name),
$selectedToolName === $tool->name ? ' selected' : ''
);
}
?>
</select>
<input type="submit" name="submit" add="choose">
<?php
foreach($tools->xpath("(tool[name = '{$selectedToolName}'])[1]") as $tool) {
var_dump((string)$tool->name);
}
Output:
<form method="GET" action="showspecifictool.php">
<select id="choose" name="choose">
<option value="Tool A" selected>Tool A</option></select>
<input type="submit" name="submit" add="choose">
string(6) "Tool A"
The Xpath expression fetches the tool child elements of the $tools context node. tool[name = 'Tool A'] adds the condition of a specific name. (tool[name = 'Tool A'])[1] limits the result to the first found node.
In DOM you would need some more Xpath expressions:
$_REQUEST['tool'] = 'Tool A';
$selectedToolName = str_replace(['"', "'"], '', $_REQUEST['tool'] ?? '');
$document = new DOMDocument();
$document->loadXML(getXML());
$xpath = new DOMXpath($document);
?>
<form method="GET" action="showspecifictool.php">
<select id="choose" name="choose">
<?php
foreach($xpath->evaluate('/*/tool') as $tool) {
$toolName = $xpath->evaluate('string(name)', $tool);
printf(
'<option value="%1$s"%2$s>%1$s</option>',
htmlspecialchars($toolName),
$selectedToolName === $toolName ? ' selected' : ''
);
}
?>
</select>
<input type="submit" name="submit" add="choose">
<?php
foreach($xpath->evaluate("(/*/tool[name = '{$selectedToolName}'])[1]") as $tool) {
var_dump(
$xpath->evaluate('string(name)', $tool)
);
}
来源:https://stackoverflow.com/questions/65088508/how-to-show-information-about-the-selected-engineering-tools-using-xpath-in-php