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

时间:2015-04-15 08:09:23

标签: xml xslt xslt-1.0

我正在使用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的节点簿,则创建节点“book”的副本(属性id =“A12”)并且在副本中将id更改为“AA12”(因此其节点的副本具有属性值的更改),但是如果xml中已存在具有book(id =“AA12”)的节点,则添加这些子节点book(id = A12)的节点,不存在于“book”中(属性id =“AA12”), 在这里,我想只添加那些子节点(bookattribute“name”作为键),这些节点在book @ id = AA12中不存在,例如,如果子节点bookattribute name =“abc”已存在于book @ id = AA12那么它不应该再添加到它,进一步它们可能是某些其他元素节点,如小说或电子书,也可能有属性id =“AA12”这些节点必须按原样复制。

输出,以防万一(上面的输入文件包含节点“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>

1 个答案:

答案 0 :(得分:0)

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

使用按ID标识book个元素。然后,理由如下:

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

XSLT样式表

编辑我已根据

编辑了样式表
  

这个xslt可能需要小修复,因为它可能会复制子节点,例如,如果已经存在于book @ id =“A12”和“AA12”中,则需要一个避免复制重复子节点的过滤器

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