0

タブで区切られた ELEMENT 内のテキスト値の PHP simplexml xpath 検索を実行し、検索テキストのオフセットとは異なるオフセットで同じ要素からテキストを返す方法は?

'2' の値を含む DATA 要素を見つけて、LongValue 'Academy' を返したいとしましょう。

xml ドキュメントの形式は次のとおりです。

    <METADATA Resource="Property" Lookup="Area"> 
    <COLUMNS>->fieldname *(->fieldname)-></COLUMNS>
    *(<DATA>->fielddata *(->fielddata)-></DATA>) 
    </METADATA>

   Note: ignore spaces
         *()  means 1 or more
         -> is tab chr(9)

以下の例では、COLUMNS 要素に 3 つの列名 ( LongValueShortValueValue ) が含まれており、任意の順序にすることができます。

各 DATA 要素には、対応する 3 つのタブ区切りのテキスト値があります。たとえば、以下の最初の DATA 要素には

    LongVlaue = 'Salado'  
    ShortValue = 'Sal' 
    Value = '5' 

ここにXML文書があります

<METADATA Resource="Property" Lookup="Area">
<COLUMNS>   LongValue   ShortValue  Value   </COLUMNS>
<DATA>  Salado  Sal 5   </DATA>
<DATA>  Academy Aca 2   </DATA>
<DATA>  Rogers  Rog 1   </DATA>
<DATA>  Bartlett    Bar 4   </DATA>
</METADATA>

注: COLUMNS 要素と DATA 要素には、3 列のテキスト タブ区切りがあり、各列はタブで始まり、テキストが続き、最後に最後のタブが 1 つあります。

これが私が思うことです:

1.) DATA 要素から対応するテキストを検索する前に、COLUMNS 要素から 'Value' という名前の列のオフセットを見つけることをお勧めします。その順序で。

2.) 「値」列にテキストを含む DATA 要素を検索し、「LongValue」からテキストを返します。

これは、COLUMNS 要素の Value 列のオフセットを考慮せず、「Value」列の対応する (正しい) 位置を適切に見つけることができるため、機能するものの欠陥がある xpath 検索の例です。データ要素。

コードスニップは次のとおりです。

$xml_text = ‘the xml document above’;
$xml = simplexml_load_string($xml_text); //load the xml document
$resource = 'Property'; //value for the Resource attribute METADATA.
$lookup = 'Area'; //value for the Lookup attribute in METADATA
$value = '2'; //the needle we are looking for

$find = "\t" . $value . "\t";
/* 
 adding tabs before and after the $value may be flawed, although each 
 column starts with a tab followed by text, only the last column has 
 the an extra tab. Not sure this would work properly if the column 
 was in the middle, or if the ELEMENT happened to have multiple $value 
 in the same element. */

   /* 
     Search for a specific METADATA element with matching 
     Resource and Lookup attributes */


$node = $this->xml->xpath(
             "//METADATA[@Resource='{$resource}' and @Lookup='{$lookup}']"
            ."/DATA[contains(., '{$find}')]"
        ); 

    $x = explode("\t", (string) trim($node[0])); //convert the tab delimited 
                                                 //string to an array

    echo print_r($x,true); //this shows what the array would look like, 
                           //with out the trim there would be empty 
                           //first and last array elements

Array
(
    [0] => Academy
    [1] => Aca
    [2] => 2
)


    $LongValue = $x[0]; //assuming the LongValue is in the first column

    echo $LongValue; //this shows the LongValue retuned
    Academy

助けてくれてありがとう!

更新...投稿後、これを思いつきました...</p>

//get index of 'Values' column from COLUMNS element
$node = $this->xml->xpath(
             "//METADATA[@Resource='{$resource}' and @Lookup='{$lookup}']"
            ."/COLUMNS");
if($node) {

    //array of column names
    $columns = explode("\t", strtolower((string) trim($node[0]))); 

    $long_value_index = array_search('longvalue', $columns);

} else {
    echo 'not found';
    exit;
}

$index を使用すると、適切なオフセットから LongValue を返すことができます

$LongValue = $x[$long_value_index]; 

何かご意見は

4

1 に答える 1