通过xml文件中的属性ID从每个元素获取值 [英] Get value from each element by attributes id in xml file

查看:81
本文介绍了通过xml文件中的属性ID从每个元素获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是重复的,但到目前为止,我发现没有任何用处.

This could be duplicate, but I found nothing useful so far.

我现在要通过lang id获取每个控件(占位符,工具提示,标题)和每个项目(标题)的lang值时遇到的问题.

The problem I'm facing at the moment I'm trying to get lang value for each control (placeholder,tooltip,caption) and for each item (caption) by lang id.

我曾经尝试过更改xpath,但没有帮助.

I've tried changing xpath but dose not help.

如何通过lang id获取每个项目的标题值?

How to get caption value for each item by lang id ?

我尝试..

$pageMenu = new simplexmlelement($xml);

foreach($pageMenu->menucontrol->contrlos->control as $control){

    $placeholder = $pageMenu->menucontrol->contrlos->xpath(
        "//control[@category='{$category}']//placeholder//lang[@id='{$lang}']"
    );     
}

xml ..

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <menucontrol name="pageMenu">
    <contrlos>

      <control category="input">
        <type>number</type>
        <permission role="0">1</permission>
        <permission role="1">1</permission>
        <permission role="2">1</permission>

        <caption>
          <lang id="en"/>
          <lang id="ru"/>
          <lang id="ua"/>
        </caption>

        <placeholder>
          <lang id="en">Ticket 22#</lang>
          <lang id="ru">Ticket #</lang>
          <lang id="ua">Ticket #</lang>
        </placeholder>

        <tooltip>
          <lang id="en"/>
          <lang id="ru"/>
          <lang id="ua"/>
        </tooltip>

        <onpageevent/>
        <serverevent>searchTicketByNumber</serverevent>

        <showon>
          <tickets>1</tickets>
          <companies>0</companies>
          <consumer-users>0</consumer-users>
          <products>0</products>
          <staff>0</staff>
        </showon>

        <icon/>
        <disabled>0</disabled>

        <attributes>
          <attribute name="class">input-field</attribute>
          <attribute name="id">inputTiketId</attribute>
          <attribute name="min">0</attribute>
        </attributes>

      </control>

      <control class="dropdown" category="dropList">
        <type>button</type>
        <permission role="0">1</permission>
        <permission role="1">1</permission>
        <permission role="2">1</permission>
        <caption>
          <lang id="en">New</lang>
          <lang id="ru">New</lang>
          <lang id="ua">New</lang>
        </caption>
        <tooltip>
          <lang id="en"/>
          <lang id="ru"/>
          <lang id="ua"/>
        </tooltip>
        <icons>
          <icon class="svg" alt="image">new.svg</icon>
          <icon class="svg" alt="image">arrow-b.svg</icon>
        </icons>
        <disabled>0</disabled>
        <onpageevent>ShowListItems</onpageevent>
        <serverevent/>
        <showon>
          <tickets>1</tickets>
          <companies>0</companies>
          <consumer-users>0</consumer-users>
          <products>0</products>
          <staff>0</staff>
        </showon>

        <attributes>
          <attribute name="class">btn</attribute>
          <attribute name="class">btn-secondary</attribute>
          <attribute name="data-toggle">dropdown</attribute>
        </attributes>

        <listitems>

          <item category="ListItem">

            <permission role="0">100</permission>
            <permission role="1">1</permission>
            <permission role="2">0</permission>

            <caption>
              <lang id="en">New ticket</lang>
              <lang id="ru">New ticket</lang>
              <lang id="ua">New ticket</lang>
            </caption>

            <attributes>
              <attribute name="class">dropdown-item__sub</attribute>
              <attribute name="for">newTicket</attribute>
            </attributes>

            <icon/>
            <onpageevent>newTicket</onpageevent>
            <serverevent/>
          </item>

          <item category="ListItem">

            <permission role="0">1</permission>
            <permission role="1">1</permission>
            <permission role="2">0</permission>
            <caption>
              <lang id="en">New product</lang>
              <lang id="ru">New product</lang>
              <lang id="ua">New product</lang>
            </caption>
            <attributes>
              <attribute name="class">dropdown-item__sub</attribute>
              <attribute name="for">asideNewProduct</attribute>
            </attributes>
            <icon/>
            <onpageevent>asideNewProduct</onpageevent>
            <serverevent/>

          </item>

          <item category="ListItem">
            <permission role="0">1</permission>
            <permission role="1">1</permission>
            <permission role="2">0</permission>
            <caption>
              <lang id="en">New staff</lang>
              <lang id="ru">New staff</lang>
              <lang id="ua">New staff</lang>
            </caption>
            <attributes>
              <attribute name="class">dropdown-item__sub</attribute>
              <attribute name="for">asideNewUser</attribute>
            </attributes>
            <icon/>
            <onpageevent>asideNewUser</onpageevent>
            <serverevent/>
          </item>

          <item category="ListItem">
            <permission role="0">1</permission>
            <permission role="1">1</permission>
            <permission role="2">0</permission>
            <caption>
              <lang id="en">New consumer user</lang>
              <lang id="ru">New consumer user</lang>
              <lang id="ua">New consumer user</lang>
            </caption>
            <attributes>
              <attribute name="class">dropdown-item__sub</attribute>
              <attribute name="for">asideNewConsumerUser</attribute>
            </attributes>
            <icon/>
            <onpageevent>asideNewConsumerUser</onpageevent>
            <serverevent/>
          </item>

          <item category="ListItem">
            <permission role="0">1</permission>
            <permission role="1">1</permission>
            <permission role="2">0</permission>
            <caption>
              <lang id="en">New company</lang>
              <lang id="ru">New company</lang>
              <lang id="ua">New companyr</lang>
            </caption>
            <attributes>
              <attribute name="class">dropdown-item__sub</attribute>
              <attribute name="for">asideNewCompany</attribute>
            </attributes>
            <icon/>
            <onpageevent>asideNewCompany</onpageevent>
            <serverevent/>
          </item>

        </listitems>
      </control>
    </contrlos>
  </menucontrol>
