使用php按xpath排序

下面是我的Xml,我需要使用php中的xpath按年龄排序</ p>

< 预> <代码>&LT;家庭&GT;
&LT;年龄&GT;
&LT;年龄&GT; 22℃; /年龄&GT;
&LT;年龄→6&LT; /年龄&GT;
&LT;年龄大于5&LT; /年龄&GT;
&LT;年龄大于2&LT ; / Age&gt;
&lt; Age&gt; 12&lt; / Age&gt;
&lt; / Ages&gt;
&lt; / Guest&gt;
</ code> </ pre>

我尝试下面的代码,但不是 正在工作</ p>

  foreach($ xd-&gt; xpath('Guest / Ages [descendant :: Age]')$ xd_age)
{
echo $ xd_age-&gt ;年龄。',';
}
</ code> </ pre>

我需要如下</ p>

 年龄:2,5  ,6,12,22 
</ code> </ pre>
</ div>

展开原文

原文

Below is my Xml, i need to sort by age using xpath in php

<Guest>
<Ages>
<Age>22</Age>
<Age>6</Age>
<Age>5</Age>
<Age>2</Age>
<Age>12</Age>
</Ages>
</Guest>

i try below code, but not working

foreach ($xd->xpath('Guest/Ages[descendant::Age]') as $xd_age)
        {
            echo $xd_age->Age.',';
        }

I need like below

Ages : 2,5,6,12,22

douqilai4263
douqilai4263 使用SimpleXML/XPath排序XML的可能重复?
4 年多之前 回复
doz59484
doz59484 XPath本身是为浏览XML文档而设计的,它实际上并没有进行操作或排序。这个答案提供了一些从XML中排序某些值的选项。
4 年多之前 回复

1个回答

Consider an XSLT solution. As information, XSLT is a special purpose, declarative language designed specifically to manipulate XML files (not just a stylesheet for HTML). Practically all the general purpose languages, Java, C#, Perl, Python, VB, carry XSLT 1.0 processors including PHP -even command line programs like PowerShell and Bash. Various dedicated processors also exist such as open source Saxon (which can run XSLT 2.0 scripts) and Apache's Xalan.

PHP can both embed or call an external XSLT script (.xsl or .xslt) which by the way is a well-formed XML file. Below is an embedded solution:

// Load the XML source
$doc = new DOMDocument();
$doc->load('Input.xml');

$xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
            <xsl:output version="1.0" encoding="UTF-8" indent="yes" />
            <xsl:strip-space elements="*"/>

              <!-- IDENTITY TRANSFORM -->
              <xsl:template match="@*|node()">
                <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
              </xsl:template>  

              <!-- SORT AGE CHILDREN IN EACH AGES NODE -->
              <xsl:template match="Ages">
                <xsl:copy>
                  <xsl:apply-templates select="Age">
                    <xsl:sort select="." order="ascending" data-type="number"/>
                  </xsl:apply-templates>
                </xsl:copy>
              </xsl:template>

            </xsl:transform>';

$xsl = new DOMDocument;
$xsl->loadXML($xslstr);

// Configure the processor
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 

// Transform XML source
$newXml = $proc->transformToXML($doc);    
echo $newXml;

// Save output to file
$xmlfile = 'Output.xml';
file_put_contents($xmlfile, $newXml);

Output

<?xml version="1.0" encoding="UTF-8"?>
<Guest>
  <Ages>
    <Age>2</Age>
    <Age>5</Age>
    <Age>6</Age>
    <Age>12</Age>
    <Age>22</Age>
  </Ages>
</Guest>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问