xslt转换错误与Web服务soap返回

时间:2015-08-26 11:35:28

标签: c# web-services xslt soap

我有一个来自webservice的返回soap xml:

<?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值吗? 我的代码问题是什么?

1 个答案:

答案 0 :(得分:1)

您的问题在于命名空间。您已经正确地尝试在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">