【问题标题】:Display XML DATA as HTML with PHP使用 PHP 将 XML 数据显示为 HTML
【发布时间】:2013-12-01 11:11:30
【问题描述】:

我希望有人可以帮助我我被困了一段时间..

我是附属公司,我从提供的获取 xml 数据。我正在尝试转换此数据并在我的网站上显示为 html。

我已经测试了我的 xsl,并且可以确认 xsl 正在按我想要的方式显示数据。

我的问题是从 urla 中提取数据并使用 xsl 转换数据以显示 html 的 php 代码不起作用。

<?php


    $context = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));

    $url = 'http://merchandising.expediaaffiliate.com/campaign/?campaignId=85413&cid=409870 ';

    $xml = file_get_contents($url, false, $context);

    $xml = simplexml_load_string($xml);

    $doc = new DOMDocument();

    $doc->load('http://sitename/hotels.xslt');

    $xsl = new XSLTProcessor();

    $xsl->importStyleSheet($doc);

    $doc->load($xml);

    echo $xsl->transformToXML($doc);


    ?>

我的 XSl 如下:

<?xml version="1.0" encoding="ISO-8859-1"?>

<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">

     <xsl:for-each select="HotelAvailabilityListResults">
    <xsl:for-each select="Hotel">


      <div style="background-color:teal;color:white;padding:4px">
        <span style="font-weight:bold"><xsl:value-of 
    select="name" 
    disable-output-escaping="yes"/></span>
        - <xsl:value-of select="city"/>
      </div>
      <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
        <p><xsl:value-of select="shortDescription" 
    disable-output-escaping="yes"/>.
        <span style="font-style:italic">
          <xsl:value-of select="address1" 
    disable-output-escaping="yes"/>
        </span>.</p>
 <p>
        <span style="font-style:italic">
       <xsl:element name="img">
    <xsl:attribute name="src">

    <xsl:value-of select="thumbNailUrl"/>
    </xsl:attribute>
    </xsl:element>
        </span>.</p>
      </div>
<span style="font-style:italic">
          <xsl:value-of select="HotelProperty/promoDescription"  />
        </span>
<span style="font-style:italic">
          <xsl:value-of select="ValueAdds/ValueAdd"  />
        </span>
<div>
 <span style="font-style:italic">
       <xsl:element name="a">
    <xsl:attribute name="href">

    <xsl:value-of select="hotelUrl"/>
    </xsl:attribute>
    </xsl:element>
        </span>
</div>
    </xsl:for-each>
    </xsl:for-each>



  </body>
</html>

【问题讨论】:

  • 您能否详细解释一下您的设置中的不工作是什么意思?
  • 除非有充分的理由,否则为什么不让客户处理转换呢?只需将样式表声明添加到 XML-&lt;?xml-stylesheet type="text/xsl" href="(url of your .xsl file)" ?&gt;

标签: php xml xslt xhtml affiliate


【解决方案1】:

你有两个问题,首先 XML 不好,描述不是 CDATA 部分,而是文本节点。好像有人逃过两次。要在 XSLT 中使用它们,您需要修复它们。

$context = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'http://merchandising.expediaaffiliate.com/campaign/?campaignId=85413&cid=409870 ';
$xml = file_get_contents($url, false, $context);

$xmlDom = new DOMDocument();
$xmlDom->loadXml($xml);

$xpath = new DOMXPath($xmlDom);

/* repair double escaped cdatas */
$translationTable = array_flip(
  get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_HTML5, 'UTF-8')
);
$nodes = $xpath->evaluate('//text()[starts-with(., "<![CDATA[")]');  
foreach ($nodes as $node) {
  $fragment = $xmlDom->createCDATASection(
    str_replace(
      array_keys($translationTable),
      $translationTable,
      substr($node->nodeValue, 9, -3)
    )
  );
  $node->parentNode->appendChild($fragment);
  $node->parentNode->removeChild($node);
}

$xslDom = new DOMDocument();
$xslDom->load('template.xsl');

$xsl = new XSLTProcessor();
$xsl->importStyleSheet($xslDom);

echo $xsl->transformToXML($xmlDom);

您问题中的模板不是 XSLT 样式表。 XSLT 的根元素是样式表。您还需要匹配 XML 中的某些内容。

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

  <xsl:output 
    method="html"
    encoding="UTF-8" 
    standalone="yes"
    indent="yes"/>

  <xsl:template match="/">
    <html >
      <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
        <xsl:for-each select="HotelAvailabilityListResults">
          <xsl:for-each select="Hotel">
            <div style="background-color:teal;color:white;padding:4px">
              <span style="font-weight:bold">
                <xsl:value-of select="name"/>
              </span>
              - <xsl:value-of select="city"/>
            </div>
            <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
              <p>
                <xsl:value-of select="shortDescription" disable-output-escaping="yes"/>.
                <span style="font-style:italic">
                  <xsl:value-of select="address1"/>
                </span>
              </p>
              <p>
                <span style="font-style:italic">
                  <img src="{thumbNailUrl}"/>
                </span>.
              </p>
            </div>
            <span style="font-style:italic">
              <xsl:value-of select="HotelProperty/promoDescription"/>
            </span>
            <span style="font-style:italic">
              <xsl:value-of select="ValueAdds/ValueAdd"  />
            </span>
            <div>
              <span style="font-style:italic">
                <a href="{hotelUrl}">
                  <xsl:value-of select="hotelUrl"/>
                </a>
              </span>
            </div>
          </xsl:for-each>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

XSLT 的一些额外提示:

  1. 小心disable-out-escaping="yes",这会打开你 HTML 注入。你必须相信来源。尽可能避免。
  2. 您可以在属性值中使用{} 来访问Xpath,从&lt;xsl:element/&gt;&lt;xsl:attribute/&gt; 中消除了很多混乱

【讨论】:

    猜你喜欢
    • 2011-11-11
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多