XSLTでEntityObject XMLを変換する際の問題
-
05-07-2019 - |
質問
Entity Frameworkオブジェクトから生成したXMLドキュメントがあります。生成されたXMLは次のようになります。
REPOSTED FULL 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で何かが変更されました。ローカル名と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()アプローチを使用することに追いやられていますが、本当にこれを理解したいと思います。
解決
暫定的に... 申し訳ありませんが、次の動作をテストする時間はありません...
編集(仕事で長い一日を終えて...)はいそれはほぼ正しい。テンプレート一致文字列の*のみが欠落していました。これは&quot; / *&quotである必要があります。 &quot; /&quot;だけではありません
これは、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の問題に気付かないのです。詳細については、a href = "http://www.xml.com/pub/a/2004/02/25/qanda.html" rel = "nofollow noreferrer">リンクをご覧ください。
xmlnsで指定された要素を持つXML入力に直面した場合、XSLスクリプトの作成者は、ターゲットノードの名前を完全に修飾する(推奨されるアプローチ)か、上記のlocal-name()トリックを使用してXPathに無視するよう指示する必要があります名前空間。 xmlns接頭辞付きの名前を使用するには、通常どおり短い接頭辞を定義し、次のようにXSLロジックで使用できます。 (サンプルxmlファイルが不完全であるため、実際のxmlnsは間違っている可能性があります)
<?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>