2016-07-27 4 views
0
I 아래 XSLT를 사용하여 XML 네임 스페이스를 제거

가져 오는 요소 = XSLT

<?xml version="1.0" encoding="utf-8"?> 
<typ:CustomerResponse xmlns:typ="http://xml.mycomp.com/customer/types"> 
<typ:CustomerReturn> 
    <typ:Address> 
     <typ:state>PA</typ:state> 
     <typ:city>Harrisburg</typ:city> 
    </typ:Address> 
    <typ:User> 
     <typ:firstName>test</typ:firstName> 
     <typ:lastName>test</typ:lastName> 
    </typ:User> 
</typ:CustomerReturn> 
</typ:CustomerResponse> 

원래 요청.

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="*"> 
     <xsl:element name="{local-name(.)}"> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="@*"> 
     <xsl:attribute name="{local-name(.)}"> 
     <xsl:value-of select="." /> 
    </xsl:attribute> 
    </xsl:template> 
    <xsl:template match="@xsi:nil[.='true']"/> 

</xsl:stylesheet> 

네임 스페이스를 가지고 내 orginal 한 XML에 위의 변환을 실행 한 후 나는 아래 얻을 -

<?xml version="1.0" encoding="utf-8"?> 
<CustomerResponse> 
<CustomerReturn> 
    <Address> 
     <state>PA</state> 
     <city>Harrisburg</city> 
    </Address> 
    <User> 
     <firstName>test</firstName> 
     <lastName>test</lastName> 
    </User> 
</CustomerReturn> 
</CustomerResponse> 

내가 다음과 유사한 출력을 얻기 위해 기존의 XSLT 파일을 강화하고자합니다.

<CustomerResponse> 
     <Address> 
      <state>PA</state> 
      <city>Harrisburg</city> 
     </Address> 
     <User> 
      <firstName>test</firstName> 
      <lastName>test</lastName> 
     </User> 
</CustomerResponse> 
+0

귀하의 질문은 명확하지 않다. 원본 입력 XML과 예상되는 변환 결과를 보여주십시오. –

+0

안녕 마이클, 예상 출력을 추가했습니다. –

+1

원본 입력 사항도 함께 표시하십시오. - 원하는 출력이 올바른 형식의 XML이 아니라는 사실을 알고 있습니까? –

답변

2

가 이런 식으로 시도 출력 예상?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:typ="http://xml.mycomp.com/customer/types" 
exclude-result-prefixes="typ"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="typ:CustomerReturn"> 
    <xsl:apply-templates/> 
</xsl:template> 

</xsl:stylesheet> 
+0

감사합니다. 고맙습니다. 무엇이 행해졌는지 설명 할 수 있습니까? –

+0

첫 번째 템플릿은 원본 XML의 모든 요소에 대해 네임 스페이스가없는 해당 요소를 만듭니다. 재귀 적으로 수행하여 루트에서 리프까지 전체 트리를 가로 지릅니다. 두 번째 템플릿은'typ : CustomerReturn'을 오버라이드합니다. (' '를 호출하여) 재귀를 계속하지만 현재 노드에 해당하는 요소를 만드는 것을 건너 뜁니다. 따라서'typ : CustomerReturn'의 자식들은 계층 구조에서 위로 올라가서'CustomerResponse'의 자식이됩니다. - 또한보십시오 : https://www.w3.org/TR/xslt/#section-Processing-Model –

+0

고마워요, 마이클! –