【问题标题】:dynamic XSLT Transformation动态 XSLT 转换
【发布时间】:2015-10-21 20:27:08
【问题描述】:

我在下面有一个传入的简单 xml 请求,需要使用正确的名称空间转换为下面的 SOAP 消息。在传入的 XML 请求中,名称空间没有出现,因此在形成 SOAP 消息时,我们需要处理名称空间也。是否有任何 XSLT 代码 sn-p 可以帮助我实现这一目标。 注意 - 我们需要动态地执行此 XSLT 转换,就像传入的请求可以是任何元素,如“GetImageRequest”,因此需要基于此元素构造名称空间。 (大概我们可以将所有的命名空间保存在一个xml文件中,并且需要构造SOAP消息)

传入的 XML 请求:

<request>
<payload>
<GetImageRequest>
   <participantId>1191152220010</participantId>
   <participantCode>131029</participantCode>
   <groupCode>027198</groupCode>
   <userType>EE</userType>
   <clientName>Test</clientName>
   <shoeboxID>123444</shoeboxID>
   <imageID>45235</imageID>
</GetImageRequest>
</payload>
</request>

================== 需要在 SOAP 消息下面构造正确的命名空间。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>

   </soapenv:Header>
   <soapenv:Body>
      <get:GetShoeboxItemRequest xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/GetShoeboxItem">
         <get:participantId>1060687620010</get:participantId>
         <get:participantCode>1060687620010</get:participantCode>
         <get:groupCode>027198</get:groupCode>
         <get:userType>EE</get:userType>
         <get:clientName>Test</get:clientName>
         <get:shoeboxID>123444</get:shoeboxID>
      </get:GetShoeboxItemRequest>
   </soapenv:Body>
</soapenv:Envelope>

在这方面需要快速帮助。 XSLT 代码 sn-p 会很有帮助。

【问题讨论】:

  • "基于这个元素需要构造名字空间" 说的不够清楚。请提供构建命名空间的确切规则。我看不出GetImageRequest 变成"urn:webservice/server/mobile/shoebox/types/v1/GetShoeboxItem" 的逻辑。 --- 附: your other question 有人回答吗?

标签: xslt


【解决方案1】:

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pRequestedItemName" select="'Shoebox'"/>

 <xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
 <xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>

 <xsl:variable name="vLcItemName" select=
                "translate($pRequestedItemName, $vUpper, $vLower)"/>

 <xsl:variable name="vDynNamespace" select=
   "concat('urn:webservice/server/mobile/', $vLcItemName, '/types/v1/Get', 'Item')"/>

  <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:Header>
            </soapenv:Header>
            <soapenv:Body>
              <xsl:element name="get:Get{$pRequestedItemName}ItemRequest" 
                   namespace="{$vDynNamespace}">
                <xsl:apply-templates select="/*/*/GetImageRequest/node()"/>
              </xsl:element>
            </soapenv:Body>
        </soapenv:Envelope>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="get:{name()}" namespace="{$vDynNamespace}">
      <xsl:copy-of select="namespace::*|@*"/>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

应用于提供的源 XML 文档时

<request>
    <payload>
        <GetImageRequest>
            <participantId>1191152220010</participantId>
            <participantCode>131029</participantCode>
            <groupCode>027198</groupCode>
            <userType>EE</userType>
            <clientName>Test</clientName>
            <shoeboxID>123444</shoeboxID>
            <imageID>45235</imageID>
        </GetImageRequest>
    </payload>
</request>

产生想要的正确结果

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <get:GetShoeboxItemRequest
         xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/GetItem">
         <get:participantId>1191152220010</get:participantId>
         <get:participantCode>131029</get:participantCode>
         <get:groupCode>027198</get:groupCode>
         <get:userType>EE</get:userType>
         <get:clientName>Test</get:clientName>
         <get:shoeboxID>123444</get:shoeboxID>
         <get:imageID>45235</get:imageID>
      </get:GetShoeboxItemRequest>
   </soapenv:Body>
</soapenv:Envelope>

解释

  1. 充分利用了 &lt;xsl:element&gt; 指令的功能。
  2. 使用AVT (Attribute Value Templates)
  3. 动态命名空间的唯一可变部分应由转换的调用者提供为 a global parameter

如果您需要构造一个完全动态的命名空间(例如由转换的调用者传入一个全局参数),请参阅this answer


更新

OP 在评论中澄清说,他可以将所有特定于请求的命名空间收集在单独的 XML 文档或全局参数中。

这是这个已澄清问题的解决方案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pRequestData">
   <r name="GetImageRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/GetShoeboxItem"/>
   <r name="SaveShoeBoxitemRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/SaveShoeboxItem"/>
   <r name="SaveClaimWithReceiptRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/SaveClaimAndReceipt"/>
   <r name="GetThumbNailImageRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/GetThumbnail"/>
   <r name="AttachReceiptwithExistingClaimRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/AttachClaimAndReceipt"/>
 </xsl:param>

 <xsl:variable name="vParams" select="document('')/*/xsl:param[@name='pRequestData']"/>

  <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:Header>
            </soapenv:Header>
            <soapenv:Body>
                <xsl:apply-templates select="/*/*/*"/>
            </soapenv:Body>
        </soapenv:Envelope>
  </xsl:template>

  <xsl:template match="*">
    <xsl:param name="pKey" select="local-name()"/>
    <xsl:element name="get:{local-name()}" namespace="{$vParams/*[@name = $pKey]/@ns}">
      <xsl:copy-of select="namespace::*|@*"/>
      <xsl:apply-templates>
        <xsl:with-param name="pKey" select="$pKey"/>
      </xsl:apply-templates>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时(所提供的文档带有第二个不同名称的 payload 子代):

