XSLT将名称空间声明从子元素移动到父元素

时间:2013-02-19 20:00:04

标签: xslt

我有这个来源:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <sc:GetFdrRewardsResponse xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema"
    xmlns:sc_1="http://somecompany.com/soa/common1.1/schema"
    xmlns:sc_2="http://somecompany.com/soa/common/schema"
    xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema">
      <sc_1:ResponseCode>0000</sc_1:ResponseCode>
      <sc_1:ResponseMessage>Successful Execution</sc_1:ResponseMessage>
      <sc_1:CorrelationId>1234</sc_1:CorrelationId>
      <sc:FdrRewards>
        <sc_2:BankNumber>0175</sc_2:BankNumber>
        <sc:IsRewardsMember>true</sc:IsRewardsMember>
        <sc:IsEligibleToRedeem>true</sc:IsEligibleToRedeem>
        <sc_3:AmazingRewardsBalance>10442.00</sc_3:AmazingRewardsBalance>
        <sc_3:CashBackSavingsBalance>0.00</sc_3:CashBackSavingsBalance>
      </sc:FdrRewards>
    </sc:GetFdrRewardsResponse>
  </soapenv:Body>
</soapenv:Envelope>

需要这个结果:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <sc_3:GetFdrRewardsResponse 
       xmlns:sc_1="http://somecompany.com/soa/common1.1/schema"
       xmlns:sc_2="http://somecompany.com/soa/common/schema"
       xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema" >
      <sc_1:ResponseCode>0000</sc_1:ResponseCode>
      <sc_1:ResponseMessage >Successful Execution</sc_1:ResponseMessage>
      <sc_1:CorrelationId >1234</sc_1:CorrelationId>
      <sc_3:FdrRewards>
        <sc_2:BankNumber >0175</sc_2:BankNumber>
        <sc_3:AmazingRewardsBalance>10442.00</sc_3:AmazingRewardsBalance>
        <sc_3:CashBackSavingsBalance>0.00</sc_3:CashBackSavingsBalance>
      </sc_3:FdrRewards>
    </sc_3:GetFdrRewardsResponse>
  </soapenv:Body>
</soapenv:Envelope>

我正在使用此转换:

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema"
  xmlns:sc_1="http://somecompany.com/soa/common1.1/schema"
    xmlns:sc_2="http://somecompany.com/soa/common/schema"
    xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema"
  >

  <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>

  <xsl:strip-space elements="*"/>

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

  <xsl:template match="sc:IsRewardsMember"/>
  <xsl:template match="sc:IsEligibleToRedeem"/>

  <xsl:template match="sc:*">
    <xsl:element name="sc_3:{local-name()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

哪个几乎有效。它将结果命名空间声明放在每个子元素上,而不是在父元素中声明它一次。

有什么想法吗?一个完整的XSLT文件太棒了......

1 个答案:

答案 0 :(得分:0)

当我将XSLT更改为以下内容时:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema" xmlns:sc_1="http://somecompany.com/soa/common1.1/schema" xmlns:sc_2="http://somecompany.com/soa/common/schema" xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="sc:IsRewardsMember"/>
    <xsl:template match="sc:IsEligibleToRedeem"/>

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

所需的输出几乎是您想要的,但输出中仍然存在以下命名空间:

xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

但它不应该伤害任何人