不带架构的 sharepoint 数据视图 Web 部件 xslt

发布于 2024-08-23 21:03:10 字数 3691 浏览 4 评论 0 原文

我正在尝试使用数据视图 Web 部件(通过 SPD 2007)来使用基于 SOAP 的 Web 服务的结果,并使用 XSL 转换呈现所述结果的部分内容。我遇到的问题是设计器没有太大帮助,因为 Web 服务的架构实际上并不包含结果的元素,因此无法从数据源拖放到 Web 部件中,我尝试过的手动转换不起作用。

以下是 Web 服务的定义:

<?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>

以及响应的定义:

<?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>

查询定义没有问题 - 您只需提供一个股票代码作为字符串。不过,您会在结果中看到我正在谈论的内容。它将结果定义为字符串。

在 SPD2007 中,数据源几乎只包含soap:Envelope/soap:Body/GetQuoteResponse/GetQuoteResult,但结果字符串中包含的实际结果如下所示:

<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>

我尝试设置 XSL数据视图 Web 部件中的样式表如下所示:

<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>

这几乎符合您的预期:它呈现整个结果字符串。但是,如果我替换

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

为,

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

我什么也得不到。这是怎么回事?如何使用 XSL 在没有模式的情况下从字符串结果中挑选出 XML?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-08-30 21:03:10

看起来结果是一个字符串,而不是需要进行此类处理的 XML。如果不查看结果 xml,我无法确定。

尝试添加

<xsl:copy-of select="." //> 并发布结果。

您可以删除肥皂上的火柴:信封等并用火柴“*”替换吗?

然后在里面添加一个

<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 我无法确定)
这一切都是为了调试。

Looks like the results are a string, not XML which would require processing as such. I cannot be sure without looking at the result xml.

Try adding <xmp><xsl:copy-of select="." /></xmp> and posting the results.

Can you remove the match on soap:Envelope etc and replace with match "*".

Then inside that add a

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

If that does not provide values, the matching is not correct. Also try

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

At the end of the day you are wanting to get something like (without seeing the raw xml I cannot be sure)
This is all for debugging.

梦里°也失望 2024-08-30 21:03:10

在查看您正在使用的服务时,它确实返回字符串中的值与 <使其看起来像 XML。我无法想象他们为什么要这样做,但您需要将字符串解析为 XML 才能处理它。没有本机 XSLT 函数可以执行此操作,因此您必须使用扩展函数。我不知道微软有什么,所以你必须自己写一个。

幸运的是,这篇文章中有一个很好的例子 这个确切的问题。此人最终使用用 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="<Empty />";
        }
        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)" />

我不能保证自定义函数将完全按照您需要的方式工作,但它应该有望让您接近。

In looking at the service you are using it does return the values in a string with < making it look like XML. I can't imagine why they would do this, but you'll need to parse the string as XML in order to process it. There is no native XSLT function to do this, so you'll have to use an extension function. I don't know of one from Microsoft so you'll have to write your own.

Fortunately there is a good example in this post of this exact question. This person ended up using a custom extension function written in c# to convert the string to XML and then pass it back to the XSLT for regular processing. The custom function they use is:

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

    public XPathNodeIterator parse(string data) {
        if(data==null || data.Length==0) {
            data="<Empty />";
        }
        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>

And then you call the function on your string:

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

I can't guarantee that the custom function will work exactly the way you need it, but it should hopefully get you close.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文