2011-09-08 2 views
2

저는 XSL에 익숙하며 XSL에 대한 약간의 도움이 필요합니다. 네임 스페이스 SPR에 정의 된 문자열을 다른 문자열로 대체해야하지만, somereason에 대해서는 XSL does not work, 일부 사람이 나를 잘못 빠져 나올 수있게 도와 줬어.이름 공간에 정의 된 문자열을 바꾸기

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> 
      <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 
+0

좋은 질문, +1. 가장 근본적이고 강력한 XSLT 디자인 패턴을 기반으로하는 완전하고, 쉽고 간단한 솔루션 인 정체성 규칙 재정의 내 대답을 확인하십시오. –

+0

또한 광범위한 설명을 추가했습니다. –

답변

0

참고 : 당신은 당신의 XSL의 모든 네임 스페이스를 선언하지 않았고 필요한 접두사를 사용하지 않은

  • . 이 question을 참조하십시오.
  • HEADER 요소는 입력 XML의 가장 바깥 쪽 요소가 아닙니다. 이 question을 참조하십시오.
  • SubmitPreClaimRequest 요소는 입력 XML
  • 아무도는 그런 식의 요소의 이름을 대체 할 "문자열 대체"사용하지 않는다 (또는 적어도 샘플 당신이 보여주고있다)에 존재하지 않습니다. 이 question을 참조하십시오.
0

이 짧고 쉬운 완전한 변환 (신원 규칙 재정의) : 제공된 XML 문서에 적용

<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> 

:

<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> 

은 원 생산 , 올바른 결과 :

<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> 

설명 :

  1. identity rule/템플릿을 복사 모든 노드 "로는-입니다."

  2. 요소와 일치하는 - 동일성 규칙을 무시하는 템플릿은 하나뿐입니다. 이 오버라이드 템플릿의

  3. tns:ONE라는 새로운 리터럴 결과 소자는 출력이다 - 몸의 내부 노드가 처리되는 모든 자식 ("그대로"에 복사 초래 정체성 템플릿으로) .

기억하여 가장 근본적이고 강력한 XSLT 디자인 패턴을 식별 규칙입니다 무시 - "있는 그대로"만 다르게 처리하기 위해 대부분의 노드를 복사하는 데 필요한 모든 작업에 가장 일부 특정 노드 - 이름 변경/삭제/삽입, ... 등

+0

당신의 설명과 XSL에 감사드립니다. – san

+0

@ 산 : 내 대답이 유용했기 때문에 기쁩니다. 여기에서 SO를 표현하는 공식적인 형식은 가장 좋은 대답을 받아들이는 것입니다. 이것은 답변 옆에있는 턱 마크를 클릭하면됩니다. –