我有一个要求,我必须将 SOAP 消息插入到消息字段字符长度为 200 个字符的数据库中。我必须使用 XSLT 来实现这一点,并且我在 CDATA 部分中获得了 SoapMessage。
下面是我存储在 CDATA 标记内的变量中的 SOAP 消息
<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>
下面是我编写的 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 部分。
请帮我解决这个问题。