2009-09-09 6 views
1

Entity Framework 객체에서 생성 한 XML 문서가 있습니다. 생성 된 XML은 다음과 같습니다XSLT를 사용하여 EntityObject XML 변환 문제

전체 XML

<Task z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/MCC.DAL" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"> 
    <EntityKey z:Id="i2" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" 
      xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
    <a:EntityContainerName>Editorial_MasterEntities</a:EntityContainerName> 
    <a:EntityKeyValues> 
     <a:EntityKeyMember> 
     <a:Key>TaskID</a:Key> 
     <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1670</a:Value> 
     </a:EntityKeyMember> 
    </a:EntityKeyValues> 
    <a:EntitySetName>Task</a:EntitySetName> 
    </EntityKey> 
    <FormCOIAns i:nil="true" /> 
    <FormCOIAnsReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
    <a:EntityKey i:nil="true" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </FormCOIAnsReference> 
    <FormCrownLicAns z:Id="i3"> 
    <EntityKey z:Id="i4" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
     <a:EntityContainerName>Editorial_MasterEntities</a:EntityContainerName> 
     <a:EntityKeyValues> 
     <a:EntityKeyMember> 
      <a:Key>TaskID</a:Key> 
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1670</a:Value> 
     </a:EntityKeyMember> 
     </a:EntityKeyValues> 
     <a:EntitySetName>FormCrownLicAns</a:EntitySetName> 
    </EntityKey> 
    <CrownAgreement>1</CrownAgreement> 
    <GovernmentAgency>ASDSADSADSADSA</GovernmentAgency> 
    <Task z:Ref="i1" /> 
    <TaskID>1670</TaskID> 
    <TaskReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
     <a:EntityKey z:Ref="i2" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </TaskReference> 
    </FormCrownLicAns> 
    <FormCrownLicAnsReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
    <a:EntityKey z:Ref="i4" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </FormCrownLicAnsReference> 
    <FormStdLicAns i:nil="true" /> 
    <FormStdLicAnsReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
    <a:EntityKey i:nil="true" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </FormStdLicAnsReference> 
    <FormType i:nil="true" /> 
    <FormTypeReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
    <a:EntityKey z:Id="i5" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"> 
     <b:EntityContainerName>Editorial_MasterEntities</b:EntityContainerName> 
     <b:EntityKeyValues> 
     <b:EntityKeyMember> 
      <b:Key>FormType_ID</b:Key> 
      <b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">3</b:Value> 
     </b:EntityKeyMember> 
     </b:EntityKeyValues> 
     <b:EntitySetName>FormType</b:EntitySetName> 
    </a:EntityKey> 
    </FormTypeReference> 
    <LastModified i:nil="true" /> 
    <Manuscript z:Id="i6"> 
    <EntityKey z:Id="i7" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
     <a:EntityContainerName>Editorial_MasterEntities</a:EntityContainerName> 
     <a:EntityKeyValues> 
     <a:EntityKeyMember> 
      <a:Key>ManuscriptID</a:Key> 
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">3000004</a:Value> 
     </a:EntityKeyMember> 
     <a:EntityKeyMember> 
      <a:Key>PubID</a:Key> 
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">3</a:Value> 
     </a:EntityKeyMember> 
     </a:EntityKeyValues> 
     <a:EntitySetName>Manuscript</a:EntitySetName> 
    </EntityKey> 
    <Editor i:nil="true" /> 
    <EditorReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
     <a:EntityKey z:Id="i8" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"> 
     <b:EntityContainerName>Editorial_MasterEntities</b:EntityContainerName> 
     <b:EntityKeyValues> 
      <b:EntityKeyMember> 
      <b:Key>EditorID</b:Key> 
      <b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">3</b:Value> 
      </b:EntityKeyMember> 
     </b:EntityKeyValues> 
     <b:EntitySetName>Editor</b:EntitySetName> 
     </a:EntityKey> 
    </EditorReference> 
    <ManuscriptID>3000004</ManuscriptID> 
    <ManuscriptStatus i:nil="true" /> 
    <ManuscriptStatusReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
     <a:EntityKey i:nil="true" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </ManuscriptStatusReference> 
    <PubID>3</PubID> 
    <Publication i:nil="true" /> 
    <PublicationReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
     <a:EntityKey z:Id="i9" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"> 
     <b:EntityContainerName>Editorial_MasterEntities</b:EntityContainerName> 
     <b:EntityKeyValues> 
      <b:EntityKeyMember> 
      <b:Key>PubID</b:Key> 
      <b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">3</b:Value> 
      </b:EntityKeyMember> 
     </b:EntityKeyValues> 
     <b:EntitySetName>Publication</b:EntitySetName> 
     </a:EntityKey> 
    </PublicationReference> 
    <Task> 
     <Task z:Ref="i1" /> 
    </Task> 
    <Title>test</Title> 
    </Manuscript> 
    <ManuscriptReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
    <a:EntityKey z:Ref="i7" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </ManuscriptReference> 
    <MasterPerson z:Id="i10"> 
    <EntityKey z:Id="i11" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
     <a:EntityContainerName>Editorial_MasterEntities</a:EntityContainerName> 
     <a:EntityKeyValues> 
     <a:EntityKeyMember> 
      <a:Key>MPID</a:Key> 
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1380007</a:Value> 
     </a:EntityKeyMember> 
     </a:EntityKeyValues> 
     <a:EntitySetName>MasterPerson</a:EntitySetName> 
    </EntityKey> 
    <Comments i:nil="true" /> 
    <Created>2008-01-08T14:14:01.867</Created> 
    <Do_Not_Use>false</Do_Not_Use> 
    <Editor /> 
    <FName>tony</FName> 
    <LName>kim</LName> 
    <MName /> 
    <MPGUID>C6C28CFFD41A4106B3FDB4CFBBF79B63</MPGUID> 
    <MPID>1380007</MPID> 
    <Prefix i:nil="true" /> 
    <Suffix i:nil="true" /> 
    <Task> 
     <Task z:Ref="i1" /> 
    </Task> 
    <Updated>2008-01-08T14:14:01.867</Updated> 
    </MasterPerson> 
    <MasterPersonReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
    <a:EntityKey z:Ref="i11" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </MasterPersonReference> 
    <TaskID>1670</TaskID> 
    <TaskStatus z:Id="i12"> 
    <EntityKey z:Id="i13" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data"> 
     <a:EntityContainerName>Editorial_MasterEntities</a:EntityContainerName> 
     <a:EntityKeyValues> 
     <a:EntityKeyMember> 
      <a:Key>TaskStatusID</a:Key> 
      <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">3</a:Value> 
     </a:EntityKeyMember> 
     </a:EntityKeyValues> 
     <a:EntitySetName>TaskStatus</a:EntitySetName> 
    </EntityKey> 
    <Description>Completed</Description> 
    <Task> 
     <Task z:Ref="i1" /> 
    </Task> 
    <TaskStatusID>3</TaskStatusID> 
    </TaskStatus> 
    <TaskStatusReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses"> 
    <a:EntityKey z:Ref="i13" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" /> 
    </TaskStatusReference> 