</Configuration>

推荐答案

您在这里的位置正确,但是我认为您需要让XPath为您完成更多工作.尚不清楚您要完成什么.您需要以不同的方式查询所需的不同信息.如果您希望能够通过字段或菜单项ID提取标题或占位符,则需要制作不同的查询来做到这一点.有关如何执行此操作的示例,请参见 getPlaceholderForInputByIdAndLanguage getPlaceholderForInputByIdAndLanguage .如果要同时提取所有占位符和标题并将它们与它们所属的字段/菜单项相关联,则同样需要使用两个查询来执行此操作,我们可以在函数中执行此操作并将结果组合在一起-请参见 getFieldIdToLabelMapForLanguage .tootips很奇怪,因为下拉菜单有一个工具提示,但是似乎没有整个下拉菜单的任何标识符,因此我不确定您将如何查询特定的工具提示,或者将其与父提示相关联.您可以在 getTooltipsForLanguage 中看到如何简单地提取所有这些内容.

You're on the right track here, but I think you need to let XPath do more of the work for you. It's not entirely clear what you're trying to accomplish. The different pieces of information you want need to be queried in different ways. If you want to be able to pull the caption or placeholder by the field or menu item ID, you need to craft different queries to do that. See getPlaceholderForInputByIdAndLanguage and getPlaceholderForInputByIdAndLanguage for examples of how to do that. If you want to pull all of the placeholders and captions at the same time and correlate them to the fields/menu items they belong to, you likewise need to use two queries to do that, we can do that in a function and combine the results - see getFieldIdToLabelMapForLanguage. The tootips are odd because the drop down has a tooltip, but there doesn;t seem to be any identifier for the whole drop down, so I'm not sure how you would query for a specific tooltip, or correlate one to its parent. You can see how to simply extract all of them in getTooltipsForLanguage.

<?php

function getPlaceholderForInputByIdAndLanguage($rootElement, $fieldId, $language)
{
    $placeholder = $rootElement->xpath("//control[attributes/attribute[@name='id'][text() = '$fieldId']]//placeholder//lang[@id='{$language}']");
    return $placeholder[0];
}

function getCaptionForListItemByFiendNameAndLanguage($rootElement, $fieldName, $language)
{
    $caption = $rootElement->xpath("//control[@category='dropList']//item[attributes/attribute[@name='for'][text() = '$fieldName']]//caption//lang[@id='{$language}']");
    return $caption[0];
}

function getFieldIdToLabelMapForLanguage($rootElement, $language)
{
    $buffer = [];
    $items = $rootElement->xpath("//control[@category='input']");
    foreach($items as $currItem)
    {
        $id = $currItem->xpath("attributes/attribute[@name='id']");
        $placeholder = $currItem->xpath("placeholder//lang[@id='{$language}']");

        $buffer[(string) $id[0]] = (string) $placeholder[0];
    }

    $items = $rootElement->xpath("//control[@class='dropdown']//item");
    foreach($items as $currItem)
    {
        $for = $currItem->xpath("attributes/attribute[@name='for']");
        $caption = $currItem->xpath("caption//lang[@id='{$language}']");

        $buffer[(string) $for[0]] = (string) $caption[0];
    }

    return $buffer;
}

function getTooltipsForLanguage($rootElement, $language)
{
    $buffer = [];
    $tooltips = $rootElement->xpath("//tooltip//lang[@id='{$language}']");
    foreach($tooltips as $currTooltip)
    {
        $buffer[] = (string) $currTooltip[0];
    }

    return $buffer;
}

$pageMenu = simplexml_load_string($xml);

$placeholder = getPlaceholderForInputByIdAndLanguage($pageMenu, 'inputTiketId', 'en');
echo 'Placeholder for inputTiketId: '.$placeholder.PHP_EOL;

$caption = getCaptionForListItemByFiendNameAndLanguage($pageMenu, 'asideNewProduct', 'en');
echo 'Caption for asideNewProduct: '.$caption.PHP_EOL;

$idToLabelMap = getFieldIdToLabelMapForLanguage($pageMenu, 'en');

echo PHP_EOL.'All id to label mappings:'.PHP_EOL;
foreach ($idToLabelMap as $attributeName=>$caption)
{
    echo $attributeName.':'.$caption.PHP_EOL;
}

$tooltips = getTooltipsForLanguage($pageMenu, 'en');

echo PHP_EOL.'All tooltips:'.PHP_EOL;
foreach ($tooltips as $tooltip)
{
    echo $tooltip.PHP_EOL;
}

这篇关于通过xml文件中的属性ID从每个元素获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