在PHP中使用XsltProcessor进行xslt属性和URL编码(或解码)时出现问题

在PHP中使用XsltProcessor进行xslt属性和URL编码(或解码)时出现问题,php,url,attributes,xslt,Php,Url,Attributes,Xslt,我试过几种方法使它工作 这是我的问题 我有这个XML内容 <?xml version="1.0" encoding="UTF-8"?> <csw:GetRecordByIdResponse xmlns:csw="http://www.opengis.net/cat/csw/2.0.2"> <csw:Record xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows"

我试过几种方法使它工作

这是我的问题

我有这个XML内容

<?xml version="1.0" encoding="UTF-8"?>
<csw:GetRecordByIdResponse xmlns:csw="http://www.opengis.net/cat/csw/2.0.2">
  <csw:Record xmlns:dct="http://purl.org/dc/terms/" xmlns:ows="http://www.opengis.net/ows" xmlns:geonet="http://www.fao.org/geonetwork" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <dc:identifier>b6ebec90-96ac-477f-bfcf-ccef920e3e99</dc:identifier>
  <dc:title>Physiographic Map of North and Central Eurasia (Sample record, please remove!)</dc:title>
  <dct:abstract>Physiographic maps ... </dct:abstract>
  <dc:rights>copyright</dc:rights>
  <dc:language />
  <dc:source />
  <ows:BoundingBox crs="urn:ogc:def:crs:::Lambert Azimuthal Projection">
    <ows:LowerCorner>156 -3</ows:LowerCorner>
    <ows:UpperCorner>37 83</ows:UpperCorner>
  </ows:BoundingBox>
  <dc:URI protocol="image/gif" name="thumbnail">resources.get?id=14&amp;fname=phy_s.gif&amp;access=public</dc:URI>
  </csw:Record>
</csw:GetRecordByIdResponse>

b6ebec90-96ac-477f-bfcf-ccef920e3e99
欧亚大陆北部和中部地形图(样本记录,请删除!)
地形图。。。
版权
156 -3
37 83
资源。获取?id=14&;fname=phy_.gif&;访问=公共
我试图使用
resources.get?id=14&;的值;fname=phy_.gif&;access=public
作为img标签的src

请注意,XML中的值是经过编码的(&而不是&)

下面是我用来将XML转换为HTML的xsl

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:csw="http://www.opengis.net/cat/csw/2.0.2"
xmlns:dct="http://purl.org/dc/terms/"
xmlns:geonet="http://www.fao.org/geonetwork"
xmlns:ows="http://www.opengis.net/ows"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:fn="http://www.w3.org/2005/xpath-functions"   >

<xsl:output method="html" encoding="UTF-8" indent="yes" />
<xsl:template match="csw:GetRecordByIdResponse">
    <html>
        <head>
            <meta charset="utf-8" />
        </head>
        <body>
                <xsl:apply-templates select="csw:Record" />
        </body>
    </html>
</xsl:template>

<xsl:template match="csw:Record">
    <h1><xsl:value-of select="dc:title" /><span class='identifier'><xsl:value-of select="dc:identifier" /></span></h1>
    <p class='description'><xsl:value-of select="dc:title" /></p>
    <ul>
        <xsl:for-each select="dc:URI">
            <xsl:if test="@name = 'thumbnail'">
                <li>
                    <xsl:value-of select="." disable-output-escaping="yes" />               
                    <xsl:element name="img">
                        <xsl:attribute name="src"><xsl:text>http://localhost:8080/geonetwork/srv/en/</xsl:text><xsl:value-of select="." disable-output-escaping="yes" /></xsl:attribute>
                    </xsl:element>  
                </li>
            </xsl:if>
        </xsl:for-each>
    </ul>
</xsl:template>

  • http://localhost:8080/geonetwork/srv/en/

我正在使用PHP来处理它

<?php
$xp = new XsltProcessor();
// create a DOM document and load the XSL stylesheet
$xsl = new DomDocument;
$xsl->load('sample.xsl');

// import the XSL styelsheet into the XSLT process
$xp->importStylesheet($xsl);

$xml_doc = new DomDocument;
$xml_doc->loadXML(file_get_contents('sample.xml'));
// transform the XML into HTML using the XSL file
if ($html = $xp->transformToXML($xml_doc))
    echo $html;
?>
loadXML(文件获取内容('sample.xml');
//使用XSL文件将XML转换为HTML
如果($html=$xp->transformToXML($xml\u doc))
echo$html;
?>
我仍然得到这个输出<代码>
  • resources.get?id=14&fname=phy\s.gif&access=public
  • 。。。请注意,src属性中的值仍然具有
    &
    而另一个输出正确
    &

    提前感谢,, 杰克

    请注意,href属性中的值仍然具有
    &
    而另一个输出正确
    &

    否,正确且必需的书写是
    &在输出中的href属性中。我懒于打字,有关解释,请参见问题和


    即使你错误地认为它是正确的另一个(
    &
    )也必须写成
    &正确。

    杰克,我可能遗漏了什么,但拥有
    &在输出中的href属性中。@hakre它不仅可以,实际上是必需的。@Tomalak:说得好,真的!-值得回答。