在PHP中使用XSLT转换XML [英] Transform XML with XSLT in PHP

查看:87
本文介绍了在PHP中使用XSLT转换XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并两个xml文件,并使用XSLT文件将它们转换成XHTML页面.我以前没有做过,也无法弄清楚该怎么做.到目前为止,这是我仅有的一个xml文件:

I'm trying to combine two xml-files and with a XSLT-file transform them into a XHTML-page. I have not done this before and can´t figure out how to do it. This is what I have so far, with just one xml-file:

<?php

$xsl = new DOMDocument();
$xsl->load("file.xsl");
$inputdom = new DomDocument();
$inputdom->load("file.xml");

$proc = new XSLTProcessor();
$xsl = $proc->importStylesheet($xsl);
$proc->setParameter(null, "", "");

$newdom = $proc->transformToDoc($inputdom);
print $newdom-> saveXML();

xsl文件,带有:ss命名空间.我不太确定如何使用ss前缀?

xsl-file with the :ss namespace. I'm not really sure how to use the ss prefix?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

<xsl:template match="/">
  <html>
  <body>

  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Title2</th>
    </tr>
    <xsl:for-each select="something/some">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="title2"/></td>
    </tr>
   </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

xml文件

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="file.xsl"?>
<something>
  <some>
    <Firstname>Peter</Firstname>
    <Lastname>Anderson</Lastname>
  <some>
.....

如果有人能朝着正确的方向踢我,我将不胜感激.

If somebody could give me a kick in the right direction I would be grateful.

这是第二个xml文件:

Here is the second xml-file:

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Created>2012-09-25T13:44:01Z</Created>
  </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>14060</WindowHeight>
  <WindowWidth>25040</WindowWidth>
  <WindowTopX>25540</WindowTopX>
  <WindowTopY>4100</WindowTopY>
  <Date1904/>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s62">
   <Font ss:FontName="Courier" ss:Color="#000000"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Workbook1.csv">
  <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="79" x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="65" ss:DefaultRowHeight="15">
   <Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="43"/>
   <Column ss:AutoFitWidth="0" ss:Width="113"/>
   <Column ss:Index="5" ss:AutoFitWidth="0" ss:Width="220"/>
   <Row ss:Index="6">
    <Cell ss:Index="3" ss:StyleID="s62"/>
   </Row>
   <Row>
    <Cell ss:Index="3" ss:StyleID="s62"/>
   </Row>
    <Row>
    <Cell ss:Index="3" ss:StyleID="s62"/>
   </Row>
   <Row>
    <Cell ss:Index="2"><Data ss:Type="String">id</Data></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">latitude</Data></Cell>
    <Cell><Data ss:Type="String">longitude</Data></Cell>
   </Row>
   <Row>
    <Cell ss:Index="2"><Data ss:Type="Number">8</Data></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="Number">57.4999</Data></Cell>
    <Cell><Data ss:Type="Number">15.8280</Data></Cell>
   </Row>
.....

所以我的问题是如何将两个XML文件中的数据与XSLT合并?我希望结果是XHTML页面上表中的数据.

So my question is how to combine data from both XML-files with the XSLT? I want the result to be the data in a table on a XHTML-page.

推荐答案

这可能是对部分问题的解答.

This might be an answer to part of your question.

关于使用两个XML文件,您有两个选择.您可以将两个XML文件合并为一个较大的文件,然后对其进行转换.另外,您可以使用XSLT document()函数从XSLT内加载XML文件之一.

With regard to using two XML files, you have a couple of options. You could combine the two XML files into one larger one, then apply a transform to that. Alternatively, you could use the XSLT document() function to load one of the XML files from within an XSLT.

1.制作一个大型XML文档

<?php

// XML
$x1 = file_get_contents("file1.xml");
$x2 = file_get_contents("file2.xml");
$xml_doc = new DOMDocument();
$xml_doc->loadXML("<root><x1>$x1</x1><x2>$x2</x2></root>");

// XSL
$xsl_doc = new DOMDocument();
$xsl_doc->load("file.xsl");

// Proc
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl_doc);
$newdom = $proc->transformToDoc($xml_doc);

print $newdom->saveXML();

?>

2.使用XSTL document()函数

2. Use the XSTL document() function

<?php

// XML
$xml_doc = new DOMDocument();
$xml_doc->load("file1.xml");


// XSL
$xsl_doc = new DOMDocument();
$xsl_doc->load("file.xsl");

// Proc
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl_doc);
$newdom = $proc->transformToDoc($xml_doc);

print $newdom->saveXML();

?>


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="file2" select="document('file2.xml')/*"/>

  <xsl:template match="/">
    <xsl:copy-of select="$file2"/>
  </xsl:template>

</xsl:transform>

与第二种技术相比,我倾向于使用第一种技术.我不喜欢将文件名硬编码到XSLT模板中.当我确实使用第二种方法时,通常会传入文件名作为外部参数,以避免将其硬编码在XSLT中.

I tend to use the first technique more than the second. I don't like hardcoding filenames into XSLT templates. When I do use the second method, I would usually pass in the filename as an external parameter to avoid having it hardcoded in the XSLT.

这篇关于在PHP中使用XSLT转换XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