2012-11-27 2 views
0

초보자 인 XSLT입니다. XML을 XML로 변환하는 데 사용하고 있습니다.XSLT 상위 노드의 이름 공간 지정

소스 XML :

<Response> 
    <Text>Hello</Text> 
</Response> 

XSLT :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:a="http://myexample.org/a" 
xmlns:b="http://myexample.org/b" 
version="1.0"> 

    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="Response" namespace="http://myexample.org/a"> 
     <xsl:element name="Root"> 
      <xsl:element name="a:Parent"> 
       <xsl:element name="b:Child"> 
        <xsl:value-of select="Text"/> 
       </xsl:element> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

출력 :

<Root> 
    <a:Parent xmlns:a="http://myexample.org/a"> 
    <b:Child xmlns:b="http://myexample.org/b">Hello</b:Child> 
    </a:Parent> 
</Root> 

내가 좋아하는 것은 변환하기 XML을 XSLT를 사용하여 XML 아래로 변환합니다. Outpout 예상

:

<Root xmlns:a="http://myexample.org/a"> 
    <a:Parent xmlns:b="http://myexample.org/b"> 
     <b:Child/> 
    </a:Parent> 
<Root> 

나는 성공적으로 데이터를 변환하는 XSLT를 만든 그러나 여기 내가 네임 스페이스와 혼동입니다. 위와 같이 생성 할 수 없습니다.

도와주세요. 감사.

+0

처음 [XML XSLT] (http://blog.marcocantu.com/blog/whyxslt.html) – vels4j

+0

@ vels4j : XML을 XML로 변환하는 용도로 사용되는 것으로 알고 있습니다. 나는 또한 같은 일을하지만 여기서 출력 XML은 구조를 언급해야한다. 나는 그 질문을 갱신했다. 그것이 의미가 있는지 알려주십시오. –

답변

1

이 (가 <xsl:namespace>을 가지고 2.0 훨씬 쉽게) XSLT 1.0을 사용하여 특정 장소에서 네임 스페이스 선언을 만드는 데 조금 어색하지만은 스타일 문서 자체에서 네임 스페이스 노드를 복사하는 트릭을 수행 할 수 있습니다 : document('')

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:a="http://myexample.org/a" 
xmlns:b="http://myexample.org/b" 
version="1.0"> 

    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="Response"> 
     <xsl:element name="Root"> 
      <xsl:copy-of select="document('')/*/namespace::a" /> 
      <xsl:element name="a:Parent"> 
       <xsl:copy-of select="document('')/*/namespace::b" /> 
       <xsl:element name="b:Child"> 
        <xsl:value-of select="Text"/> 
       </xsl:element> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

그래서 document('')/*<xsl:stylesheet> 요소, 스타일 시트 문서를 구문 분석하고 당신에게 루트 노드를 제공합니다. 그런 다음 해당 요소에서 네임 스페이스 노드을 지정된 접두사로 바인딩하여 출력 문서에 복사합니다.

또는, <xsl:stylesheet>에서 네임 스페이스 선언을 꺼내 리터럴 결과 요소를 사용

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

    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="Response"> 
     <Root xmlns:a="http://myexample.org/a"> 
      <a:Parent xmlns:b="http://myexample.org/b"> 
       <b:Child> 
        <xsl:value-of select="Text"/> 
       </b:Child> 
      </a:Parent> 
     </Root> 
    </xsl:template> 
</xsl:stylesheet> 

을 다른 곳 스타일 시트의 ab 접두사를해야하는 경우이 작동하지 않습니다하지만.