<request>
    <payload>
        <GetImageRequest>
            <participantId>1191152220010</participantId>
            <participantCode>131029</participantCode>
            <groupCode>027198</groupCode>
            <userType>EE</userType>
            <clientName>Test</clientName>
            <shoeboxID>123444</shoeboxID>
            <imageID>45235</imageID>
        </GetImageRequest>
        <SaveShoeBoxitemRequest>
            <participantId>1191152220010</participantId>
            <participantCode>131029</participantCode>
            <groupCode>027198</groupCode>
            <userType>EE</userType>
            <clientName>Test</clientName>
            <shoeboxID>123444</shoeboxID>
            <imageID>45235</imageID>
        </SaveShoeBoxitemRequest>
    </payload>
</request>

产生了想要的、正确的(不像其他答案中的“解决方案”)结果

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Header/>
       <soapenv:Body>
          <get:GetImageRequest 
xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/GetShoeboxItem">
             <get:participantId>1191152220010</get:participantId>
             <get:participantCode>131029</get:participantCode>
             <get:groupCode>027198</get:groupCode>
             <get:userType>EE</get:userType>
             <get:clientName>Test</get:clientName>
             <get:shoeboxID>123444</get:shoeboxID>
             <get:imageID>45235</get:imageID>
          </get:GetImageRequest>
          <get:SaveShoeBoxitemRequest 
xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/SaveShoeboxItem">
             <get:participantId>1191152220010</get:participantId>
             <get:participantCode>131029</get:participantCode>
             <get:groupCode>027198</get:groupCode>
             <get:userType>EE</get:userType>
             <get:clientName>Test</get:clientName>
             <get:shoeboxID>123444</get:shoeboxID>
             <get:imageID>45235</get:imageID>
          </get:SaveShoeBoxitemRequest>
       </soapenv:Body>
    </soapenv:Envelope>

【讨论】:

  • 您好 Dimitre,感谢您的回复。我需要 XSLT 是动态的,就像任何 oot 元素在“有效负载”标签下一样,它应该相应地构造肥皂信封。像这里我们只为“GetImageRequest”构建。我们如何在 XSLT 中做到这一点。需要您的帮助才能做到这一点。
  • @user5458829,如果您指定规则如何从&lt;payload&gt;的相应子元素构造顶级生成元素的名称(例如“&lt;GetShoeboxItemRequest&gt;”),这是可能的,以及如何从源 xml 文档中的相同对应元素构造名称空间字符串值。如果无法实现稳定的约定,这些都可以作为全局参数提供。
  • 您好 Dimitre,感谢您的评论
  • 嗨 Dimitre,感谢您的评论就像我在“有效负载”下有 5 个可能的传入请求 1.GetImageRequest”2.“SaveShoeBoxitemRequest”3.“SaveClaimWithReceiptRequest”4.“GetThumbNailImageRequest 和 5.“AttachReceiptwithExistingClaimRequest”它位于“有效负载”元素下,并且在传入那些传入的 XML 时,它们不会发送这 5 个元素的命名空间。因此,如果我知道这 5 个元素的所有命名空间是什么,我可以将其放入一个 xml 文件中并可以从该 XML 文件中检索。或者正如您提到的,可以在全局文档中定义这些名称空间。 XSLT 代码 sn-p 会有所帮助。
  • 如果我有“GetImageRequest”元素在“payload”下进行更新,那么我们需要在 BODY 下的 SOAP 消息中构造相同的元素。
【解决方案2】:

所以,如果我知道这 5 个元素的所有命名空间,我可以将它们放在一个 xml 文件,并且可以从该 XML 文件中检索。或者你提到的可以 在全局文档中定义这些名称空间。

如果您有一个与哪个“根”元素名称一起使用的命名空间的映射,您也可以将它保存在样式表本身中。

这是一个示例(使用虚构的命名空间,因为您没有指定自己的命名空间):

XSLT 1.0

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

<my:ns-map>
    <ns root="GetImageRequest">urn:webservice/a/b/GetShoeboxItem</ns>
    <ns root="SaveShoeBoxitemRequest">urn:webservice/c/d/SaveShoeBoxitemRequest</ns>
    <ns root="SaveClaimWithReceiptRequest">urn:webservice/e/f/SaveClaimWithReceiptRequest</ns>
    <ns root="GetThumbNailImageRequest">urn:webservice/g/h/GetThumbNailImageRequest</ns>
    <ns root="AttachReceiptwithExistingClaimRequest">urn:webservice/i/k/AttachReceiptwithExistingClaimRequest</ns>
</my:ns-map>

<xsl:variable name="root" select="name(/request/payload/*)"/>
<xsl:variable name="ns" select="document('')/xsl:stylesheet/my:ns-map/ns[@root=$root]"/>

<xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header/>
        <soapenv:Body>
            <xsl:apply-templates select="request/payload/*" />
        </soapenv:Body>
    </soapenv:Envelope>
</xsl:template>     

<xsl:template match="*" >
    <xsl:element name="get:{local-name()}" namespace="{$ns}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2019-08-03
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多