【问题标题】:Transform XML using XSLT for SOAP output使用 XSLT 为 SOAP 输出转换 XML
【发布时间】:2020-12-04 17:15:29
【问题描述】:

我有这个 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <txnId>0</txnId>
    <headerNotes>
        <reservationId>505524998</reservationId>
        <computerId>93725091</computerIdId>
        <customerNumber>2166552</customerNumber>
        <modifiedDate>2015-07-27T13:36:19.000+0000</modifiedDate>
        <modifiedBy>sa_oapig</modifiedBy>
        <requestedBy>sa_oapig</requestedBy>
        <serialNumber>1</serialNumber>
        <note/>
        <headerSummary>
            <reserve>Reservation info</reserve>
            <actionTaken>Changed</actionTaken>
        </headerSummary>
        <headerDetails>
            <reserve>Reservation info</reserve>
            <actionBy/>
            <field/>
            <original/>
            <new/>
        </headerDetails>
    </headerNotes>
  </Root>

我想使用 XSLT 转换将上述 XML 转换为新的 SOAP 格式, 这里主要是我不需要根元素和headerNotes,但需要它的子元素,另一个条件是headerNotes 三个子元素应该再次嵌入到它们自己的标签中,如下面的要求所示。 我需要确切的 XSLT 代码来转换到肥皂信封内的 XML 之下

 <reservationId>505524998</reservationId>
 <computerId>93725091</computerIdId>
 <customerNumber>2166552</customerNumber>
 <modifiedDate>2015-07-27T13:36:19.000+0000</modifiedDate>
 <modifiedBy>sa_oapig</modifiedBy>
 <requestedBy>sa_oapig</requestedBy>
 <serialNumber>1</serialNumber>
 <note/>
 <headerSummary>
     <reserve>Reservation info</reserve>
     <actionTaken>Changed</actionTaken>
 </headerSummary>
 <headerDetails>
     <reserve>Reservation info</reserve>
     <actionBy/>
     <field/>
     <original/>
     <new/>
  </headerDetails>

我尝试过使用下面的 xslt 代码,但没有成功

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

    <xsl:template match="Root">
    <xsl:for-each select="/Root/headerNotes">
    <reservationId>
        <reservationId><xsl:value-of select="reservationId" /> </reservationId>
    </reservationId>
    <computerId>
        <computerId> <xsl:value-of select="computerId" /> </computerId>
    </computerId>
    <customerNumber>
        <customerId><xsl:value-of select="customerNumber"/></customerId>
    </customerNumber>
    </xsl:for-each>
    </xsl:template>
    <xsl:template match="headerNotes">
    <xsl:copy-of select="./*" />
    </xsl:template>
    <xsl:template match="headerSummary">
    <xsl:copy-of select="." />
    </xsl:template>
    <xsl:template match="headerDetails">
    <xsl:copy-of select="." />
    </xsl:template>
</xsl:stylesheet>

我正在寻找可用于在我的代码中使用它的 xslt 代码。一旦我有了解决方案,它将有助于我的转型。

谢谢

【问题讨论】:

  • 您确定这正是您想要的结果吗?没有单个根元素的 XML 片段?
  • 我想将此输出转换为 SOAP 正文。请帮帮我
  • 我在你的输出中没有看到 SOAP,所以不清楚你想要什么。
  • 我将硬核soap标签并希望将xslt转换放入soap body

标签: xml xslt


【解决方案1】:

我认为你过于复杂了......

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="/Root">
    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> 
      <env:Body>
        <xsl:apply-templates select="headerNotes/*"/>
      </env:Body>
    </env:Envelope>
  </xsl:template>
  
</xsl:stylesheet>

【讨论】:

  • 能否请您重新审视我的要求。谢谢
  • 请对上述查询有任何帮助
  • @RamReddy - 您还没有添加任何您希望 SOAP 信封看起来像的示例,但这应该只是在 XSLT 中对信封进行硬编码以及“正文”由应用模板填充。请查看我的编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
相关资源
最近更新 更多