2011-04-20 6 views
7
여기

내 (이 경우 시나리오 간체)되는 XSLT의 출력 (노 네임 스페이스) 속성은 XML :는 XMLNS 제거하는 방법 = ""

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

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

<xsl:template match="foo:doc"> 
    <xsl:element name="newdoc" namespace="http://www/w3.org/1999/xhtml"> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="foo:div"> 
    <segment title="{foo:title}"> 
    <xsl:apply-templates/> 
    </segment> 
</xsl:template> 

<xsl:template match="foo:title"> 
    <xsl:element name="h2"> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="foo:paragraph"> 
    <xsl:element name="p"> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 
: 여기

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet href="test_1.xsl" type="text/xsl"?> 

<doc xmlns="http://www.foo.org"> 
    <div> 
    <title>Mr. Title</title> 
    <paragraph>This is one paragraph. 
    </paragraph> 
    <paragraph>Another paragraph. 
    </paragraph> 
    </div> 
</doc> 

그리고 내 XSLT 없다 세그먼트 엘리먼트 = ""xmlns 속성을 제외한 크다

<newdoc xmlns="http://www/w3.org/1999/xhtml"> 
    <segment xmlns="" title="Mr. Title"> 
    <h2>Mr. Title</h2> 
    <p>This is one paragraph. 
    </p> 
    <p>Another paragraph. 
    </p> 
    </segment> 
</newdoc> 

는, 그 자체로 네임 스페이스를 정의되지 같다 :

출력이 생성 그리고 모든 아이들. 이걸 추가하지 않으려면 어떻게해야합니까?

(!) 참고 : 또한 대신

<xsl:template match="mydoc:doc"> 
    <html xmlns="http://www/w3.org/1999/xhtml"> 
    <xsl:apply-templates/> 
    </html> 
</xsl:template> 

으로 첫 번째 노드가 바뀌는 시도하지만 동일한 효과를 생성한다.

도움이되는 사람들입니다.

답변

11

것 같습니다 당신이 "HTTP로 출력 문서의 모든 요소를 ​​넣어 원하는 같이 ://www/w3.org/1999/xhtml "네임 스페이스를 참조하십시오. 현재는 "newdoc"요소에 대한 네임 스페이스 만 지정하고 스타일 시트에 네임 스페이스 선언이 없으므로 다른 모든 요소는 기본 네임 스페이스에 있습니다. 스타일 시트 안의 중첩은 변환 후 중첩이 아닌 요소가 속한 네임 스페이스를 결정합니다.

당신은 모든 다른 비정규 요소에 영향을주는 스타일 시트의 기본 네임 스페이스를 선언 할 수

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:foo="http://www.foo.org" 
    xmlns="http://www.w3.org/1999/xhtml"> 

지금 당신은 또한 더 이상 xsl:element 태그를 필요로하지 않고 직접 올바른 네임 스페이스의 요소를 만들 newdoc를 사용할 수 있습니다.

+0

örn : 감사합니다! 매력처럼 작동하고, 나는 지금 그것을 얻는다라고 생각한다. – Zori

+0

이것은 나에게도 도움이되었지만 대답에는 오타가 있습니다. 대답은'http : //www/w3.org ...''http://www.w3.org ... '라고 말해야합니다. 슬래시는 점이어야합니다. – maddoxej

4

foo:div 템플릿에서 빈 네임 스페이스가있는 segment 요소를 만듭니다. 부모 요소는 다른 네임 스페이스를 가지므로 프로세서는이 네임 스페이스 선언을 추가해야합니다. 당신이 원하는 것은 부모보다 같은 네임 스페이스와 segment 경우

, 다음 대신 xsl:element를 사용

<xsl:template match="foo:div"> 
    <xsl:element name="segment"> 
     <xsl:attribute name="title"> 
      <xsl:value-of select="foo:title"/> 
     </xsl:attribute> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template>