【问题标题】:Apply XSLT with php, but keep the original xml structure将 XSLT 与 php 一起应用,但保留原始 xml 结构
【发布时间】:2018-03-05 17:35:31
【问题描述】:

我有一个结构化的 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 的结构比那里显示的要复杂得多,而且我不确定所有实例都会具有所有可能的字段(信息是从外部系统中提取的)。

谢谢

【问题讨论】:

  • @Parfait 我按照你的要求添加了

标签: php xml xslt


【解决方案1】:

目前,您的 XSLT 仅在一个模板中输出带有 &lt;xsl:copy&gt; 命令的根节点。所有其他节点都不会被重写。要保留原始结构,只需合并复制文档的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>

【讨论】:

  • @NimrodYanai ...更新后的 XSLT 解决了您的问题吗?
  • 谢谢!这就像一个魅力!我将向使用此解决方案的人补充一点,XML 传输实际上不是 XML,它以字符串形式出现,因此当您返回结果时,请确保在您的对象上使用 ->asXml()!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
  • 1970-01-01
  • 2011-01-17
  • 2014-01-08
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
相关资源
最近更新 更多