Adding namespace to child elements using xslt

2019-08-30 10:06发布

below is the input xml:

<ns:TXLife xmlns:ns="http://ACORD.org/Standards/Life/2">

  <TXLifeResponse>
     <TransRefGUID/>
     <TransExeDate/>
     <TransExeTime/>

     <TransType tc="228"/>

</ns:TXLife>

and below is my XSLT :

xmlns:ns="http://ACORD.org/Standards/Life/2" version="1.0">

<xsl:output omit-xml-declaration="yes" indent="yes"/>    

<xsl:template match="/">

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:acor="http://www.foresters.com/esb/ws/wsdl/ACORD-v1.0" xmlns:ns="http://ACORD.org/Standards/Life/2">

       <soapenv:Header/>

        <soapenv:Body>

    <xsl:copy>
        <xsl:apply-templates />
    </xsl:copy>

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

</xsl:template>

<xsl:template match="node() [local-name(.) = 'TXLife']">

    <xsl:element name="ns:{local-name()}">

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

       </xsl:element>

  </xsl:template>

By using this tranformation i am not able to add namespace prefix to all the child element of TXLife.

how to add namespace prefix (ns) to all child elements? so that it should look as below

  <ns:TXLifeResponse>
     <ns:TransRefGUID/>
     <ns:TransExeDate/>
     <ns:TransExeTime/>

     <ns:TransType tc="228"/>

</ns:TXLife>

标签: xslt
2条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-30 10:57

If you want only TXLife and descendant to be under http://ACORD.org/Standards/Life/2 namespace, use this stylesheet:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns="http://ACORD.org/Standards/Life/2"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:template match="/">
        <soapenv:Envelope>
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:apply-templates/>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
    <xsl:template match="*[ancestor-or-self::ns:TXLife]">
        <xsl:element name="ns:{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output:

<soapenv:Envelope
 xmlns:ns="http://ACORD.org/Standards/Life/2"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header />
    <soapenv:Body>
        <ns:TXLife>
            <ns:TXLifeResponse>
                <ns:TransRefGUID></ns:TransRefGUID>
                <ns:TransExeDate></ns:TransExeDate>
                <ns:TransExeTime></ns:TransExeTime>
                <ns:TransType tc="228"></ns:TransType>
            </ns:TXLifeResponse>
        </ns:TXLife>
    </soapenv:Body>
</soapenv:Envelope>
查看更多
三岁会撩人
3楼-- · 2019-08-30 10:59

Your XML isn't valid, but I assume you just've missed the closing TXLifeResponse element.

The following transformation will do what you want:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns:TXLife xmlns:ns="http://ACORD.org/Standards/Life/2">

  <TXLifeResponse>
    <TransRefGUID/>
    <TransExeDate/>
    <TransExeTime/>

    <TransType tc="228"/>
  </TXLifeResponse>

</ns:TXLife>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:ns="http://ACORD.org/Standards/Life/2">

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <soapenv:Envelope 
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
      xmlns:acor="http://www.foresters.com/esb/ws/wsdl/ACORD-v1.0" 
      xmlns:ns="http://ACORD.org/Standards/Life/2">
      <soapenv:Header/>
      <soapenv:Body>
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </soapenv:Body>
    </soapenv:Envelope>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="ns:{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

Output:

<soapenv:Envelope 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:acor="http://www.foresters.com/esb/ws/wsdl/ACORD-v1.0" 
  xmlns:ns="http://ACORD.org/Standards/Life/2">
  <soapenv:Header/>
  <soapenv:Body>
    <ns:TXLife>
      <ns:TXLifeResponse>
        <ns:TransRefGUID/>
        <ns:TransExeDate/>
        <ns:TransExeTime/>
        <ns:TransType tc="228"/>
      </ns:TXLifeResponse>
    </ns:TXLife>
  </soapenv:Body>
</soapenv:Envelope>

The template xsl:template match="node()[local-name(.) = 'TXLife']" is somewhat strange to me. What are you trying to accomplish? Maybe we can help explain why this isn't the appropriate way to do it.

查看更多
登录 后发表回答