使用web服务soap返回的XSLT转换错误

本文关键字:XSLT 转换 错误 返回 web 服务 soap 使用 | 更新日期: 2023-09-27 18:09:58

我有一个返回soap xml从webservice:

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body><GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
    <GetCitiesByCountryResult>
      <NewDataSet>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Abadan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Omidieh</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Gach Saran Du Gunbadan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Masjed-Soleyman</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Ahwaz</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Bushehr Civ / Afb</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Bandar Lengeh</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Kermanshah</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Ghasre-Shirin</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Sanandaj</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Kashan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Esfahan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Shahre-Kord</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Rasht</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Arak</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Tehran-Mehrabad</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Ghazvin</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Semnan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Bandarabbass</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Kerman</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Bam</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Birjand</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Torbat-Heydarieh</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Mashhad</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Bojnourd</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Sabzevar</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Tabas</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Gorgan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Ramsar</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Abadeh</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Fasa</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Shiraz</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Khoy</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Orumieh</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Saghez</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Tabriz</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Zanjan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Yazd</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Zabol</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Chahbahar</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Zahedan</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Iranshahr</City>
  </Table>
  <Table>
    <Country>Iran, Islamic Republic of</Country>
    <City>Jask</City>
  </Table>
</NewDataSet>
</GetCitiesByCountryResult>
</GetCitiesByCountryResponse>
</soap:Body>
</soap:Envelope>

我为它写了这样的xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:p="http://www.webservicex.net">
  <xsl:template match="soap:Envelope/soap:Body/p:GetCitiesByCountryResponse/p:GetCitiesByCountryResult/p:NewDataSet">
    <xsl:for-each select="p:Table">
      <table>
        <Country>
          <xsl:value-of select="p:Country"/>
        </Country>
        <City>
          <xsl:value-of select="p:City"/>
        </City>
      </table>
    </xsl:for-each>
  </xsl:template>  
</xsl:stylesheet>

 <?xml version="1.0" encoding="utf-8"?>Iran, Islamic Republic ofAbadanIran, Islamic Republic ofOmidiehIran, Islamic Republic ofGach Saran Du GunbadanIran, Islamic Republic ofMasjed-SoleymanIran, Islamic Republic ofAhwazIran, Islamic Republic ofBushehr Civ / AfbIran, Islamic Republic ofBandar LengehIran, Islamic Republic ofKermanshahIran, Islamic Republic ofGhasre-ShirinIran, Islamic Republic ofSanandajIran, Islamic Republic ofKashanIran, Islamic Republic ofEsfahanIran, Islamic Republic ofShahre-KordIran, Islamic Republic ofRashtIran, Islamic Republic ofArakIran, Islamic Republic ofTehran-MehrabadIran, Islamic Republic ofGhazvinIran, Islamic Republic ofSemnanIran, Islamic Republic ofBandarabbassIran, Islamic Republic ofKermanIran, Islamic Republic ofBamIran, Islamic Republic ofBirjandIran, Islamic Republic ofTorbat-HeydariehIran, Islamic Republic ofMashhadIran, Islamic Republic ofBojnourdIran, Islamic Republic ofSabzevarIran, Islamic Republic ofTabasIran, Islamic Republic ofGorganIran, Islamic Republic ofRamsarIran, Islamic Republic ofAbadehIran, Islamic Republic ofFasaIran, Islamic Republic ofShirazIran, Islamic Republic ofKhoyIran, Islamic Republic ofOrumiehIran, Islamic Republic ofSaghezIran, Islamic Republic ofTabrizIran, Islamic Republic ofZanjanIran, Islamic Republic ofYazdIran, Islamic Republic ofZabolIran, Islamic Republic ofChahbaharIran, Islamic Republic ofZahedanIran, Islamic Republic ofIranshahrIran, Islamic Republic ofJask

我应该使用任何其他标签在c#转换?我有xml值的问题吗?我的代码有什么问题?

使用web服务soap返回的XSLT转换错误

您的问题是名称空间。您已经正确地尝试在XSLT中引用名称空间,但是没有使用正确的URI

在XML中,命名空间URI是这样声明的。

xmlns="http://www.webserviceX.NET"

但是在您的XSLT中,您这样声明它…

xmlns:p="http://www.webservicex.net"

XSLT区分大小写,因此这两个名称空间被认为是不同的。这意味着您当前的模板不匹配任何内容,因此XSLT的内置模板会启动,最终只输出文本。

您只需要将XSLT更改为与XML

相同
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
     xmlns:p="http://www.webserviceX.NET" 
     exclude-result-prefixes="soap p">