2010-07-15 2 views
2

누군가이 일이 왜 일어나는지 말해 줄 수 있습니까?이 XML이이 XSLT에서 출력되는 이유는 무엇입니까? XslCompiledTransform을 사용하고 있습니다.

내 XML은 다음과 같습니다

<?xml version="1.0" encoding="utf-8" ?> 
<example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XMLCompositeQuoteswithSelect.xsd"> 
    <title>some text goes here</title> 
    <atopelem>a top elem</atopelem> 
    <date>today</date> 
    <anode anodeattr="an attribute"> 
     <asubnode>a subnode</asubnode> 
     <somemorecontent>more content</somemorecontent> 
    </anode> 
    <anode anodeattr="another attribute"> 
     <asubnode>another subnode</asubnode> 
     <somemorecontent>even more content</somemorecontent> 
    </anode> 
</example> 

내 XSL은 다음과 같습니다

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet 
version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="XMLCompositeQuoteswithSelect.xsd" 
exclude-result-prefixes="msxsl xsl xsi xmlns"> 

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

<xsl:template match="/"> 
    <exampleelems> 
     <xsl:copy-of select="*/title | */atopelem | */date" /> 
     <xsl:for-each select="*/anode"> 
      <xsl:element name="node"> 
       <xsl:element name="anodeattr"> 
        <xsl:value-of select="@anodeattr"/> 
       </xsl:element> 
       <xsl:apply-templates /> 
      </xsl:element> 
     </xsl:for-each> 
    </exampleelems> 
</xsl:template> 

<xsl:template match="/anode/*" > 
    <xsl:copy > 
     <xsl:apply-templates /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="node()|@*" > 
    <xsl:copy /> 
</xsl:template> 
</xsl:stylesheet> 

내 출력 XML은 다음과 같습니다

<?xml version="1.0" encoding="utf-16"?> 
<exampleelems> 
    <title xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">some text goes here</title> 
    <atopelem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">a top elem</atopelem> 
    <date xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">today</date> 
    <node> 
     <anodeattr>an attribute</anodeattr> 
     <asubnode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
     <somemorecontent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
    </node> 
    <node> 
     <anodeattr>another attribute</anodeattr> 
     <asubnode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
     <somemorecontent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
    </node> 
</exampleelems> 

그리고이 (약간 손질)는 실행하는 코드 :

// create the xsl transformer 
    XslCompiledTransform t = new XslCompiledTransform(true); 
    t.Load(reader); 

    // create the writer which will output the transformed xml 
    StringBuilder sb = new StringBuilder(); 
    //XmlWriterSettings tt = new XmlWriterSettings(); 
    //tt.Encoding = new UTF8Encoding(false); 
    XmlWriter results = XmlWriter.Create(new StringWriter(sb));//, tt); 

    // write the transformed xml out to a stringbuilder 
    t.Transform(input, null, results); 

아마 추측 할 수 있듯이 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 속성이 모든 하위 요소에 복사되는 것을 원하지는 않지만이를 중지하는 방법을 모르겠습니다.

모든 도움과 정보를 제공해 주셔서 감사합니다.

매트.

답변

2

루트 요소의 이름을 변형하고 속성을 복사하여 원하는대로 만들 수 있습니다.

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

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

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

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

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

    <xsl:template match="anode/@anodeattr"> 
    <xsl:element name="{name()}"> 
     <xsl:value-of select="."/> 
    </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 
+0

정말 고맙습니다. 이제는 제 XML로 작업하고 있습니다. 하나의 확장 질문 : "이 수준에서이 요소는 있지만 다른 요소는 없습니다"라고 어떻게 말합니까? 나는 내가 생각한 것뿐만 아니라 템플릿을 이해하지 못할 수도 있다고 생각한다. –

+0

& @Matt W :이 답변은 맞지만 설명이 빠져 있습니다. 스키마 인스턴스 네임 스페이스는 모든 노드 **의 범위이지만 문서 루트 **는 입력에 포함됩니다. 네임 스페이스 선언이 출력에 없으면 ** 직렬 요소 **에서 ** 출력 요소를 출력했습니다 (스타일 시트에서 어떻게 출력했는지보십시오) 직렬화면에서는'copy'와'copy- of' 명령. –

+0

매트, 나는 새로운 질문에서 당신이 "이 수준에서,이 요소에서 그러나 다른 요소에서는"달성하고자하는 것을 더 자세하게 설명하는 것이 더 낫다고 생각합니다. 처리를 위해 특정 하위 요소 (이름이 "foo") 만 선택하려는 경우 예 : ''대신에

관련 문제