【问题标题】:How to select only specific nodes from xml file如何从 xml 文件中仅选择特定节点
【发布时间】: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 文件的正确语法是什么?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    看起来像

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

    【讨论】:

    • 是吗?它返回的结果与您的问题中显示的结果不同,因为 Code 和 Name 是从 OrganisatieEenheidType 子分支复制的。
    • @michael.hor257k,我想你是对的,我可能迷失在输入结构中并复制了错误的元素。奇怪的是,张贴者似乎已经能够使用它。
    【解决方案2】:

    来点简单的怎么样:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/root">
        <xsl:copy>
            <xsl:for-each select="OrganisatieEenheid">
                <xsl:copy>
                    <xsl:copy-of select="Code | Naam"/>
                </xsl:copy>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多