在xml文件中使用逗号分隔检查字符串值

时间:2013-04-27 09:45:27

标签: php xml xpath

我有这种类型的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";
 }
?>

2 个答案:

答案 0 :(得分:1)

您可以使用

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

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

答案 1 :(得分: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