使用XSLT更改数据格式

时间:2016-05-04 01:55:32

标签: xml web-services xslt wsdl bpel

我使用BPEL流程来编排两个Web服务。基本上,这两个Web服务返回书籍对象。 第一个Web服务返回以下数据格式:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <searchBooksResponse xmlns="urn:soft.xyz.ac.be/">
      <book xmlns="">
        <author>Robert Ludlum</author>
        <year>2004</year>
        <isbn>95248457</isbn>
        <language>fr</language>
        <publisher>Orion</publisher>
        <title>La Mémoire dans la peau (SOFT Library)</title>
      </book>
      <book xmlns="">
        <author>Douglas Adams</author>
        <year>2002</year>
        <isbn>60184547</isbn>
        <language>fr</language>
        <publisher>Del Rey</publisher>
        <title>Le Guide du Voyageur Galactique (SOFT Library)</title>
      </book>

    </searchBooksResponse>
  </soapenv:Body>
</soapenv:Envelope>

第二种返回以下格式:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <searchForBooksResponse xmlns="http://library.be">
      <searchForBooksReturn>
        <author>James, E. L.</author>
        <date>3914-01-10T23:00:00.000Z</date>
        <isbn>0345803485</isbn>
        <language>English</language>
        <publisher>Vintage Books</publisher>
        <title>50 Shades of Grey (National Library)</title>
      </searchForBooksReturn>
      <searchForBooksReturn>
        <author>James Dashner</author>
        <date>3914-04-20T22:00:00.000Z</date>
        <isbn>0385388896</isbn>
        <language>English</language>
        <publisher>The Maze Runner Series</publisher>
        <title>The Maze Runner Series (National Library)</title>
      </searchForBooksReturn>
    </searchForBooksResponse>
  </soapenv:Body>
</soapenv:Envelope>

要合并结果,我将以下bpel命令与xls文件一起使用。这是bpel命令:

 <![CDATA[bpel:doXslTransform("books.xsl", $sl_searchBooksResponse.parameters, "second", $nl_searchForBooksResponse.parameters/*)]]>

这是xsl文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL

/Transform" xmlns="urn:soft.librarysearch" xmlns:tns="urn:soft.librarysearch" xmlns:nl="http://library.be">
        <xsl:param name="second">
        foo
        </xsl:param>
        <xsl:template match="/">
            <root>
                <xsl:apply-templates />
                <xsl:copy-of select="$second"/>
            </root>
        </xsl:template>

        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>
         </xsl:template> -->

    </xsl:stylesheet>

然而,结果显示不一致如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">   <soapenv:Body>
    <LibrarySearchResponse xmlns="urn:soft.librarysearch" xmlns:tns="urn:soft.librarysearch">
                            <tns:query/>
                            <tns:books xmlns:nl="http://library.be">
        <searchBooksResponse xmlns="urn:soft.xxx.ac.be/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <book xmlns="">
            <author>Robert Ludlum</author>
            <year>2004</year>
            <isbn>95248457</isbn>
            <language>fr</language>
            <publisher>Orion</publisher>
            <title>La Mémoire dans la peau (SOFT Library)</title>
          </book>
          <book xmlns="">
            <author>Douglas Adams</author>
            <year>2002</year>
            <isbn>60184547</isbn>
            <language>fr</language>
            <publisher>Del Rey</publisher>
            <title>Le Guide du Voyageur Galactique (SOFT Library)</title>
          </book>
        </searchBooksResponse>


        <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <author>James, E. L.</author>
          <date>3914-01-10T23:00:00.000Z</date>
          <isbn>0345803485</isbn>
          <language>English</language>
          <publisher>Vintage Books</publisher>
          <title>50 Shades of Grey (National Library)</title>
        </searchForBooksReturn>
        <searchForBooksReturn xmlns="http://library.be" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <author>James Dashner</author>
          <date>3914-04-20T22:00:00.000Z</date>
          <isbn>0385388896</isbn>
          <language>English</language>
          <publisher>The Maze Runner Series</publisher>
          <title>The Maze Runner Series (National Library)</title>
        </searchForBooksReturn>
      </tns:books>
                        </LibrarySearchResponse>   </soapenv:Body> </soapenv:Envelope>

我希望所有图书对象都使用相同的标签&#34; 预订&#34;,并且应该通过删除标记&#34; searchBooksResponse <将它们放在同一级别/强>&#34 ;.例如,更改代码&#34; searchForBooksReturn &#34;到&#34; 预订&#34;。我怎样才能做到这一点?在很长的问题上感谢你的病人。

1 个答案:

答案 0 :(得分:1)

怎么样:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:nl="http://library.be"
exclude-result-prefixes="nl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="second"/>

<xsl:template match="/">
    <root>
        <xsl:apply-templates select="//book"/>
        <xsl:apply-templates select="$second//nl:searchForBooksReturn"/>
    </root>
</xsl:template>

<xsl:template match="nl:searchForBooksReturn">
    <book>
        <xsl:apply-templates/>
    </book>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>