简体   繁体   English

XSLT 根据属性值复制元素到另一个

[英]XSLT copy element to another according to attribute value

I need to copy a node according to an attribute in another node in the same element.我需要根据同一元素中另一个节点中的属性复制一个节点。 An example input XML is as follows:示例输入 XML 如下:

<?xml version="1.0"?>
<Company>
    <Department id="Marketing">
        <depName>Marketing</depName>
        <position>Marketer</position>
    </Department>
    <Employee id="e1" level="l1">
        <First>John</First>
        <Dep code="Marketing"></Dep>
    </Employee>
    <Employee id="e2" level="l1">
        <First>Jane</First>
        <Dep code="Marketing"></Dep>
    </Employee>
    <Boss id="e3" level="l2">
        <First>Ben</First>
        <Dep code="Marketing"></Dep>
    </Boss>
</Company>

The output XML should look as follows: output XML 应如下所示:

<?xml version="1.0"?>
<Company>
    <Department id="Marketing">
        <depName>Marketing</depName>
        <position>Marketer</position>
    </Department>
    <Employee id="e1" level="l1">
        <First>John</First>
        <Dep code="Marketing"></Dep>
        <position>Marketer</position>
    </Employee>
    <Employee id="e2" level="l1">
        <First>Jane</First>
        <Dep code="Marketing"></Dep>
        <position>Marketer</position>
    </Employee>
    <Boss id="e3" level="l2">
        <First>Ben</First>
        <Dep code="Marketing"></Dep>
    </Boss>
</Company>

Of course there can be many more employees and departments.当然可以有更多的员工和部门。 I need to copy the position element from the Department to each of the Employees that work in the Department (eg for Marketing have <Dep code="Marketing"> ), but not to the Bosses .我需要将 position 元素从 Department 复制到该部门的每个员工(例如,对于 Marketing 有<Dep code="Marketing"> ),而不是 Bosses The department can be checked using its id attribute or depName element, they should be identical.可以使用其id属性或depName元素检查部门,它们应该相同。

I'm new to an XSLT so just have a basic idea how to select the element but don't really know how to go on from here (copy it to the correct place):我是 XSLT 的新手,所以只需了解如何使用 select 元素,但不知道如何使用 go 从这里开始(复制到正确的位置):

<xsl:template match="Department">
    <xsl:copy>
        <xsl:copy-of select="position"/>
    </xsl:copy>
</xsl:template>

Edit: Added attributes to employees, they need to be retained in the output编辑:为员工添加了属性,他们需要保留在 output

Declare a key声明一个密钥

<xsl:key name="dep" match="Department" use="@id"/>

and then write the template for Employee然后为Employee编写模板

<xsl:template match="Employee">
  <xsl:copy>
     <xsl:copy-of select="@*, node(), key('dep', Dep/@code)/position"/>
  </xsl:copy>
</xsl:template>

The comma operator , is availble with XSLT 2 or 3 processors, for an XSLT 1 processor use two separate逗号运算,适用于 XSLT 2 或 3 个处理器,对于 XSLT 1 处理器使用两个单独的

<xsl:copy-of select="@* | node()"/>
<xsl:copy-of select="key('dep', Dep/@code)/position"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当匹配一个属性的值时,将一个完整的元素复制到一个新的 XML,并使用 XSLT 更改内部另一个属性的值 - copy one complete element to a new XML when match the value of an attribute, and changing the value of another attribute inside, using XSLT 复制具有属性和名称空间xslt的元素 - copy element with attribute and namespace xslt XSLT-通过比较其他元素属性来复制属性 - XSLT - copy attribute by comparing other element attribute 如何将一个元素的属性值与另一个元素匹配,然后用 XSLT 1.0 将两个属性的总和变成另一个属性 - how to match the attribute value of one element with another element, and then make the sum of two attributes into another attribute with XSLT 1.0 具有属性值的XSLT重命名元素 - XSLT rename element with attribute value 如果另一个属性的值与使用xslt存储在变量中的值匹配,则选择元素的xml属性的值 - select the value of an xml attribute of an element if value of another attribute matches with value stored in a variable using xslt 每个匹配元素的XSLT 1.0 Copy属性 - XSLT 1.0 Copy attribute of each matching element XSLT:将属性从一个节点复制到另一个节点 - XSLT: Copy an attribute from one node to another 使用XSLT将元素属性添加到另一个元素的另一个属性 - Using XSLT prepend element attribute to another attribute of another element Xmlstarlet - 将一个属性的值复制到另一个,如果它存在于一个元素中 - Xmlstarlet - copy value of one attribute to another, if it exists in an element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM