我有一個要求,即必須將SOAP消息插入到消息字段長度爲200個字符的DB中。 我必須使用XSLT來實現這一點,並且我正在獲取CDATA部分中的SoapMessage。使用XSL將CDATA中的XML溢出爲多個塊
下面是我在一個變量存儲CDATA標籤
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v4="http://www.example.com/ServiceBody/V4">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<v4:getBookDetails>
<v4:Request>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
</v4:Request>
</v4:getBookDetails>
</soapenv:Body>
</soapenv:Envelope>
下面內的SOAP消息是,我已經寫得到的消息到多個標籤,這樣我可以將其插入到DB的XSLT 。
<xsl:stylesheet extension-element-prefixes="date str" exclude-result-prefixes="dp str date" version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:str="http://exslt.org/strings">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:variable name="cdataVariable">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:copy-of select="."/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:variable>
<MessageInChunks>
<stringLength><xsl:value-of select="string-length($cdataVariable)"/></stringLength>
<xsl:choose>
<xsl:when test="string-length($cdataVariable) < 200">
<Chunks>
<xsl:copy-of select="$cdataVariable"/>
</Chunks>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="splitMessageIntoChunks">
<xsl:with-param name="messageToSplit">
<xsl:copy-of select="$cdataVariable"/>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</MessageInChunks>
</xsl:template>
<xsl:template name="splitMessageIntoChunks">
<xsl:param name="messageToSplit"/>
<xsl:variable name="chunkSize" select="'200'"/>
<xsl:if test="string-length($messageToSplit) >0">
<Chunks>
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:copy-of select="substring($messageToSplit, 12, $chunkSize)"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</Chunks>
<xsl:call-template name="splitMessageIntoChunks">
<xsl:with-param name="messageToSplit" select="substring($messageToSplit, $chunkSize+1)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
但是,我越來越犯規輸出包含的元素的值預期爲字符串函數的輸出不顯示的元素。
我需要這樣的東西。
<MessageInChunks>
<Chunks><![CDATA[<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v4="http://www.example.com/ServiceBody/V4">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<v4:getBookDet]]></Chunks>
<Chunks><![CDATA[ails>
<v4:Request>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Compu]]>
</Chunks>
<Chunks><![CDATA[ter</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
]]>
</Chunks>
<Chunks><![CDATA[ </book>
</catalog>
</v4:Request>
</v4:getBookDetails>
</soapenv:Body>
</soapenv:Envelope>]]>
</Chunks>
</MessageInChunks>
基本上我需要的是一個灑XML消息到200個字符塊,使用XSLT把它變成一個CDATA部分。
請幫我解決這個問題。
既然你到了數據分割成200個字符右塊的邏輯,這基本上是http://stackoverflow.com/questions/6696382/xslt-how的副本-to-轉換的XML節點到字符串。 –
@ AndreasVeithen ..將輸入XML轉換爲字符串在我的場景中無效,因爲在將消息顯示在另一個工具中之前,必須將此消息重新轉換爲XML。由於我使用Data Power,因此我使用dp:serialize,如其中一個答案中的建議,它對我很有用。 –