使用php应用XSLT,但保留原始的xml结构

问题描述 投票:1回答:1

我有一个结构化的XML,有几个级别的父子节点(这是其中一个项目的示例):

<item-data>
<item>
<rec-key>001373746000010</rec-key>
<barcode>000091939685</barcode>
<sub-library>Aranne Library</sub-library>
<collection>3</collection>
<item-status>98</item-status>
<note/>
<call-no-1>K-05200</call-no-1>
<call-no-2/>
<description>1996 VOL.1</description>
<chronological-i>1996</chronological-i>
<chronological-j/>
<chronological-k/>
<enumeration-a>1</enumeration-a>
<enumeration-b/>
<enumeration-c/>
<library>BGU50</library>
<on-hold>N</on-hold>
<requested>N</requested>
<expected>N</expected>
</item>
</item-data>

我试图在XML上使用xslt转换来通过两个节点对其进行排序。我创建了以下XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="item-data">
  <xsl:copy>
    <xsl:apply-templates select="item">
      <!-- sort first on the ACCT_DOC_NUM -->
      <xsl:sort select="sub-library" data-type="text" order="ascending" />
      <!-- for lines with the same doc num, sort by line -->
      <xsl:sort select="description" data-type="text" order="ascending" />
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

并使用以下代码来实现它:

//Load sorting XSL
    $xsl = new DOMDocument();
    $xsl->load("SortXsl.xsl");

//Apply sorting XSL
    $sortxml = new DOMDocument();
    $proc = new XSLTProcessor();
    $proc->importStylesheet($xsl);
    $sortxml = $proc->transformToDoc($xml);
    $xml2 = new SimpleXMLElement ($sortxml->saveXML());

但是,一旦我使用“transformToDoc(),xml就失去了它的原始结构,变成了一个带有父节点的基本xml:和一个带有原始节点值的长字符串。我可以看到XSLT正确地工作了(这些值确实按正确的顺序排序):

