如何删除不同节点的重复属性值,并将剩余的属性值附加到当前节点

时间:2017-01-12 06:37:50

标签: node.js xml xslt

我有一个XML文件,如:

<Documents>
  <Document>
     <firstname>Vaneet</firstname>
     <lastname>chendra</lastname> 
     <ID_DOC>101121</ID_DOC>
     <Doc_Type>html</Doc_Type>
  </Document>
  <Document>
     <firstname>joshna</firstname>
     <lastname>shekar</lastname> 
     <ID_DOC>101121</ID_DOC>
     <Doc_Type>Text</Doc_Type>
  </Document>
</Documents>

我的输出XML应该像

<Documents>
  <Document>
     <firstname>Vaneet</firstname>
     <lastname>chendra</lastname> 
     <ID_DOC>101121</ID_DOC>
     <Doc_Type>html</Doc_Type>
     <firstname>joshna</firstname>
     <lastname>shekar</lastname>
     <Doc_Type>Text</Doc_Type>
  </Document>
</Documents>

我尝试了下面的XSLT,但无法获得输出 应删除具有不同父节点相同值的子节点,并且需要将重复子节点的父节点的其余子节点附加到上一个父节点enter code here

    <?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

   <xsl:template match = "/">
      <html>
         <body>
            <h3>Details of each Documents. Xpath expression = "/Documents/Document"</h3>

            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>ID_DOC</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Doc_Type</th>
               </tr>

            <xsl:for-each select = "/Documents/Document">
                <xsl:variable name="myVar" select="@ID_DOC"
               <xsl:for-each select = "/Documents/Document">
                    <xsl:variable name="i" select="position()+1" />
                    <xsl:variable name="myVar1" select="ID_DOC[i]"
                  <xsl:if test="$myVar != $myVar1">
            <!comment : i am trying to compare the previous node value with current node value>   
            <!comment : and also to append it to the previous parent node if present and i am not able to do so>      
                <tr>
                     <td><xsl:value-of select = "ID_DOC"/></td>
                     <td><xsl:value-of select = "firstname"/></td>
                     <td><xsl:value-of select = "lastname"/></td>
                     <td><xsl:value-of select = "Doc_Type"/></td>
                  </tr>
                   </xsl:if>
            </xsl:for-each>  
               </xsl:for-each>
            </table> 
            </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

我试过通过链接解释,但我收到像net.sf.saxon.s9api.SaxonApiException这样的错误,我不知道错误在哪里,所以你能告诉我哪里出错了。 这是我试过的代码 - &gt; @ michael.hor257k

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

   <xsl:template match = "/">
      <html>
         <body>
            <h3>Details of each Documents. Xpath expression = "/Documents/Document"</h3>

            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>ID_DOC</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Doc_Type</th>
               </tr>
                <xsl:key name = "Documents-by-ID_DOC" match="Document" use="ID_DOC" />
                <xsl:template match="Documents">
                    <xsl:for-each select="Document[count(. | key('Documents-by-ID_DOC', ID_DOC)[1]) = 1]">
                        <xsl:sort select="ID_DOC" />
                        <xsl:value-of select="ID_DOC" />,<br />
                        <xsl:for-each select="key('Documents-by-ID_DOC', ID_DOC)">
                                <xsl:sort select="Doc_Type" />
                                <xsl:value-of select="Doc_Type" /> (<xsl:value-of select="firstname" />)<br />
                        </xsl:for-each>
                    </xsl:for-each>
                </xsl:template> 

            </table> 
            </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

您可以帮助使用XSLT获取输出吗? 感谢提前

0 个答案:

没有答案