开发者

Replace string defined wint in a name space

I am very new to XSL and i need some help with my XSL ,I have request where i need to replace the string defined with in the namespace SPR to a different string but for somereason my XSL doesnt work,could some one help me out where it went wrong.

XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://xyz.com/xsd">
   <soapenv:Header>
      <xsd:myHeader>
         <!--Optional:-->
         <APP_ID>APP_ID</APP_ID>
         </xsd:myHeader>
   </soapenv:Header>
   <soapenv:Body>
      <tns:SPR xmlns:tns="http://xyz.com/xsd">
         <Info>
     开发者_如何学Python       <System>
               <id>id</id>
               <sourceSystemName>sourceSystemName</sourceSystemName>
            </System>
            <Type>transmissionType</Type>
            <Id>encounterId</Id>
            </Info>
              </tns:SPR>
   </soapenv:Body>
</soapenv:Envelope>

XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <xsl:for-each select="/Header/Body/SubmitPreClaimRequest/*[namespace-uri(.)='']">     
      <xsl:call-template name="string-replace-all">
         <xsl:with-param name="text" select="/Header/Body/SPR" />
         <xsl:with-param name="replace" select="SPR" />
         <xsl:with-param name="by" select="ONE" />
      </xsl:call-template>
      </xsl:for-each>
      <xsl:apply-templates />


   </xsl:template>
<xsl:template name="string-replace-all">
   <xsl:param name="text" />

   <xsl:param name="replace" />

   <xsl:param name="by" />

   <xsl:choose>
      <xsl:when test="contains($text,$replace)">
         <xsl:value-of select="substring-before($text,$replace)" />

         <xsl:value-of select="$by" />

         <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="substring-after($text,$replace)" />

            <xsl:with-param name="replace" select="$replace" />

            <xsl:with-param name="by" select="$by" />
         </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
         <xsl:value-of select="$text" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>
</xsl:stylesheet>

Result with my XSL:

<?xml version="1.0" encoding="UTF-16"?>APP_IDidsourceSystemNametransmissionTypeencounterId

EXPECTED Result:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://xyz.com/xsd">
   <soapenv:Header>
      <xsd:myHeader>
         <!--Optional:-->
         <APP_ID>APP_ID</APP_ID>
         </xsd:myHeader>
   </soapenv:Header>
   <soapenv:Body>
      **<tns:ONE xmlns:tns="http://xyz.com/xsd">**
         <Info>
            <System>
               <id>id</id>
               <sourceSystemName>sourceSystemName</sourceSystemName>
            </System>
            <Type>transmissionType</Type>
            <Id>encounterId</Id>
            </Info>
              **</tns:ONE>**
   </soapenv:Body>
</soapenv:Envelop


Note:

  • You did not declare any namespace in your xsl and did not use the required prefixes. See this question.
  • The HEADER element is not the outermost element of your input XML. See this question.
  • The SubmitPreClaimRequest element is not present in your input XML (or at least in the sample you are showing)
  • Nobody uses a "string-replace" to replace the name of an element in that way. See this question.


This short and easy complete transformation (override of the identity rule):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soapenv=
 "http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:tns="http://xyz.com/xsd">
 <xsl:output omit-xml-declaration="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="tns:SPR">
  <tns:ONE>
   <xsl:apply-templates/>
  </tns:ONE>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<soapenv:Envelope xmlns:soapenv=
 "http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsd="http://xyz.com/xsd">
    <soapenv:Header>
        <xsd:myHeader>
            <!--Optional:-->
            <APP_ID>APP_ID</APP_ID>
        </xsd:myHeader>
    </soapenv:Header>
    <soapenv:Body>
        <tns:SPR xmlns:tns="http://xyz.com/xsd">
            <Info>
                <System>
                    <id>id</id>
                    <sourceSystemName>sourceSystemName</sourceSystemName>
                </System>
                <Type>transmissionType</Type>
                <Id>encounterId</Id>
            </Info>
        </tns:SPR>
    </soapenv:Body>
</soapenv:Envelope>

produces the wanted, correct result:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://xyz.com/xsd">
   <soapenv:Header>
      <xsd:myHeader><!--Optional:-->
         <APP_ID>APP_ID</APP_ID>
      </xsd:myHeader>
   </soapenv:Header>
   <soapenv:Body>
      <tns:ONE xmlns:tns="http://xyz.com/xsd">
         <Info>
            <System>
               <id>id</id>
               <sourceSystemName>sourceSystemName</sourceSystemName>
            </System>
            <Type>transmissionType</Type>
            <Id>encounterId</Id>
         </Info>
      </tns:ONE>
   </soapenv:Body>
</soapenv:Envelope>

Explanation:

  1. The identity rule/template copies every node "as-is".

  2. There is only one template that overrides the identity rule -- matching any tns:SPR element.

  3. In this overriding template a new literal-result element, named tns:ONE is output -- inside its body all its children nodes are processed (by the identity template, which results in copying them "as-is").

Remember: Using and overriding the identity rule is the most fundamental and powerful XSLT design pattern -- best for any task that requires to copy most of the nodes "as-is" and only to process differently some specific nodes -- rename/delete/insert, ..., etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