</Task> 

이 어제 일하는 스타일 시트했지만, 분명히 뭔가가 내 XML 변경 재 게시 - 로컬 - 이름으로 된 컴포지션을 볼 수 있습니다 vs. XPath.

<?xml version='1.0'?> 
    <xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" 
     xmlns:c="http://www.w3.org/2001/XMLSchema" 
     xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" 
     xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" 
     xmlns="http://schemas.datacontract.org/2004/07/MCC.DAL"> 
     <!--xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" --> 

     <xsl:template match="/*"> 
     <DIV STYLE="font-weight:bold"> 
      Local name:<xsl:value-of select="/*[local-name()='Task']/*[local-name()='MasterPerson']/*[local-name()='FName']" /><br /> 
      XPath:<xsl:value-of select="/Task/MasterPerson/FName"/> 
     </DIV> 
     </xsl:template> 
    </xsl:stylesheet> 

내가 네임 스페이스를 나열하는 데 문제는 그 결과 문제가 될 수 있도록 그들 중 일부는, 태그 레벨의 컨텍스트를 기반으로 겹쳐있다. 어제 분명히 다른 소스 XML (argh)을 사용하거나 전혀 다른 객체 세트를 사용하고있었습니다. (어느 쪽이든 소스가 변경되면 XSLT를 이렇게 폭파하지 않아야합니다.)

지금 당장은 local-name() 접근 방식을 사용하는 것으로 전락했지만 실제로 이것을 알아 내고 싶습니다.

