-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>

这是一个字符串:-

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

在这里,我想在 xml 文件中一一检查逗号分隔值检查。
我试试这个 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 回答