2012-09-24 4 views
0

은 왜 내가 그것을 예상대로 작동하지 다음을 수행 지정된 네임 스페이스의 속성?출력하는 방법에

<root xmlns:ns0="xmlns" 
     ns0:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:ns1="xsi" 
     ns1:schemaLocation="[some schema location]" /> 

기본적으로, 수행하여이없는 XML 파일의 schemaLocation을 추가하려고 해요 : -

<xsl:template match="/s:*"> 
    <xsl:element name="{local-name()}" namespace="some other namespace"> 
    <xsl:attribute namespace="xmlns" name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute> 
    <xsl:attribute namespace="xsi" name="schemaLocation">[some-loc]</xsl-attribute> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

와의 Xalan-C는 위의 XML을 제공합니다. 내가 할 노력하고있어

는 뭔가 같은 : -

<root xmlns:ns0="http://www.w3.org/2001/XMLSchema-instance" 
     ns0:schemaLocation="[some schema location]" /> 
+0

잘라 내기/붙여 넣기 문제가 될 수 있지만, 어디입니다 수 있음 첫 번째 일치 항목에 사용 된 s- 접두어가 정의 되었습니까? – conciliator

+0

게시 한 XML 샘플은 실제 출력입니다. 기대하는 결과물 (달성하고자하는 목표)을 보여줄 수 있습니까? 또는 어떻게 든 "내가 기대할 수있는 것"이 ​​무엇인지 설명하십시오. – LarsH

+0

@conciliator는 음의 접두사는 바로 모든 일에 좀 무관하다? 어떤 경우에는이 요소에 정의되어 owagh

답변

3

당신은 <xsl:attribute name="xsi:schemaLocation">[some-loc]</xsl:attribute>이 필요합니다.

편집 (전체 예제를 추가) : 당신은 당신이 가능 <xsl:element name="{local-name()}"/> 대신 요소를 복사 할 <xsl:copy>을 사용할 수 있습니다 일을하는지 정확히에 따라

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:template match="root"> 
     <xsl:element name="{local-name()}" namespace="some other namespace"> 
     <!--<xsl:attribute namespace="xmlns" name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>--> 
     <xsl:attribute name="xsi:schemaLocation">[some-loc]</xsl:attribute> 
     <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

; 그것은 다소 간단합니다.

+0

+1, 좋은 답변. – LarsH

3

내가 질문을 해석하고 당신이 실제로 원하는 것을 추론하고있어 바와 같이, 문제는 네임 스페이스 접두사 될 예정 namespace="..."에 값을 넣는 것을이지만, actually supposed to be에게 네임 스페이스 URI입니다. 필요한 경우

<xsl:template match="/s:*"> 
    <xsl:element name="{local-name()}" namespace="some other namespace"> 
    <xsl:attribute namespace="http://www.w3.org/2001/XMLSchema-instance" 
     name="xsi:schemaLocation">[some-loc]</xsl-attribute> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

그런 다음 XSLT 프로세서 출력 올바른 네임 스페이스에 지정된 schemaLocation 속성이, 또한 그 접두사에 대한 네임 스페이스 선언을 방출 할 것이다

따라서 당신은 아마 원하는 코드는 하나의 <xsl:attribute> 요소입니다 (및 해당 접두어는 xsi: 일 수 있지만 반드시 그럴 필요는 없습니다). 여기에 문제의

일부는 사람들이 무엇을 의미하는지에 대해 명확하지 않고 단어 "네임 스페이스"(정신적 또는 다른 사람에게) 주위에 던질 것입니다. 네임 스페이스는 네임 스페이스 접두사가 아니며 실제로 네임 스페이스 URI도 아닙니다. 네임 스페이스는 세계적으로의 URI로 식별되며, 는 로컬 접두사로 식별.

XSLT 사양이 namespace="..." 대신이 특성의 이름이 namespace-uri="..." 인 경우이 트랩에 포함되는 사람이 줄어 듭니다. 당신은 또한 말한다 누군가가 "네임 스페이스는"정말 "네임 스페이스 URI", "네임 스페이스 접두사", "네임 스페이스 선언", 등 등을 의미 여부를 묻는으로 자신을 보호 할 수 있습니다