-4

私はこのタイプのxmlファイル呼び出しを持っていますregin.xmlです:-

<root>
  <child_1 entity_id = "1" value="Game" parent_id="0">
    <child_2 entity_id="2" value="Activities" parent_id="1">
      <child_3 entity_id="3" value="Physical1" parent_id="2">
        <child_6 entity_id="6" value="Cricket" parent_id="3">
          <child_7 entity_id="7" value="One Day" parent_id="6"/>
        </child_6>
      </child_3>
      <child_4 entity_id="4" value="Test1" parent_id="1">
        <child_8 entity_id="8" value="Test At Abc" parent_id="4"/>
      </child_4>
      <child_5 entity_id="5" value="Test2" parent_id="1">
        <child_9 entity_id="9" value="Test At Xyz" parent_id="5"/>
      </child_5>
    </child_2>
</child_1>
</root>

これは1つの文字列です:-

$activity = "One Day,Test At Abc,Test At Xyz";

ここでは、xml ファイルのカンマ区切り値チェックを 1 つずつチェックしたいと思います。
私はこのxpathを試します。

$Rgn_id = $region->xpath("//*[@value = '$region1']/@entity_id");

アクティビティがxmlファイルで一致する場合、xmlファイルからentity_idが表示されます。
ありがとう...

これは私のPHPの試みです:-

<?php
$Activity = 'One Day,Test At Abc';
echo "$Activity";
$string = explode(",",$Activity);
echo "<pre>";
print_r ($string);
echo "</pre>";
$r = file_get_contents('final.xml');
$region = simplexml_load_string($r);
foreach($string as $Activity){
list($result) = $region->xpath("//*[contains('$Activity', (@value ,','))]/@entity_id");
$result = (string)$result;
echo "my activity id is = $result";
 }
?>
4

2 に答える 2

1

使用:

//*[contains(concat(',', $activity, ','), concat(',', @value, ','))]/@entity_id

XSLT ベースの検証:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="activity" select="'One Day,Test At Abc,Test At Xyz'"/>

 <xsl:template match="/">
     <xsl:variable name="vWanted" select=
     "//*[contains(concat(',', $activity, ','),
                   concat(',', @value, ',')
                   )
        ]/@entity_id"/>
  <xsl:for-each select="$vWanted">
   <xsl:value-of select="concat(., ' ')"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると、次のようになります。

<root>
    <child_1 entity_id = "1" value="Game" parent_id="0">
        <child_2 entity_id="2" value="Activities" parent_id="1">
            <child_3 entity_id="3" value="Physical1" parent_id="2">
                <child_6 entity_id="6" value="Cricket" parent_id="3">
                    <child_7 entity_id="7" value="One Day" parent_id="6"/>
                </child_6>
            </child_3>
            <child_4 entity_id="4" value="Test1" parent_id="1">
                <child_8 entity_id="8" value="Test At Abc" parent_id="4"/>
            </child_4>
            <child_5 entity_id="5" value="Test2" parent_id="1">
                <child_9 entity_id="9" value="Test At Xyz" parent_id="5"/>
            </child_5>
        </child_2>
    </child_1>
</root>

XPath 式が評価され、この評価の結果として選択された属性の値が出力されます

7 8 9 
于 2013-04-27T16:10:17.140 に答える
1

あなたが使用することができます

$value = ",$activity,"; // now $value is ",One Day,Test At Abc,Test At Xyz,"

$Rgn_id = $region->xpath("//*[contains('$value', concat(',', @value, ','))]/@entity_id");
于 2013-04-27T10:07:38.207 に答える