<item-data>
001373746000540 Aranne Library OR BGU50 N N N 001373746000580 Aranne Library OR BGU50 N N N 001373746000010 000091939685 Aranne Library 3 98 K-05200 1996 VOL.1 1996 1 BGU50 N N N 001373746000020 000090166440 Aranne Library 90 K-05200 1997 VOL. 2 1997 2 1 BGU50 N N N A N 16/08/2016 0959 001373746000520 000099608187 Aranne Library 3 98 K-05200 1997 VOL. 2 1997 2 2 BGU50 N N N 001373746000040 000090166433 Aranne Library 90 K-05200 1998 VOL. 3 1998 3 BGU50 N N N A N 16/08/2016 0959 001373746000050 000100005523 Aranne Library 3 98 K-05200 1998 VOL.3 1998 3 BGU50 N N N 001373746000060 000100042573 Aranne Library 3 98 K-05200 1999 VOL.4 1999 4 BGU50 N N N 001373746000070 000092187634 Aranne Library 3 98 K-05200 2000 VOL. 5 2000 5 BGU50 N N N 001373746000080 000093270779 Aranne Library 3 98 K-05200 2001 VOL. 6 2001 6 BGU50 N N N 001373746000090 000093270786 Aranne Library 3 98 K-05200 2002 VOL.7 2002 7 BGU50 N N N 001373746000100 000094816624 Aranne Library 3 98 K-05200 2003 VOL.8 2003 8 BGU50 N N N 001373746000110 000094816686 Aranne Library 3 98 K-05200 2003 VOL.9 2003 9 BGU50 N N N 001373746000120 000094438987 Aranne Library 3 98 K-05200 2004 VOL.10 2004 10 BGU50 N N N 001373746000130 000094838527 Aranne Library 3 98 K-05200 2004 VOL.11 2004 11 BGU50 N N N 001373746000170 000094839333 Aranne Library 3 98 K-05200 2004 VOL.12 2004 12 BGU50 N N N 001373746000300 000095960241 Aranne Library 90 K-05200 2004 VOL.12 2004 12 BGU50 N N N A N 30/11/2016 1500 001373746000200 000095255873 Aranne Library 3 98 K-05200 2005 VOL.13 2005 13 BGU50 N N N 001373746000280 000095960135 Aranne Library 90 K-05200 2005 VOL.13 2005 13 BGU50 N N N A N 30/11/2016 1500 001373746000230 000095224893 Aranne Library 3 98 K-05200 2006 VOL.14 2006 14 BGU50 N N N 001373746000270 000095960210 Aranne Library 90 K-05200 2006 VOL.14 2006 14 BGU50 N N N A N 30/11/2016 1500 001373746000240 000098784219 Aranne Library 3 98 K-05200 2006 VOL.15 2006 15 BGU50 N N N 001373746000260 000095960180 Aranne Library 90 K-05200 2007 VOL.15 2007 15 BGU50 N N N A N 30/11/2016 1500 001373746000250 000096769935 Aranne Library 3 98 k-05200 2007 VOL.16 2007 16 BGU50 N N N 001373746000290 000095960142 Aranne Library 90 K-05200 2007 VOL.16 2007 16 BGU50 N N N A N 30/11/2016 1500 001373746000310 000096295441 Aranne Library 3 98 K-05200 2008 VOL.17 2008 17 BGU50 N N N 001373746000320 000097209898 Aranne Library 3 98 k-05200 2008 VOL.18 2008 18 BGU50 N N N 001373746000330 000097209928 Aranne Library 3 98 K-05200 2009 VOL.19 2009 19 BGU50 N N N 001373746000340 000097209959 Aranne Library 3 98 K-05200 2009 VOL.20 2009 20 BGU50 N N N 001373746000350 000097279969 Aranne Library 3 98 K-05200 2010 VOL.21 2010 21 BGU50 N N N 001373746000360 000098784189 Aranne Library 3 98 k-05200 2010 VOL.22 2010 22 BGU50 N N N 001373746000370 000098337941 Aranne Library 3 98 K-05200 2010 VOL.23 2010 23 BGU50 N N N 001373746000380 000098338016 Aranne Library 3 98 K-05200 2011 VOL.24 2011 24 BGU50 N N N 001373746000390 000098425150 Aranne Library 3 98 K-05200 2011 VOL.25 2011 25 BGU50 N N N 001373746000400 000098618286 Aranne Library 3 98 K-05200 2012 VOL.26 2012 26 BGU50 N N N 001373746000430 000098610808 Aranne Library 3 98 K-05200 2012 VOL.27 2012 27 BGU50 N N N 001373746000440 000098738366 Aranne Library 3 98 K-05200 2012 VOL.28 2012 28 BGU50 N N N 001373746000450 000098919260 Aranne Library 3 98 K-05200 2013 VOL.29 2013 29 BGU50 N N N 001373746000460 000098919239 Aranne Library 3 98 K-05200 2013 VOL.30 2013 30 BGU50 N N N 001373746000470 000099026790 Aranne Library 3 98 K-05200 2014 VOL.31 2014 31 BGU50 N N N 001373746000480 000099233860 Aranne Library 3 98 K-05200 2014 VOL.32 2014 32 BGU50 N N N 001373746000490 000099382766 Aranne Library 3 98 K-05200 2015 VOL.33 2015 33 BGU50 N N N 001373746000500 000099601645 Aranne Library 3 98 K-05200 2016 VOL.34 2016 34 BGU50 N N N 001373746000510 000099725327 Aranne Library 3 98 K-05200 2016 VOL.35 2016 35 BGU50 N N N 001373746000530 000099792794 Aranne Library 3 98 K-05200 2017 VOL.36 2017 36 BGU50 N N N 001373746000560 000099792725 Aranne Library 3 98 K-05200 2017 VOL.37 2017 37 BGU50 N N N 001373746000570 Aranne Library 98 2018 VOL.38 2018 38 BGU50 N N Y
</item-data>

我需要保留原始的xml结构,同时应用xslt。

我应该使用哪个命令而不是“transformToDoc()”?我尝试使用transformToXml()。但事实证明,该命令返回一个字符串,而不是XML(完全无视其名称)。

起初我想使用一个数组,例如here。但是我的XML结构比那里显示的要复杂得多,而且我不确定所有实例都会有所有可能的字段(信息是从外部系统中提取的)。

谢谢

php xml xslt
1个回答
1
投票

目前,您的XSLT仅在一个模板中输出带有<xsl:copy>命令的根节点。所有其他节点都不会被重写。要保留原始结构,只需合并按原样复制文档的Identity Transform模板即可。然后,任何apply-templates调用都将输出节点,文本和任何属性。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="item-data">
    <xsl:copy>
      <xsl:apply-templates select="item">
        <!-- sort first on the ACCT_DOC_NUM -->
        <xsl:sort select="sub-library" data-type="text" order="ascending" />
        <!-- for lines with the same doc num, sort by line -->
        <xsl:sort select="description" data-type="text" order="ascending" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.