如何测试节点 XML 编码的 XSLT 1.0 中是否"<?xml version="1.0" encoding="utf-8"?>"



我有一个要求,试图从节点<payload>中提取转义的XML字符。我面临的挑战是如何在转义的XML消息中进行XML声明。

这是输入 (XML声明)

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:processInboundMessage xmlns:ns2="http://integration.nexuse2e.org/BackendDeliveryInterface/">
            <choreographyId>InventoryActualUsage</choreographyId>
            <businessPartnerId>Development</businessPartnerId>
            <actionId>SendFile</actionId>
            <conversationId>943fe0e5-2f88-48ce-ae3c-f7124ee498fd</conversationId>
            <messageId>e9b3af1d-70fd-43c0-b467-df21fd903672</messageId>
            <payload>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;test&gt;somethinginformation&lt;/test&gt;
&lt;test1&gt;someotherinfo&lt;/test1&gt;
    ;</payload>
        </ns2:processInboundMessage>
    </soap:Body>
</soap:Envelope>

挑战:如果代码有XML声明,它就可以工作,但可能存在没有XML声明的情况

示例:没有XML声明

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:processInboundMessage xmlns:ns2="http://integration.nexuse2e.org/BackendDeliveryInterface/">
            <choreographyId>InventoryActualUsage</choreographyId>
            <businessPartnerId>Development</businessPartnerId>
            <actionId>SendFile</actionId>
            <conversationId>943fe0e5-2f88-48ce-ae3c-f7124ee498fd</conversationId>
            <messageId>e9b3af1d-70fd-43c0-b467-df21fd903672</messageId>
            <payload>
&lt;test&gt;somethinginformation&lt;/test&gt;
&lt;test1&gt;someotherinfo&lt;/test1&gt;
    ;</payload>
        </ns2:processInboundMessage>
    </soap:Body>
</soap:Envelope>

代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:date="http://exslt.org/dates-and-times" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes=" date">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="node()|@*">
        <xsl:apply-templates select="node()|@*"/>
    </xsl:template>
    <xsl:template match="payload">
        <xsl:variable name="xmlencode" select="'&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;'"/>
        <xsl:variable name="message">
            <xsl:choose>
                <xsl:when test="contains($xmlencode,'?&gt;')">
                    <xsl:value-of select="substring-after(text(),'?&gt;')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:value-of select="$message" disable-output-escaping="yes"/>
            </soapenv:Body>
        </soap:Envelope>
    </xsl:template>
</xsl:stylesheet>

我遇到了第二种情况的问题,如果XML声明不可用,我什么都得不到。

得到这样的输出:

<soap:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body/>
</soap:Envelope>

预期输出

<soap:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
<test>somethinginformation</test>
<test1>someotherinfo</test1>
    </soapenv:Body>
</soap:Envelope>

基本上,主要问题是测试变量xmlencode是否包含'>'这总是正确的,因为xmlencode有一个静态值。

如果您想检查payload元素的值是否从xmlencode变量中的XML声明开始,您可以将测试条件从contains($xmlencode,'?&gt;')更改为以(normalize space(.),$xmlencode开头

所以你的代码将是下一个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:date="http://exslt.org/dates-and-times" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes=" date">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
    <xsl:apply-templates select="node()|@*"/>
</xsl:template>
<xsl:template match="payload">
    <xsl:variable name="xmlencode" select="'&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;'"/>
    <xsl:variable name="message">
        <xsl:choose>
            <xsl:when test="starts-with(normalize-space(.), $xmlencode)">
                <xsl:value-of select="substring-after(text(),'?&gt;')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header/>
        <soapenv:Body>
            <xsl:value-of select="$message" disable-output-escaping="yes"/>
        </soapenv:Body>
    </soap:Envelope>
</xsl:template>
</xsl:stylesheet>

相关内容

  • 没有找到相关文章