我正在尝试从Magyar Nemzeti Bank下载并转换肥皂回复。我设法让下载部分正确,现在我遇到了xslt转换的问题。我想将消息转换为xml,以便我可以反序列化它。
SOAP响应:
<?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>
<GetCurrentExchangeRatesResponse xmlns="http://www.mnb.hu/webservices/">
<GetCurrentExchangeRatesResult>
<MNBCurrentExchangeRates>
<Day date="2013-12-03">
<Rate curr="AUD" unit="1">203,5</Rate>
<Rate curr="BGN" unit="1">154,82</Rate>
<Rate curr="BRL" unit="1">94,69</Rate>
<Rate curr="CAD" unit="1">209,79</Rate>
<Rate curr="CHF" unit="1">246,21</Rate>
<Rate curr="CNY" unit="1">36,62</Rate>
<Rate curr="CZK" unit="1">11,02</Rate>
<Rate curr="DKK" unit="1">40,59</Rate>
<Rate curr="EUR" unit="1">302,8</Rate>
<Rate curr="GBP" unit="1">366,05</Rate>
<Rate curr="HKD" unit="1">28,78</Rate>
<Rate curr="HRK" unit="1">39,67</Rate>
<Rate curr="IDR" unit="100">1,88</Rate>
<Rate curr="ILS" unit="1">63,32</Rate>
<Rate curr="INR" unit="1">3,58</Rate>
<Rate curr="ISK" unit="1">1,87</Rate>
<Rate curr="JPY" unit="100">217,08</Rate>
<Rate curr="KRW" unit="100">21,04</Rate>
<Rate curr="LTL" unit="1">87,71</Rate>
<Rate curr="LVL" unit="1">430,68</Rate>
<Rate curr="MXN" unit="1">16,89</Rate>
<Rate curr="MYR" unit="1">69,33</Rate>
<Rate curr="NOK" unit="1">36,46</Rate>
<Rate curr="NZD" unit="1">183,65</Rate>
<Rate curr="PHP" unit="1">5,1</Rate>
<Rate curr="PLN" unit="1">72,03</Rate>
<Rate curr="RON" unit="1">67,89</Rate>
<Rate curr="RSD" unit="1">2,65</Rate>
<Rate curr="RUB" unit="1">6,71</Rate>
<Rate curr="SEK" unit="1">34,08</Rate>
<Rate curr="SGD" unit="1">177,85</Rate>
<Rate curr="THB" unit="1">6,93</Rate>
<Rate curr="TRY" unit="1">109,6</Rate>
<Rate curr="UAH" unit="1">27,14</Rate>
<Rate curr="USD" unit="1">223,09</Rate>
<Rate curr="ZAR" unit="1">21,59</Rate>
</Day>
</MNBCurrentExchangeRates>
</GetCurrentExchangeRatesResult>
</GetCurrentExchangeRatesResponse>
</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:ws="http://www.mnb.hu/webservices/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:template match="/">
<xsl:element name="list">
<xsl:value-of select="soap:Envelope/soap:Body/ws:GetCurrentExchangeRatesResponse/ws:GetCurrentExchangeRatesResult" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这个XSLT的工作方式与之相同,但我无法深入了解。每次我尝试访问MNBCurrentExchangeRates元素时,选择结果为空。 (我试过ws:MNBCurrentExchangeRates,* [local-name()='MNBCurrentExchangeRates']和许多其他不太现实的选项)
如果有帮助,此soap请求的规范声明GetCurrentExchangeRatesResult元素的内容是字符串:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCurrentExchangeRatesResponse xmlns="http://www.mnb.hu/webservices/">
<GetCurrentExchangeRatesResult>string</GetCurrentExchangeRatesResult>
</GetCurrentExchangeRatesResponse>
</soap:Body>
</soap:Envelope>
有没有办法获得Day和Rate元素并选择它们?
谢谢, kwitee
编辑: 我尝试了user3016153和MathiasMüller的答案,但没有一个对我有用,所以让我改变一下这个问题:
为什么这个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:ws="http://www.mnb.hu/webservices/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:template match="/">
<xsl:element name="list">
<xsl:value-of select="//ws:GetCurrentExchangeRatesResult" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
工作正常:
<?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:ws="http://www.mnb.hu/webservices/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:template match="/">
<xsl:element name="list">
<xsl:value-of select="//ws:MNBCurrentExchangeRates" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
不是吗?
MathiasMüller的答案创造了这个结果(xml编码以某种方式被破坏,日期访问不起作用):
<?xml version="1.0" encoding="utf-16"?><MNBCurrentExchangeRates><Day date="2013-12-03"><Rate curr="AUD" unit="1">203,5</Rate><Rate curr="BGN" unit="1">154,82</Rate><Rate curr="BRL" unit="1">94,69</Rate><Rate curr="CAD" unit="1">209,79</Rate><Rate curr="CHF" unit="1">246,21</Rate><Rate curr="CNY" unit="1">36,62</Rate><Rate curr="CZK" unit="1">11,02</Rate><Rate curr="DKK" unit="1">40,59</Rate><Rate curr="EUR" unit="1">302,8</Rate><Rate curr="GBP" unit="1">366,05</Rate><Rate curr="HKD" unit="1">28,78</Rate><Rate curr="HRK" unit="1">39,67</Rate><Rate curr="IDR" unit="100">1,88</Rate><Rate curr="ILS" unit="1">63,32</Rate><Rate curr="INR" unit="1">3,58</Rate><Rate curr="ISK" unit="1">1,87</Rate><Rate curr="JPY" unit="100">217,08</Rate><Rate curr="KRW" unit="100">21,04</Rate><Rate curr="LTL" unit="1">87,71</Rate><Rate curr="LVL" unit="1">430,68</Rate><Rate curr="MXN" unit="1">16,89</Rate><Rate curr="MYR" unit="1">69,33</Rate><Rate curr="NOK" unit="1">36,46</Rate><Rate curr="NZD" unit="1">183,65</Rate><Rate curr="PHP" unit="1">5,1</Rate><Rate curr="PLN" unit="1">72,03</Rate><Rate curr="RON" unit="1">67,89</Rate><Rate curr="RSD" unit="1">2,65</Rate><Rate curr="RUB" unit="1">6,71</Rate><Rate curr="SEK" unit="1">34,08</Rate><Rate curr="SGD" unit="1">177,85</Rate><Rate curr="THB" unit="1">6,93</Rate><Rate curr="TRY" unit="1">109,6</Rate><Rate curr="UAH" unit="1">27,14</Rate><Rate curr="USD" unit="1">223,09</Rate><Rate curr="ZAR" unit="1">21,59</Rate></Day></MNBCurrentExchangeRates>
我粘贴.NET代码来完成这个问题:
XslCompiledTransform xslt = new XslCompiledTransform();
using (StringWriter writer = new StringWriter())
{
xslt.Load(xsltPath);
xslt.Transform(xmlDoc.CreateNavigator(), null, writer);
string xml = writer.ToString();
XmlSerializer serializer = new XmlSerializer(typeof(List));
using (TextReader reader = new StringReader(xml))
{
return (List)serializer.Deserialize(reader);
}
}
答案 0 :(得分:1)
以下是访问Day
和Rate
元素的方法。它处理Day
表达式中的所有xsl:for-each
元素,并在单独的模板中匹配Rate
个元素。
另请注意这一行:
<xsl:template match="//ws:MNBCurrentExchangeRates">
选择MNBCurrentExchangeRates
元素,无论它们在文档中的位置。这样,你就可以节省驾驭这棵长树的活动。 (如果你的目标确实是获得Day和Rate元素)。
样式表:
<?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:ws="http://www.mnb.hu/webservices/"
exclude-result-prefixes="ws">
<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="//ws:MNBCurrentExchangeRates">
<xsl:element name="list">
<xsl:for-each select="ws:Day">
<xsl:copy>
<xsl:attribute name="date">
<xsl:value-of select="@date"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="ws:Rate">
<xsl:copy>
<xsl:copy-of select="@*|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这会产生以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<Day xmlns="http://www.mnb.hu/webservices/"
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"
date="2013-12-03">
<Rate curr="AUD" unit="1">203,5</Rate>
<Rate curr="BGN" unit="1">154,82</Rate>
<Rate curr="BRL" unit="1">94,69</Rate>
<Rate curr="CAD" unit="1">209,79</Rate>
<Rate curr="CHF" unit="1">246,21</Rate>
<Rate curr="CNY" unit="1">36,62</Rate>
<Rate curr="CZK" unit="1">11,02</Rate>
<Rate curr="DKK" unit="1">40,59</Rate>
<Rate curr="EUR" unit="1">302,8</Rate>
<Rate curr="GBP" unit="1">366,05</Rate>
<Rate curr="HKD" unit="1">28,78</Rate>
<Rate curr="HRK" unit="1">39,67</Rate>
<Rate curr="IDR" unit="100">1,88</Rate>
<!--And so on...-->
</Day>
</list>
答案 1 :(得分:1)
您缺少for-each语句。尝试类似:
<?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:ws="http://www.mnb.hu/webservices/"
exclude-result-prefixes="soap ws">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<list>
<xsl:for-each select="soap:Envelope/soap:Body/ws:GetCurrentExchangeRatesResponse/ws:GetCurrentExchangeRatesResult/ws:MNBCurrentExchangeRates/ws:Day/ws:Rate">
<rate>
<date><xsl:value-of select="../@date"/></date>
<curr><xsl:value-of select="@curr"/></curr>
<value><xsl:value-of select="."/></value>
</rate>
</xsl:for-each>
</list>
</xsl:template>
</xsl:stylesheet>
将产生以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<rate>
<date>2013-12-03</date>
<curr>AUD</curr>
<value>203,5</value>
</rate>
<rate>
<date>2013-12-03</date>
<curr>BGN</curr>
<value>154,82</value>
</rate>
<rate>
<date>2013-12-03</date>
<curr>BRL</curr>
<value>94,69</value>
</rate>
...
<rate>
<date>2013-12-03</date>
<curr>USD</curr>
<value>223,09</value>
</rate>
<rate>
<date>2013-12-03</date>
<curr>ZAR</curr>
<value>21,59</value>
</rate>
</list>
答案 2 :(得分:1)
回答您的新问题:两种样式表都以相同的方式工作,并以下列形式产生结果:
<?xml version="1.0" encoding="UTF-8"?>
<list xmlns:ws="http://www.mnb.hu/webservices/" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
203,5
154,82
94,69
209,79
...
223,09
21,59
</list>
它们之间的唯一差异是值周围的空行数(一个或两个)。我不确定这有多大帮助。我不是.NET程序员,所以我恐怕无法帮助你。
答案 3 :(得分:0)
对不起伙计们,我误导你了。问题是SOAP响应被部分转义:
<?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>
<GetCurrentExchangeRatesResponse xmlns="http://www.mnb.hu/webservices/">
<GetCurrentExchangeRatesResult><MNBCurrentExchangeRates><Day date="2013-12-04"><Rate curr="AUD" unit="1">200,46</Rate><Rate curr="BGN" unit="1">154,44</Rate><Rate curr="BRL" unit="1">93,56</Rate><Rate curr="CAD" unit="1">208,25</Rate><Rate curr="CHF" unit="1">245,79</Rate><Rate curr="CNY" unit="1">36,49</Rate><Rate curr="CZK" unit="1">11,02</Rate><Rate curr="DKK" unit="1">40,49</Rate><Rate curr="EUR" unit="1">302,05</Rate><Rate curr="GBP" unit="1">363,71</Rate><Rate curr="HKD" unit="1">28,67</Rate><Rate curr="HRK" unit="1">39,53</Rate><Rate curr="IDR" unit="100">1,85</Rate><Rate curr="ILS" unit="1">63,13</Rate><Rate curr="INR" unit="1">3,57</Rate><Rate curr="ISK" unit="1">1,86</Rate><Rate curr="JPY" unit="100">216,66</Rate><Rate curr="KRW" unit="100">20,94</Rate><Rate curr="LTL" unit="1">87,5</Rate><Rate curr="LVL" unit="1">429,71</Rate><Rate curr="MXN" unit="1">16,89</Rate><Rate curr="MYR" unit="1">68,9</Rate><Rate curr="NOK" unit="1">36,35</Rate><Rate curr="NZD" unit="1">181,92</Rate><Rate curr="PHP" unit="1">5,07</Rate><Rate curr="PLN" unit="1">71,9</Rate><Rate curr="RON" unit="1">67,62</Rate><Rate curr="RSD" unit="1">2,63</Rate><Rate curr="RUB" unit="1">6,68</Rate><Rate curr="SEK" unit="1">34,08</Rate><Rate curr="SGD" unit="1">177,05</Rate><Rate curr="THB" unit="1">6,89</Rate><Rate curr="TRY" unit="1">108,51</Rate><Rate curr="UAH" unit="1">26,96</Rate><Rate curr="USD" unit="1">222,29</Rate><Rate curr="ZAR" unit="1">21,4</Rate></Day></MNBCurrentExchangeRates></GetCurrentExchangeRatesResult>
</GetCurrentExchangeRatesResponse>
</soap:Body>
</soap:Envelope>
我从Visual Studio导出XML并自动替换&lt;和&gt;。但是Web服务实际上将GetCurrentExchangeRatesResult的内容视为字符串。
我花了差不多一整天都这样做。
感谢所有帮助我解决此问题的人。
对我有用的最终XSLT - 需要exsl:node-set函数(如果有人需要它):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://www.mnb.hu/webservices/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="soap:Envelope/soap:Body/ws:GetCurrentExchangeRatesResponse/ws:GetCurrentExchangeRatesResult">
<xsl:element name="kurzovni_listek">
<xsl:variable name="details" select="."/>
<xsl:variable name="parsedXml_">
<xsl:call-template name="parseXml">
<xsl:with-param name="text" select="$details"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="parsedXml" select="exsl:node-set($parsedXml_)"/>
<xsl:attribute name="banka"><xsl:text>MNB</xsl:text></xsl:attribute>
<xsl:attribute name="datum">
<xsl:value-of select="$parsedXml/MNBCurrentExchangeRates/Day/@date"/>
</xsl:attribute>
<xsl:for-each select="$parsedXml/MNBCurrentExchangeRates/Day/Rate">
<xsl:element name="kurz">
<xsl:attribute name="mena">
<xsl:value-of select="@curr"/>
</xsl:attribute>
<xsl:attribute name="nasobek">
<xsl:value-of select="@unit"/>
</xsl:attribute>
<xsl:attribute name="hodnota">
<xsl:value-of select="translate(., ',', '.')"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template name="parseXml">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '>')">
<xsl:variable name="topLevelTag">
<xsl:call-template name="getTopLevelTag">
<xsl:with-param name="text" select="$text"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="openingTag">
<xsl:value-of select="$topLevelTag"/>
</xsl:variable>
<xsl:variable name="tagName">
<xsl:call-template name="getTopLevelTagName">
<xsl:with-param name="text" select="$text"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="closingTag">
<xsl:value-of select="concat('</',$tagName,'>')"/>
</xsl:variable>
<xsl:variable name="firstNode">
<xsl:if test="not(contains($topLevelTag,'/>'))">
<xsl:value-of select="substring-before(substring-after($text,$openingTag),$closingTag)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="afterFirstNode">
<xsl:choose>
<xsl:when test="not(contains($topLevelTag,'/>'))">
<xsl:value-of select="substring-after($text,concat($firstNode,$closingTag))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after($text,$topLevelTag)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$tagName}">
<xsl:call-template name="createAttributes">
<xsl:with-param name="text" select="$topLevelTag"/>
</xsl:call-template>
<xsl:call-template name="parseXml">
<xsl:with-param name="text" select="$firstNode"/>
</xsl:call-template>
</xsl:element>
<xsl:call-template name="parseXml">
<xsl:with-param name="text" select="$afterFirstNode"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="getTopLevelTagName">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '>')">
<xsl:variable name="tagWithAttributesWithoutEnd">
<xsl:value-of select="substring-before($text, '>')"/>
</xsl:variable>
<xsl:variable name="tagWithAttributesWithoutBegining">
<xsl:value-of select="substring-after($tagWithAttributesWithoutEnd, '<')"/>
</xsl:variable>
<xsl:variable name="tagName">
<xsl:choose>
<xsl:when test="contains($tagWithAttributesWithoutBegining,' ')">
<xsl:value-of select="substring-before($tagWithAttributesWithoutBegining, ' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tagWithAttributesWithoutBegining"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$tagName"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="getTopLevelTag">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '>')">
<xsl:variable name="tagWithAttributesWithoutEnd">
<xsl:value-of select="substring-before($text, '>')"/>
</xsl:variable>
<xsl:value-of select="concat($tagWithAttributesWithoutEnd,'>')"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="createAttributes">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '="')">
<xsl:variable name="attributeName">
<xsl:value-of select="substring-before(substring-after($text,' '),'="')"/>
</xsl:variable>
<xsl:variable name="attributeValue">
<xsl:value-of select="substring-before(substring-after($text,concat($attributeName,'="')),'"')"/>
</xsl:variable>
<xsl:attribute name="{$attributeName}"><xsl:value-of select="$attributeValue"/></xsl:attribute>
<xsl:call-template name="createAttributes">
<xsl:with-param name="text" select="substring-after($text,concat($attributeName,'="',$attributeValue,'"'))"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>