+0

당신의 XML은 완전한 요소가없는 (샘플 XML 파일이 불완전로 실제의 xmlns가 잘못 될 수 있음), 그래서 그것을로드 할 수 없습니다. 오픈 엘리먼트를 닫으면 네임 스페이스 요소를 지우더라도 Xalan에서 결과를 얻지 못합니다. 예제에 'MasterPerson'이 없기 때문입니다. –

+0

죄송합니다 Tim - 지나치게 큽니다. –

+0

@CP : 편집을 위해 죄송 합니다만 질문 앞에 대답을 읽는 것이 매우 혼란 스럽습니다. ;-) 필자도 XML을 들여 쓰기 (well-formed)했다. 오, 그리고 환영합니다. :-) – Tomalak

답변

2

가칭 ... 나는 (... 직장에서 긴 하루 후 마무리) 다음 작품 ...

편집는 것을 을 테스트하는 시간을하지 않은 죄송 예! 그건 맞았 어. '/ *'가 아닌 템플릿 일치 문자열에 대해 *가 누락되었습니다.

OP의 질문에서 복사 한 XML 파일로 테스트를 완료했습니다. 질문의 예제 XML은 단편적입니다.) 그러나주의! * [local-name() = 'xyz'] 트릭을 사용하여 빠른 테스트를 위해 네임 스페이스를 무시해야합니다. 이러한 XML 네임 스페이스를 처리하는 가장 좋은 방법은 코드 스 니펫을 참조하십시오.

<xsl:template match="/"> 
    <DIV STYLE="font-weight:bold"> 
     <xsl:value-of select="*[local-name()='Task']/*[local-name()='MasterPerson']/*[local-name()='FName']" /> 
    </DIV> 
    </xsl:template> 

XPath와 네임 스페이스 XPath 데이터 모델은 각 노드의 이름의 조합 등의 요소, 속성, 텍스트 노드와 같은 노드의 트리로 XML 문서를 처리

로컬 이름과 네임 스페이스 이름. 흥미롭게도 XPath는 일치하지 않는 방식으로 기본 네임 스페이스를 처리하기 때문에 XSL이 대상으로하는 요소가 입력 XML의 정규화 된 명명 된 요소에 속할 때까지이 모든 xmlns 문제를 알지 못하는 이유가 여기에 있습니다.이 부분을 참조하십시오. link 상세 사항은.

Xml 스크립트 작성자는 xmlns 지정 요소가있는 XML 입력에 직면 할 때 대상 노드의 이름을 정규화하거나 (위의 권장 방법) 위의 local-name() 트릭을 사용하여 XPath에 네임 스페이스를 무시하십시오. xmlns 접두어로 붙인 이름을 사용하기 위해, 짧은 접두사는 일반적으로 정의 될 수 있으며 다음과 같이 XSL 논리에 의해 사용될 수 있습니다.

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:t="http://schemas.datacontract.org/2004/07/MCC.DAL"> 

<xsl:template match="/MyRoot"> 
    <DIV STYLE="font-weight:bold"> 
     <xsl:value-of select="t:Task/i:MasterPerson/i:FName" /> 
    </DIV> 
    </xsl:template> 
</xsl:stylesheet> 
+0

표현식 앞에 "/"를 추가하면 local-name() 접근 방식이 작동합니다. 로컬 이름 접근 방식을 사용할 때의 단점은 무엇입니까? 그냥 궁금해서 ... 나는이 모든 네임 스페이스를 내 XSLT에 추가 할 수 있는지 확인하려고합니다. 감사합니다. CP –

+0

오른쪽 /은 XML 입력에 따라 필요할 수 있습니다. 로컬 이름 접근법을 사용하여 네임 스페이스를 무시하는 단점 :이 네임 스페이스를 사용하는 XPath 식의 정확도가 떨어지며 다른 네임 스페이스에서 같은 이름을 가진 "잘못된"노드와 일치하거나, 노드 속성 (@ thing) 등을 포함하는 셀렉터에서 예를 들어 같은 종류의 특이성을 표현할 수 있습니다. 간단히 말해서 XPath 표현식은 더 이상 더 이상적이고 더 비속식적이며 더 구체적이지 않으며 바람직하지 않은 목표와 일치 할 수 있습니다. – mjv

관련 문제