创建XSLT转换以展平multiRef编码的SOAP消息

时间:2011-03-03 19:17:41

标签: xml xslt soap

输入是mutliRef编码的SOAP消息/文档。你怎么用 XSLT用于压缩multiRefs。可以引用Multiref节点 多次,并自己递归引用其他multiRef 节点。

可以安全引用的结构的唯一部分是 multiRef元素和@id和@href属性。其他元素或 命名空间可能会改变。

示例输入消息是:

<?xml version="1.0" encoding="utf-8"?>
<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>
    <ns1:getAccountDTOResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:ns1="http://www.example.com/pw/services/PWServices">
      <getAccountDTOReturn href="#id0" />
    </ns1:getAccountDTOResponse>
    <multiRef id="id0" soapenc:root="0"
    soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xsi:type="ns2:Account"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:ns2="urn:PWServices">
      <ID href="#id1" />
      <accountNumber xsi:type="soapenc:string"></accountNumber>
      <accountType xsi:type="soapenc:string"></accountType>
      <clientData xsi:type="soapenc:Array" xsi:nil="true" />
      <name xsi:type="soapenc:string"></name>
      <parentRef xsi:type="soapenc:string"></parentRef>
    </multiRef>
    <multiRef id="id1" soapenc:root="0"
    soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xsi:type="xsd:long"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    0</multiRef>
  </soapenv:Body>
</soapenv:Envelope>

预期输出为:

<?xml version="1.0"?>
<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>
    <ns1:getAccountDTOResponse xmlns:ns1="http://www.example.com/pw/services/PWServices"
    soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

      <getAccountDTOReturn xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
      xmlns:ns2="urn:PWServices"
      soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
      xsi:type="ns2:Account">
        <ns1:ID soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
        xsi:type="xsd:long">0</ns1:ID>
        <ns1:accountNumber xsi:type="soapenc:string" />
        <ns1:accountType xsi:type="soapenc:string" />
        <ns1:clientData xsi:type="soapenc:Array" xsi:nil="true" />
        <ns1:name xsi:type="soapenc:string" />
        <ns1:parentRef xsi:type="soapenc:string" />
      </getAccountDTOReturn>
    </ns1:getAccountDTOResponse>
  </soapenv:Body>
</soapenv:Envelope>

更新 在上面的例子中,从逻辑上讲,应该发生的是:

在第一遍中,getAccountDTOResponse包含@href =“#id0”,因此该元素将替换为所有带有@ id =“id0”的子multiRef元素,但不包括。

在第二遍中,应该发现@href =“#id1”,并且应该用@ id =“id1”的元素内容替换ID元素。

输出中不应存在multiRef元素。如果涉及整个multiRef混乱,输出中不应存在@id或@href属性。

2 个答案:

答案 0 :(得分:8)

Alex,我不完全匹配您的输出,但这是您通过hrefs解析文档的方法。

此样式表:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" >

    <xsl:key name="multiref-by-id" match="multiRef" use="@id"/>

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

    <xsl:template match="*[starts-with(@href, '#')]">
        <xsl:copy>
            <xsl:apply-templates select="@* |
             key('multiref-by-id', substring-after(@href, '#'))/@* |
            key('multiref-by-id', substring-after(@href, '#'))/node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@href[starts-with(., '#')] | multiRef[@id] | @soapenc:root"/>

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

鉴于此输入:

<?xml version="1.0" encoding="utf-8"?>
<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>
        <ns1:getAccountDTOResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                   xmlns:ns1="http://www.example.com/pw/services/PWServices">
            <getAccountDTOReturn href="#id0"/>
        </ns1:getAccountDTOResponse>
        <multiRef id="id0" soapenc:root="0"
                  soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                  xsi:type="ns2:Account"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                  xmlns:ns2="urn:PWServices">
            <ID href="#id1"/>
            <accountNumber xsi:type="soapenc:string"></accountNumber>
            <accountType xsi:type="soapenc:string"></accountType>
            <clientData xsi:type="soapenc:Array" xsi:nil="true"/>
            <name xsi:type="soapenc:string"></name>
            <parentRef xsi:type="soapenc:string"></parentRef>
        </multiRef>
        <multiRef id="id1" soapenc:root="0"
                  soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                  xsi:type="xsd:long"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
            0
        </multiRef>
    </soapenv:Body>
</soapenv:Envelope>

产生以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<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>
        <ns1:getAccountDTOResponse xmlns:ns1="http://www.example.com/pw/services/PWServices"
                                   soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <getAccountDTOReturn id="id0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                 xsi:type="ns2:Account">
                <ID xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PWServices" id="id1"
                    soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:long">
                    0
                </ID>
                <accountNumber xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PWServices"
                               xsi:type="soapenc:string"/>
                <accountType xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PWServices"
                             xsi:type="soapenc:string"/>
                <clientData xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PWServices"
                            xsi:type="soapenc:Array" xsi:nil="true"/>
                <name xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PWServices"
                      xsi:type="soapenc:string"/>
                <parentRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PWServices"
                           xsi:type="soapenc:string"/>
            </getAccountDTOReturn>
        </ns1:getAccountDTOResponse>


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

我认为这个apporach可以很容易地定制,以满足您的需求。我想概述所提供的样式表在@hrefs上运行,并且不考虑任何元素名称。因此,可以灵活使用它而无需关注引用元素名称。但是,所有引用都应命名为multiRef,但这也可以轻松自定义。

答案 1 :(得分:0)

此样式表:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <xsl:key name="kMultiRefById" match="multiRef" use="@id"/>
    <xsl:output method="xml"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="getAccountDTOReturn">
        <xsl:variable name="vRefer"
                      select="key('kMultiRefById',substring(@href,2))"/>
        <xsl:copy>
            <xsl:copy-of select="$vRefer/namespace::*"/>
            <xsl:apply-templates select="$vRefer/@*|$vRefer/node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="multiRef|multiRef/@id|multiRef/@soapenc:root"/>
</xsl:stylesheet>

输出:

<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>
        <ns1:getAccountDTOResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                   xmlns:ns1="http://www.example.com/pw/services/PWServices">
            <getAccountDTOReturn soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                 xsi:type="ns2:Account"
                                 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                                 xmlns:ns2="urn:PWServices">
                <ID href="#id1"></ID>
                <accountNumber xsi:type="soapenc:string"></accountNumber>
                <accountType xsi:type="soapenc:string"></accountType>
                <clientData xsi:type="soapenc:Array" xsi:nil="true"></clientData>
                <name xsi:type="soapenc:string"></name>
                <parentRef xsi:type="soapenc:string"></parentRef>
            </getAccountDTOReturn>
        </ns1:getAccountDTOResponse>
    </soapenv:Body>
</soapenv:Envelope>

注意:交叉引用的键。身份规则。剥离的空规则。 复制命名空间节点可能不适用于每个XSLT处理器,尽管我只知道Mozilla的TransforMiiX没有实现namespaces::轴。