gpt4 book ai didi

xml - 使用 XSLT 转换 EntityObject XML 的问题

转载 作者:行者123 更新时间:2023-12-04 16:55:20 25 4
gpt4 key购买 nike

我有一个从 Entity Framework 对象生成的 XML 文档。生成的 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 中发生了一些变化 - 您可以看到带有本地名称与 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() 方法,但真的很想弄清楚这一点。

最佳答案

姑且...
抱歉,我没有时间测试以下内容是否有效...

EDIT (在工作了一整天后完成......) 是的! 没错。 t' 只缺少模板匹配字符串的 *,它应该是“/*”而不仅仅是“/”

通过从 OP 的问题中复制 XML 文件(由于问题的示例 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 文档视为节点树,例如元素、属性和文本节点,其中每个节点的名称是其本地名称和 namespace 名称的组合。有趣的是,XPath 以一种不一致的方式处理默认命名空间,这就是为什么在 XSL 所针对的元素属于输入 XML 中完全限定的命名元素之前,人们不会注意到所有这些 xmlns 问题的原因......请参阅此 link更多细节。

当面对具有 xmlns 指定元素的 XML 输入时,XSL 脚本编写者必须确保完全限定其目标节点的名称(首选方法)或使用上面显示的 local-name() 技巧来指示 XPath 忽略 namespace 。要使用 xmlns 前缀的名称,可以像往常一样定义一个短前缀,然后它可以由 XSL 逻辑使用,如下所示。 (实际xmlns可能有误,因为示例xml文件不完整)
<?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>

关于xml - 使用 XSLT 转换 EntityObject XML 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1401261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com