将xml父元素和属性内容复制到子元素
无法获得发布的答案-以下示例更真实,因此可能有助于更好地解释我正在尝试做的事情: 当前的数据如下所示:将xml父元素和属性内容复制到子元素,xml,xslt,attributes,elements,Xml,Xslt,Attributes,Elements,无法获得发布的答案-以下示例更真实,因此可能有助于更好地解释我正在尝试做的事情: 当前的数据如下所示: <LabTest_ID>0001</LabTest_ID> <FullName>Cluster of differentiation 19+, lambda+ count</FullName> <SnomedCT conceptId="799711000000109" preferredTerm="Count of cells posi
<LabTest_ID>0001</LabTest_ID>
<FullName>Cluster of differentiation 19+, lambda+ count</FullName>
<SnomedCT conceptId="799711000000109" preferredTerm="Count of cells positive for both CD19 and lambda" />
<CollectedSpecimen id="blood" type="Blood" snomedCTConceptId="119297000">
<CollectionMethod id="L0000" name="Arterial" snomedCTConceptId="122552005" />
<CollectionMethod id="L0050" name="Venous" snomedCTConceptId="122555007" />
</CollectedSpecimen>
<CollectedSpecimen id="bonemarrow" type="Bone marrow" snomedCTConceptId="119359002">
<CollectionMethod id="L0001" name="Aspiration" snomedCTConceptId="801861000000103" />
</CollectedSpecimen>
0001
分化簇19+,λ+计数
我想从以下内容转换CollectionMethod的输出:
<CollectionMethod id="L0000" name="Arterial" snomedCTConceptId="122552005" />
为此:
<CollectionMethod LabTest_ID = "0001" type = "Blood" id="L0000" name="Arterial" snomedCTConceptId="122552005" />
也就是说,我想将LabTest的“LabTest_ID”和CollectedSample的“type”作为附加属性添加到CollectedMethod中
我对XML非常陌生,并尝试了以下代码,这些代码几乎可以正常工作,但它只在每个CollectedSample的CollectionMethod的第一次出现时输出LabTest_ID,而不是在每个样本中
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<dataroot>
<xsl:apply-templates select="@*|node()"/>
</dataroot>
</xsl:template>
<xsl:template match="CollectionMethod/@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="CollectedSpecimen/@type" />
<xsl:template match="CollectedSpecimen[@type]/*[1]">
<xsl:copy>
<xsl:apply-templates select="@*" />
<CollectedSpecimen-type>
<xsl:value-of select="../@type" />
</CollectedSpecimen-type>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CollectionMethod">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="CollectionMethod">
<CollectionMethod>
<LabTest_ID><xsl:value-of select="../../LabTest_ID"/></LabTest_ID>
<xsl:apply-templates select="@*|node()"/>
</CollectionMethod>
</xsl:template>
</xsl:stylesheet>
以下模板将新属性添加到XML中。标识模板复制除由更具体的模板处理的
孙节点
元素之外的所有节点
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="Grandchild">
<xsl:copy>
<xsl:copy-of select="@*" /> <!-- copies all existing attributes -->
<xsl:attribute name="Childid"> <!-- Adds the additional Childid attribute -->
<xsl:value-of select="../@id" />
</xsl:attribute>
<xsl:attribute name="Parentid"> <!-- Adds the additional Parrentid attribute -->
<xsl:value-of select="../../Parent_ID" />
</xsl:attribute>
</xsl:copy>
</xsl:template>
以下模板将新属性添加到XML中。标识模板复制除由更具体的模板处理的
孙节点
元素之外的所有节点
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="Grandchild">
<xsl:copy>
<xsl:copy-of select="@*" /> <!-- copies all existing attributes -->
<xsl:attribute name="Childid"> <!-- Adds the additional Childid attribute -->
<xsl:value-of select="../@id" />
</xsl:attribute>
<xsl:attribute name="Parentid"> <!-- Adds the additional Parrentid attribute -->
<xsl:value-of select="../../Parent_ID" />
</xsl:attribute>
</xsl:copy>
</xsl:template>
您是在Excel中执行此操作,还是使用Python之类的工具进行解析?谢谢您的回复。我试图将xml文件中的数据导入MS Access,但它无法保持关系。我希望将祖父母和父母的ID添加到孙辈中,以便在将数据导入Access时可以维护这些关系。您是在Excel中执行此操作还是使用Python之类的工具进行解析?谢谢您的回复。我试图将xml文件中的数据导入MS Access,但它无法保持关系。我希望将祖父母和父母的ID添加到孙辈中,以便在将数据导入Access时维护这些关系。