SimpleXML - how to get attributes with namespace? + XML vs. preg_*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WebCM

    SimpleXML - how to get attributes with namespace? + XML vs. preg_*

    There are lots of problems with XHTML parsing in PHP by XML functions.
    I've just solved most of them. However, how to get value of attribute
    with namespace?

    <input type="checkbox" id="something" f3:var="config. item" />

    <?php
    foreach(xpath('//input[@id="ms"]') as $item) echo $item['f3:var'];
    ?>

    It doesn't work. Even $f3->item['var'].

    I wonder whether parsing XML using preg_* and str_* wouldn't better.
    Though i'm only creating a template system. XML parsing is only used
    for FORMS with IF conditions generating. Example output:

    <input type="checkbox" id="something" <?php if($config['item']) echo
    'checked="check ed" ';?>/>

    OR:

    <?php if($config['item']) echo '<input type="checkbox" id="something"
    checked="checke d" />'; else echo '<input type="checkbox"
    id="something" />'; ?>

    There is only 1 (later maybe more) new attribute: f3:var - which can
    be situated in <form(for all checkbox, radio and select) or in
    input / select elements.

    Give me some advices. What is better for this purpose - XML in PHP or
    preg_* / str_*?
  • petersprc

    #2
    Re: SimpleXML - how to get attributes with namespace? + XML vs.preg_*

    Hi,

    Pass the namespace URI to the attributes method to access prefixed
    attributes:

    $attrs = $node->attributes('ht tp://example.test/myNamespace');
    echo $attrs['myAttribute'];

    More info at:



    Regards,

    John Peters

    On Apr 25, 2:25 pm, WebCM <webcm...@gmail .comwrote:
    There are lots of problems with XHTML parsing in PHP by XML functions.
    I've just solved most of them. However, how to get value of attribute
    with namespace?
    >
    <input type="checkbox" id="something" f3:var="config. item" />
    >
    <?php
    foreach(xpath('//input[@id="ms"]') as $item) echo $item['f3:var'];
    ?>
    >
    It doesn't work. Even $f3->item['var'].
    >
    I wonder whether parsing XML using preg_* and str_* wouldn't better.
    Though i'm only creating a template system. XML parsing is only used
    for FORMS with IF conditions generating. Example output:
    >
    <input type="checkbox" id="something" <?php if($config['item']) echo
    'checked="check ed" ';?>/>
    >
    OR:
    >
    <?php if($config['item']) echo '<input type="checkbox" id="something"
    checked="checke d" />'; else echo '<input type="checkbox"
    id="something" />'; ?>
    >
    There is only 1 (later maybe more) new attribute: f3:var - which can
    be situated in <form(for all checkbox, radio and select) or in
    input / select elements.
    >
    Give me some advices. What is better for this purpose - XML in PHP or
    preg_* / str_*?

    Comment

    Working...