SimpleXML을 사용한 자동 반복 구조?
문제
나는 다음과 같은 간단한 XML 구조로 양식의 요소를 정의하고 있습니다.
<subtab id="page_background" label="Page Background" prefix="page">
<input label="Background color" field="bgcolor" type="color"/>
<input label="Background image" field="bgimage" type="image"/>
<space />
</subtab>
etc.
절대적으로 동일한 정보를 포함하는 큰 블록이 있습니다. 이로 인해 XML 파일을 매우 번거롭게 만들고 협력하고 있습니다.
XML Parser (내 경우 SimpleXML)에게 다른 분기의 내용을 찾아 보라고 지시하는 기본 XML "Copy + Paste"Construct / Command / Statement가 있습니까?
의사 코드에서 :
<subtab id="content_background" copyFrom="../page_background">
<!-- sub-elements from "page_background" are magically copied here
by the parser -->
</subtab>
해결책
XML은 형식 일 뿐이므로 사본 명령이 없습니다. XSLT는 그렇게 할 수 있지만 아마도 당신의 요구에 너무 복잡 할 것입니다.
내 조언 : 보일러 플레이트 요소를 추가하는 PHP 기능을 만듭니다. 예를 들어:
function default_elements(SimpleXMLElement $node)
{
$default = array(
'input' => array(
array('label'=>"Background color", 'field'=>"bgcolor", 'type'=>"color"),
array('label'=>"Background image", 'field'=>"bgimage", 'type'=>"image")
),
'space' => array(
array()
)
);
foreach ($default as $name => $elements)
{
foreach ($elements as $attrs)
{
$new = $node->addChild($name);
foreach ($attrs as $k => $v)
{
$new[$k] = $v;
}
}
}
}
다른 팁
형식과 사용으로 SimplexMlelement를 생성합니다. 클론, 매번 변경된 속성 또는 하위 태그를 업데이트합니다.
제휴하지 않습니다 StackOverflow