Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

xslt - How to select only specific nodes from xml file

I would like to select only the Code & Naam node from all the OrganisatieEenheid nodes inside the xml below. How could i do that?

<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>

So i would like to receive the output:

<root>
        <OrganisatieEenheid>
           <Code>XXX</Code>
           <Naam>Name 1</Naam>
        </OrganisatieEenheid>
        <OrganisatieEenheid>
           <Code>YYY</Code>
           <Naam>Name 2</Naam>
        </OrganisatieEenheid>

I've tried it by using the following 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>

but this just generates an xml like

<root></root>

Can somebody show me what the correct syntax would be for my xslt file?

question from:https://stackoverflow.com/questions/66062213/how-to-select-only-specific-nodes-from-xml-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That looks like

<xsl:template match="/root">
  <xsl:copy>
    <xsl:apply-templates select="//OrganisatieEenheid"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="OrganisatieEenheid">
  <xsl:copy>
    <xsl:copy-of select="OrganisatieEenheidType/Code | OrganisatieEenheidType/Naam"/>
  </xsl:copy>
</xsl:template>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...