【发布时间】:2023-03-18 02:53:01
【问题描述】:
我想从下面的 xml 中的所有 OrganisatieEenheid 节点中仅选择 Code & Naam 节点。我怎么能这样做?
<root>
<OrganisatieEenheid>
<Code>XXX</Code>
<OrganisatieEenheidType>
<Code></Code>
<Naam></Naam>
</OrganisatieEenheidType>
<Hoofd>
<MasterKey></MasterKey>
<AlternateKey></AlternateKey>
<Naam></Naam>
<Voornaam></Voornaam>
</Hoofd>
<Naam>Name 1</Naam>
<OrganisatieCode></OrganisatieCode>
<Parent>
<Code></Code>
<OrganisatieEenheidType/>
<Hoofd>
<MasterKey/>
<AlternateKey/>
<Naam/>
<Voornaam/>
</Hoofd>
<Naam></Naam>
<OrganisatieCode/>
<Parent/>
<Niveau></Niveau>
</Parent>
<Niveau></Niveau>
</OrganisatieEenheid>
<OrganisatieEenheid>
<Code>YYY</Code>
<OrganisatieEenheidType>
<Code></Code>
<Naam></Naam>
</OrganisatieEenheidType>
<Hoofd>
<MasterKey></MasterKey>
<AlternateKey></AlternateKey>
<Naam></Naam>
<Voornaam></Voornaam>
</Hoofd>
<Naam>Name 2</Naam>
<OrganisatieCode></OrganisatieCode>
<Parent>
<Code></Code>
<OrganisatieEenheidType/>
<Hoofd>
<MasterKey/>
<AlternateKey/>
<Naam/>
<Voornaam/>
</Hoofd>
<Naam></Naam>
<OrganisatieCode/>
<Parent/>
<Niveau></Niveau>
</Parent>
<Niveau></Niveau>
</OrganisatieEenheid>
</root>
所以我想收到输出:
<root>
<OrganisatieEenheid>
<Code>XXX</Code>
<Naam>Name 1</Naam>
</OrganisatieEenheid>
<OrganisatieEenheid>
<Code>YYY</Code>
<Naam>Name 2</Naam>
</OrganisatieEenheid>
我已经使用以下 xslt 进行了尝试:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"node()[not(self::root or ancestor-or-self::code or ancestor-or-self::naam)]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
但这只会生成一个类似的xml
<root></root>
谁能告诉我 xslt 文件的正确语法是什么?
【问题讨论】: