通过 XSLT 根据条件复制具有不同属性的节点或将子节点添加到其他节点

Copy node with different attribute or Add child nodes to other node based on condition via XSLT

我正在开发一个 xslt,它将用于翻译一组 xmls 文件,示例输入 xml 类似于:-

<?xml version="1.0" encoding="UTF-8"?>  
<bookstore>

  <book id="A12">
    <bookattribute  name="abc"  price="200" />
    <bookattribute  name="xyz"  price="300" />
    <bookattribute  name="pqr"  price="400" />
    <bookattribute  name="lnz"  price="500" />
  </book>

  <book id="B12">
    <bookattribute  name="cpz"  price="300" />
    <bookattribute  name="rts"  price="800" />
  </book>


  <novel id="AA12">
    <bookattribute  name="yps"  price="200" />
    <bookattribute  name="udv"  price="600" />
  </novel>

  <!-- book node with id=AA12 may or may not be present in the xml  -->

  <book id="AA12">
    <bookattribute  name="abc"  price="200" />
    <bookattribute  name="aps"  price="600" /> 
  </book>

</bookstore>

我想通过创建具有属性 id="A12" 的节点副本 "book" 来转换它,基于以下条件,如果不存在具有属性 id=AA12 的节点书,则创建副本node "book"(attribute id="A12") 并在副本中将 id 更改为 "AA12"(因此它的节点副本具有更改的属性值),但是如果 node with book(id="AA12") 已经存在于 xml 中,然后添加 book(id=A12) 中不存在于 "book" 中的那些子节点(属性 id="AA12"), 在这里,我只想添加那些在 book@id=AA12 中不存在的子节点(以 bookattribute "name" 作为键),例如,如果子节点 bookattribute name="abc" 已经存在于book@id=AA12 然后它不应该再次添加到它,进一步它们可能是某些其他元素节点,如小说或电子书,它们也可能具有属性 id="AA12" 这些节点必须按原样复制。

so输出,以防万一(上面的输入文件包含节点"book"(属性id="AA12"))

<bookstore>
            <book id="A12">
                <bookattribute  name="abc"  price="200" />
                <bookattribute  name="xyz"  price="300" />
                <bookattribute  name="pqr"  price="400" />
                <bookattribute  name="lnz"  price="500" />
              </book>

              <book id="B12">
                <bookattribute  name="cpz"  price="300" />
                <bookattribute  name="rts"  price="800" />
              </book>

  <novel id="AA12">
    <bookattribute  name="yps"  price="200" />
    <bookattribute  name="udv"  price="600" />
  </novel>

               <book id="AA12">
                <bookattribute  name=aps  price=600 />
                <bookattribute  name="abc"  price="200" />
                <bookattribute  name="xyz"  price="300" />
                <bookattribute  name="pqr"  price="400" />
                <bookattribute  name="lnz"  price="500" />
              </book>
    </bookstore>

输出,以防(输入文件不包含节点"book"(属性id="AA12"))

    <bookstore>

                  <book id="A12">
                    <bookattribute  name="abc"  price="200" />
                    <bookattribute  name="xyz"  price="300" />
                    <bookattribute  name="pqr"  price="400" />
                    <bookattribute  name="lnz"  price="500" />
                  </book>

                  <book id="B12">
                    <bookattribute  name="cpz"  price="300" />
                    <bookattribute  name="rts"  price="800" />
                  </book>

 <novel id="AA12">
        <bookattribute  name="yps"  price="200" />
        <bookattribute  name="udv"  price="600" />
      </novel>

                   <book id="AA12">
                    <bookattribute  name="abc"  price="200" />
                    <bookattribute  name="xyz"  price="300" />
                    <bookattribute  name="pqr"  price="400" />
                    <bookattribute  name="lnz"  price="500" />
                  </book>
        </bookstore>

我已经能够如下创建 xslt,它会根据需要创建具有更改属性的副本,但我无法计算出添加子节点,以防 id="AA12" 的节点簿存在,任何指针都会有帮助

我的 xslt:-

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
                
<xsl:output method="xml"/>
    
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
       </xsl:copy>
    </xsl:template>

    <xsl:template match="comment()|processing-instruction()">
        <xsl:comment>
            <xsl:value-of select="."/>
        </xsl:comment>
    </xsl:template>
    
    <xsl:template match="book[@id='A12']">
        <xsl:copy>
            <xsl:attribute name="id">A12</xsl:attribute>
            <xsl:apply-templates />
        </xsl:copy>
        <xsl:copy>
            <xsl:attribute name="id">AA12</xsl:attribute>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

发帖时请小心XML,您输入的文档格式不正确。

使用 key 通过 ID 识别 book 元素。那么,理由如下:

  • 找到bookstore元素并将其复制到输出
  • bookstore 中,首先复制输入文档中的所有内容,除了 book 元素,其中 id="AA12"
  • 然后在所有情况下构造一个新元素 <book id="AA12">,并将(可能)book id="AA12"book id="A12"
  • 的所有子元素复制到其中

XSLT 样式表

编辑 我已编辑样式表以响应

this xslt may require minor fix as it may duplicate the child nodes, for example if is already present in book@id="A12" and "AA12", a filter to avoid copying duplicate child nodes would be required

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="book-by-id" match="book" use="@id"/>

    <xsl:template match="bookstore">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()[not(self::book and @id = 'AA12')]"/>
          <book id="AA12">
              <xsl:copy-of select="key('book-by-id','AA12')/*"/>
              <xsl:for-each select="key('book-by-id','A12')/*">
                  <xsl:if test="not(./@name = key('book-by-id','AA12')/*/@name)">
                      <xsl:copy-of select="."/>
                  </xsl:if>
              </xsl:for-each>
          </book>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:transform>

备选方案:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="book-by-id" match="book" use="@id"/>
    <xsl:key name="book-attribute-by-name" match="bookattribute" use="@name"/>

    <xsl:template match="bookstore">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()[not(@id = 'AA12')]"/>
          <book id="AA12">
              <xsl:apply-templates select="key('book-by-id','AA12')/*,key('book-by-id','A12')/*"/>
          </book>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="key('book-by-id','A12')/*[@name = key('book-by-id','AA12')/*/@name]"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:transform>

XML输出

如果已经有 book 元素 id="AA12":

<bookstore>
   <book id="A12">
      <bookattribute name="abc" price="200"/>
      <bookattribute name="xyz" price="300"/>
      <bookattribute name="pqr" price="400"/>
      <bookattribute name="lnz" price="500"/>
   </book>
   <book id="B12">
      <bookattribute name="cpz" price="300"/>
      <bookattribute name="rts" price="800"/>
   </book>
   <!-- book node with id=AA12 may or may not be present in the xml  -->
   <book id="AA12">
      <bookattribute name="abc" price="200"/>
      <bookattribute name="xyz" price="300"/>
      <bookattribute name="pqr" price="400"/>
      <bookattribute name="lnz" price="500"/>
      <bookattribute name="aps" price="600"/>
   </book>
</bookstore>

如果没有这样的元素:

<bookstore>
   <book id="A12">
      <bookattribute name="abc" price="200"/>
      <bookattribute name="xyz" price="300"/>
      <bookattribute name="pqr" price="400"/>
      <bookattribute name="lnz" price="500"/>
   </book>
   <book id="B12">
      <bookattribute name="cpz" price="300"/>
      <bookattribute name="rts" price="800"/>
   </book>
   <!-- book node with id=AA12 may or may not be present in the xml  -->
   <book id="AA12">
      <bookattribute name="abc" price="200"/>
      <bookattribute name="xyz" price="300"/>
      <bookattribute name="pqr" price="400"/>
      <bookattribute name="lnz" price="500"/>
   </book>
</bookstore>