首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT -只复制特定的元素及其子元素(而不是节点的父元素)

XSLT -只复制特定的元素及其子元素(而不是节点的父元素)
EN

Stack Overflow用户
提问于 2022-07-14 06:44:07
回答 1查看 35关注 0票数 0

我如何只复制特定的元素与他们的孩子?所以,不是父元素。

示例XLST文件:

代码语言:javascript
复制
<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"/>
    <header>Some inserted text</header>
    <xsl:template match="staff">
        <xsl:apply-templates select="subelement"/>
    </xsl:template>
    <xsl:template match="subelement">
        <xsl:copy-of select="*"/>
    </xsl:template>
</parent>

示例XML文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<company>
    <staff attrib="select" id="1001">
        <name>should-1</name>
        <role>copy-1</role>
    </staff>
    <staff id="1002">
        <name>should-3</name>
        <role>not-copy-3</role>
    </staff>
    <staff attrib="select" id="1002">
        <name>should-2</name>
        <role>copy-2</role>
    </staff>
</company>

通缉结果:(因此,没有“公司”元素)

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
  <parent><header>Some inserted text</header>
      <staff attrib="select" id="1001">
        <name>should-1</name>
        <role>copy-1</role>
      </staff>
      <staff id="1002">
        <name>should-3</name>
        <role>not-copy-3</role>
      </staff>
      <staff attrib="select" id="1002">
        <name>should-2</name>
        <role>copy-2</role>
      </staff>
  </parent>

结果:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
  <parent><p>Some inserted text</p>
    <company>
      <staff attrib="select" id="1001">
        <name>should-1</name>
        <role>copy-1</role>
      </staff>
      <staff id="1002">
        <name>should-3</name>
        <role>not-copy-3</role>
      </staff>
      <staff attrib="select" id="1002">
        <name>should-2</name>
        <role>copy-2</role>
      </staff>
    </company>
  </parent>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-14 06:52:13

尝试:

XSLT1.0

代码语言:javascript
复制
<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="/company">
    <parent>
        <header>Some inserted text</header>
        <xsl:copy-of select="staff"/>
    </parent>
</xsl:template>

</xsl:stylesheet>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72976257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档