2

I'm trying to use a data view web part (via SPD 2007) to consume the results of a SOAP-based web service and render portions of said results using XSL transforms. The problem I'm having is that the designer isn't much help because the schema for the web service doesn't actually include the elements of the results, so there's no way to drag and drop from the data source into the web part, and the manual transforms I've attempted aren't working.

Here is the definition of the web service:

<?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>
    <GetQuote xmlns="http://www.webserviceX.NET/">
      <symbol>string</symbol>
    </GetQuote>
  </soap:Body>
</soap:Envelope>

And the definition of the response:

<?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>
    <GetQuoteResponse xmlns="http://www.webserviceX.NET/">
      <GetQuoteResult>string</GetQuoteResult>
    </GetQuoteResponse>
  </soap:Body>
</soap:Envelope>

The query definition is no problem - you just supply a stock ticker symbol as a string. You'll see what I'm talking about in the results, though. It defines the result as just a string.

In SPD2007, the data source pretty much only includes soap:Envelope/soap:Body/GetQuoteResponse/GetQuoteResult, but the actual results contained in the result string look like this:

<StockQuotes>
  <Stock>
    <Symbol>MSFT</Symbol>
    <Last>28.465</Last>
    <Date>3/3/2010</Date>
    <Time>1:24pm</Time>
    <Change>+0.005</Change>
    <Open>28.52</Open>
    <High>28.61</High>
    <Low>28.35</Low>
    <Volume>28380812</Volume>
    <MktCap>249.7B</MktCap>
    <PreviousClose>28.46</PreviousClose>
    <PercentageChange>+0.02%</PercentageChange>
    <AnnRange>14.87 - 31.50</AnnRange>
    <Earns>1.815</Earns>
    <P-E>15.68</P-E>
    <Name>Microsoft Corpora</Name>
  </Stock>
</StockQuotes>

I've tried setting up an XSL stylesheet like this in the data view web part:

<xsl:stylesheet xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                        xmlns:ddw1="http://www.webserviceX.NET/"
                        version="1.0"
                        exclude-result-prefixes="xsl msxsl ddwrt"
                        xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                        xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
                        xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
                        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                        xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                        xmlns:ddwrt2="urn:frontpage:internal">
            <xsl:output method="html" indent="yes"/>
            <xsl:param name="dvt_apos">'</xsl:param>
            <xsl:template match="/soap:Envelope/soap:Body/ddw1:GetQuoteResponse">
                <xsl:value-of select="*" />             
            </xsl:template>
        </xsl:stylesheet>

This does pretty much what you would expect: it renders the entire result string. However, if I replace

<xsl:template match="/soap:Envelope/soap:Body/ddw1:GetQuoteResponse">
  <xsl:value-of select="*" />               
</xsl:template>

with

<xsl:template match="/soap:Envelope/soap:Body/ddw1:GetQuoteResponse">
  <xsl:value-of select="//Symbol" />                
</xsl:template>

I get nothing. What's going on? how do I use XSL to pick out the XML in the string result without a schema?

4

2 に答える 2

2

結果は、XML などの処理が必要な XML ではなく、文字列のようです。結果のxmlを見ないとわかりません。

結果を追加<xmp><xsl:copy-of select="." /></xmp>して投稿してみてください。

soap:Envelope などのマッチを削除して、マッチ「*」に置き換えていただけますか。

次に、その中に追加します

<p>Symbol:<xsl:value-of select="/StockQuotes/Stock/Symbol" /></p> 

それが値を提供しない場合、マッチングは正しくありません。また、試してみてください

<p>Symbol:<xsl:value-of select="/soap:Envelope/soap:Body/ddw1:GetQuoteResponse/StockQuotes/Stock/Symbol" /></p>

一日の終わりに、次のようなものを取得したいと考えています(生のxmlを見ることなく、私には確信が持てません)これはすべてデバッグ用です。

于 2010-03-03T20:20:34.940 に答える
1

使用しているサービスを見ると、< を含む文字列で値が返され、XML のように見えます。なぜこれを行うのか想像できませんが、文字列を処理するには、文字列を XML として解析する必要があります。これを行うためのネイティブ XSLT 関数はないため、拡張関数を使用する必要があります。Microsoft からのものは知らないので、自分で作成する必要があります。

幸いなことに、この正確な質問のこの投稿には良い例があります。この人物は、c# で記述されたカスタム拡張関数を使用して文字列を XML に変換し、それを XSLT に戻して通常の処理を行うことになりました。彼らが使用するカスタム関数は次のとおりです。

<msxml:script language="CSharp" implements-prefix="cd">
<msxml:using namespace="System.IO" />

    public XPathNodeIterator parse(string data) {
        if(data==null || data.Length==0) {
            data="&lt;Empty /&gt;";
        }
        StringReader stringReader = new StringReader(data);
        XPathDocument xPathDocument = new XPathDocument(stringReader);
        XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
        XPathExpression xPathExpression = xPathNavigator.Compile("/");
        XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);
        return xPathNodeIterator;
    }
</msxml:script>

次に、文字列に対して関数を呼び出します。

<xsl:variable name="theXML" select="string(/string)" />
<xsl:variable name="list" select="cd:parse($theXML)" />

カスタム関数が必要なとおりに機能することを保証することはできませんが、うまくいけば、それに近づくはずです.

于 2010-03-05T17:03:19.863 に答える