Nimrod Yanai
Nimrod Yanai

Reputation: 819

Apply XSLT with php, but keep the original xml structure

I have a structured XML, with several levels of parent-child nodes (this is an example of one of the items):

<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>

I am trying to use xslt transformation on the XML to sort it via two nodes. I have created the following 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>

and am using the following code to implement it:

//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());

However, once I use "transformToDoc(), the xml loses its original structure and becomes a basic xml with a parent node: , and a long string inside with the values of the original nodes. I can see that the XSLT did its work correctly (the values are indeed sorted in the correct order):

<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>

I need to preserve the original xml structure, while applying the xslt.

Which command should I use instead of "transformToDoc()"? I tried using transformToXml(). but it turns out that command returns a string, not even and XML (in complete defiance to its name).

At first I thought to use an array, as shown for example here. But the structure of my XML is far more complicated than what is shown there, and I'm not sure all instances will have all the possible fields (the information is extracted from an external system).

Thank you

Upvotes: 1

Views: 179

Answers (1)

Parfait
Parfait

Reputation: 107767

Currently, your XSLT outputs only the root node with <xsl:copy> command in one template. All else nodes are not rewritten. To retain original structure, simply incorporate the Identity Transform template which copies document as is. Then, any apply-templates call will output node, text, and any attributes.

<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>

Upvotes: 1

Related Questions