2011-03-26 3 views
3

나는 XML 파일에 다음과 같은 구조를 가지고 : 나는 C#을 객체로이를 역 직렬화하려고XSL에서 XML 계층을 병합 할 수 있습니까?

<INSTANCE> 
    <Sections> 
    <Section> 
     <Forms> 
     <Form> 
      <Control id="GroupHeading1"> 
      <Property/> 
      <Property/> 
      </Control> 
      <Control id="GroupHeading2"> 
      <Property/> 
      <Control id="TextBox"> 
       <Property/> 
       <Property/> 
      </Control> 
      </Control> 
     </Form> 
     </Forms> 
    </Section> 
    </Sections> 
</INSTANCE> 

을,하지만 난 (어려운 나 역 직렬화 수 있도록하는 것입니다) 계층 구조를 보존 할 필요가 없습니다.

컨트롤을 un-nest로 변환 할 수있는 XSL이 있습니까? 가능한 경우 ParentId = ""인 하위 컨트롤에 특성을 추가 하시겠습니까?

안내해 주셔서 감사합니다.

+0

비트 복제 : http://stackoverflow.com/questions/5025365/serialize-and-deserialize-xml-using-dot-net-c –

+0

@ user53885 : 원하는 출력이 없으면 완전한 질문이 아닙니다. –

+0

@ user53885 @ @ harpo의 대답에 대한 귀하의 의견을 전하면서 ​​- "저는 TextBox가 ParentId ="GroupHeading2 "라는 속성을 가지고있는 다른 컨트롤 아래에 컨트롤이있는 대신에 생각하고있었습니다. GroupHeadings. " - 모든 ''요소가 같은 수준으로 나타나는 솔루션을 추가했습니다. –

답변

1

. .NET 2.0에 대해 실행했습니다.

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes" method="xml"/> 

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

    <xsl:template match="Form"> 
     <Form> 
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates select="//Control"/> 
     </Form> 
    </xsl:template> 

    <xsl:template match="Control"> 
     <Control> 
      <xsl:if test="ancestor::Control/@id"> 
       <xsl:attribute name="ParentID"><xsl:value-of select="ancestor::Control/@id"/></xsl:attribute> 
      </xsl:if> 
      <xsl:copy-of select="*|@*"/> 
     </Control> 
    </xsl:template> 

</xsl:stylesheet> 

이것은 출력 (가독성을 위해 들여 쓰 기)입니다.

<INSTANCE> 
    <Sections> 
     <Section> 
      <Forms> 
       <Form> 
        <Control id="GroupHeading1"> 
         <Property /> 
         <Property /> 
        </Control> 
        <Control id="GroupHeading2"> 
         <Property /> 
         <Control id="TextBox"> 
          <Property /> 
          <Property /> 
         </Control> 
        </Control> 
        <Control ParentID="GroupHeading2" id="TextBox"> 
         <Property /> 
         <Property /> 
        </Control> 
       </Form> 
      </Forms> 
     </Section> 
    </Sections> 
</INSTANCE> 
+0

그것은 나를 편집 할 수 없습니다. 하지만 예를 들어 TextBox가 ParentId = "GroupHeading2"의 특성을 가지며 GroupHeadings과 동일한 수준이되는 다른 컨트롤 아래에 컨트롤이있는 대신 생각하고있었습니다. – user53885

+0

무엇을 편집 할 수 없습니까? 현재 위치에서 템플릿 파일을 다시 쓸 수 없다는 것을 의미하는 경우에는 deserialization 전에 항상 메모리에서 템플릿 파일을 변형 할 수 있습니다. – harpo

+0

죄송합니다. 여기에 게시 된 질문 자체를 의미합니다. 내가 요청한대로 업데이트하지 않을 것이다. – user53885

1

다음 스타일 :

<INSTANCE> 
    <Sections> 
     <Section> 
      <Forms> 
       <Form> 
        <Control id="GroupHeading1"> 
         <Property /> 
         <Property /> 
        </Control> 
        <Control id="GroupHeading2"> 
         <Property /> 
         <Control id="TextBox"> 
          <Property /> 
          <Property /> 
          <Control id="Grandchild"> 
           <Property /> 
          </Control> 
         </Control> 
        </Control> 
       </Form> 
      </Forms> 
     </Section> 
    </Sections> 
</INSTANCE> 

함께 출력 를 생성 :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <!-- first-level control elements --> <xsl:template match="Control"> <Control> <xsl:copy-of select="@*|*[not(self::Control)]" /> </Control> <xsl:apply-templates select="Control" /> </xsl:template> <!-- nested control elements --> <xsl:template match="Control/Control"> <Control ParentId="{../@id}"> <xsl:copy-of select="@*|*[not(self::Control)]" /> </Control> <xsl:apply-templates select="Control" /> </xsl:template> </xsl:stylesheet> 

다음 문서에 적용

(데모 용 중첩 추가 레벨 원래 동일) 중첩되지 않음 <Control> 요소
:

<INSTANCE> 
    <Sections> 
     <Section> 
      <Forms> 
       <Form> 
        <Control id="GroupHeading1"> 
         <Property /> 
         <Property /> 
        </Control> 
        <Control id="GroupHeading2"> 
         <Property /> 
        </Control> 
        <Control ParentId="GroupHeading2" id="TextBox"> 
         <Property /> 
         <Property /> 
        </Control> 
        <Control ParentId="TextBox" id="Grandchild"> 
         <Property /> 
        </Control> 
       </Form> 
      </Forms> 
     </Section> 
    </Sections> 
</INSTANCE> 
+0

+1 올바른 역 계층화 패턴. (푸시 스타일) –

+0

멋지다, 나는 xsl : copy 템플릿에 대해 몰랐다. – harpo

관련 문제