2017-06-03 8 views
-1

J'ai essayé de fusionner tous les nœuds de texte dans books.xml si leurs valeurs d'attribut correspondent. Et ces valeurs devraient être séparés par un délimiteur (EX:, etc ..)Fusion de nœuds de texte dans XSLT

Voici le XML souce:

<catalog> 
    <book id="bk101"> 
     <author>Gambardella, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <genre>Computer</genre> 
     <price>44.95</price> 
     <publish_date>2000-10-01</publish_date> 
     <description>An in-depth look at creating applications 
     with XML.</description> 
    </book> 
    <book id="bk102"> 
     <author>Ralls, Kim</author> 
     <title>Midnight Rain</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-12-16</publish_date> 
     <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world.</description> 
    </book> 
    <book id="bk103"> 
     <author>Corets, Eva</author> 
     <title>Maeve Ascendant</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-11-17</publish_date> 
     <description>After the collapse of a nanotechnology 
     society in England, the young survivors lay the 
     foundation for a new society.</description> 
    </book> 
    <book id="bk101"> 
     <author>Corets, Eva</author> 
     <title>Oberon's Legacy</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2001-03-10</publish_date> 
     <description>In post-apocalypse England, the mysterious 
     agent known only as Oberon helps to create a new life 
     for the inhabitants of London. Sequel to Maeve 
     Ascendant.</description> 
    </book> 
    <book id="bk102"> 
     <author>Corets, Eva</author> 
     <title>The Sundered Grail</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2001-09-10</publish_date> 
     <description>The two daughters of Maeve, half-sisters, 
     battle one another for control of England. Sequel to 
     Oberon's Legacy.</description> 
    </book> 
    <book id="bk101"> 
     <author>Galos, Mike</author> 
     <title>Visual Studio 7: A Comprehensive Guide</title> 
     <genre>Computer</genre> 
     <price>49.95</price> 
     <publish_date>2001-04-16</publish_date> 
     <description>Microsoft Visual Studio 7 is explored in depth, 
     looking at how Visual Basic, Visual C++, C#, and ASP+ are 
     integrated into a comprehensive development 
     environment.</description> 
    </book> 
</catalog> 

Ceci est le résultat souhaité:

<catalog> 
    <book id="bk101"> 
     <author>Gambardella, Matthew Galos, Mike Corets, Eva</author> 
     <title>XML Developer's Guide Visual Studio 7: A Comprehensive Guide Oberon's Legacy</title> 
     <genre>Computer Computer</genre> 
     <price>44.95 49.95 5.95</price> 
     <publish_date>2000-10-01 2001-04-16 2001-03-10</publish_date> 
     <description>An in-depth look at creating applications 
     with XML. Microsoft Visual Studio 7 is explored in depth, 
     looking at how Visual Basic, Visual C++, C#, and ASP+ are 
     integrated into a comprehensive development 
     environment. In post-apocalypse England, the mysterious 
     agent known only as Oberon helps to create a new life 
     for the inhabitants of London. Sequel to Maeve 
     Ascendant.</description> 
    <book id="bk102"> 
     <author>Ralls, Kim Corets, Eva</author> 
     <title>Midnight Rain The Sundered Grail</title> 
     <genre>Fantasy Fantasy</genre> 
     <price>5.95 5.95</price> 
     <publish_date>2000-12-16 2001-09-10</publish_date> 
     <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world. The two daughters of Maeve, half-sisters, 
     battle one another for control of England. Sequel to 
     Oberon's Legacy.</description> 
    </book> 
    <book id="bk103"> 
     <author>Corets, Eva</author> 
     <title>Maeve Ascendant</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-11-17</publish_date> 
     <description>After the collapse of a nanotechnology 
     society in England, the young survivors lay the 
     foundation for a new society.</description> 
    </book> 
</catalog> 

C'est le XSLT J'ai essayé:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:key name="Dbook" match="book" use="@id"/> 

    <xsl:template match="/catalog"> 
     <xsl:copy> 
     <xsl:for-each select="book[count(. | key('Dbook', @id)[1])=1]"> 
      <xsl:copy> 
      <xsl:copy-of select="@*"/> 
       <xsl:for-each select="key('Dbook',@id)">    
        <xsl:variable name="author" select="author"/> 
        <xsl:variable name="title" select="title"/> 
        <xsl:variable name="genre" select="genre"/> 
        <xsl:variable name="price" select="price"/> 
        <xsl:variable name="publish_date" select="publish_date"/> 
        <xsl:variable name="description" select="description"/> 
        <xsl:variable name="null"/> 

        <xsl:copy-of select="concat($author,$null)"/> 
        <xsl:copy-of select="concat($title,$null)"/> 
        <xsl:copy-of select="concat($genre,$null)"/> 
        <xsl:copy-of select="concat($price,$null)"/> 
        <xsl:copy-of select="concat($publish_date,$null)"/> 
        <xsl:copy-of select="concat($description,$null)"/> 

       </xsl:for-each> 
      </xsl:copy> 
     </xsl:for-each> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

S'il vous plaît ramasser soit XSLT 1.0 ou 2.0, pas les deux - esp. quand vous posez des questions sur le groupement. Dans XSLT 2.0, le regroupement est facile à réaliser en utilisant 'xsl: for-each-group'. Fusionner les nœuds de texte est également trivial en utilisant 'xsl: value-of' avec un attribut separator'. - Notez que la sortie que vous affichez ne peut pas être produite car un caractère '&' non échappé n'est pas autorisé en XML. –

+1

C'est un problème assez similaire à votre précédente question, alors peut-être devriez-vous commencer par accepter la réponse puis essayer de l'adapter à votre nouvelle exigence et nous montrer votre tentative si vous êtes bloqué. Par ailleurs, si votre sortie désirée est supposée être XML, alors le délimiteur doit être échappé comme ' Gambardella, Matthew & Galos, Mike & Corets, Eva' –

+0

@ michael.hor257k Je veux y arriver avec XSLT 1.0 et je vais bien même si je l'accomplis sans caractère et sans caractère –

Répondre

1

Une solution simple (même si fastidieuse) dans XSLT 1.0 ressemblerait à ceci:

XSLT 1,0

<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:strip-space elements="*"/> 

<xsl:key name="Dbook" match="book" use="@id"/> 
<xsl:variable name="separator" select="' &amp; '" /> 

<xsl:template match="/catalog"> 
    <xsl:copy> 
     <xsl:for-each select="book[count(. | key('Dbook', @id)[1])=1]"> 
      <xsl:copy> 
       <xsl:copy-of select="@*"/> 
       <xsl:variable name="group" select="key('Dbook', @id)" /> 
       <author> 
        <xsl:for-each select="$group"> 
         <xsl:value-of select="author"/> 
         <xsl:if test="position()!=last()"> 
          <xsl:value-of select="$separator"/> 
         </xsl:if> 
        </xsl:for-each> 
       </author> 
       <title> 
        <xsl:for-each select="$group"> 
         <xsl:value-of select="title"/> 
         <xsl:if test="position()!=last()"> 
          <xsl:value-of select="$separator"/> 
         </xsl:if> 
        </xsl:for-each> 
       </title> 

       <!-- add more child elements here --> 

      </xsl:copy> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

Résultat

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <book id="bk101"> 
    <author>Gambardella, Matthew &amp; Corets, Eva &amp; Galos, Mike</author> 
    <title>XML Developer's Guide &amp; Oberon's Legacy &amp; Visual Studio 7: A Comprehensive Guide</title> 
    </book> 
    <book id="bk102"> 
    <author>Ralls, Kim &amp; Corets, Eva</author> 
    <title>Midnight Rain &amp; The Sundered Grail</title> 
    </book> 
    <book id="bk103"> 
    <author>Corets, Eva</author> 
    <title>Maeve Ascendant</title> 
    </book> 
</catalog> 

Pour éliminer la répétition de code pour chaque propriété enfant d'un livre, vous pouvez définir une autre clé et l'utiliser comme suit:

XSLT 1,0

<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:strip-space elements="*"/> 

<xsl:key name="Dbook" match="book" use="@id"/> 
<xsl:key name="book-property" match="book/*" use="concat(../@id, '|', name())"/> 

<xsl:variable name="separator" select="' &amp; '" /> 

<xsl:template match="/catalog"> 
    <xsl:copy> 
     <xsl:for-each select="book[count(. | key('Dbook', @id)[1])=1]"> 
      <xsl:copy> 
       <xsl:copy-of select="@*"/> 
       <xsl:for-each select="*"> 
        <xsl:copy> 
         <xsl:for-each select="key('book-property', concat(../@id, '|', name()))"> 
          <xsl:value-of select="."/> 
          <xsl:if test="position()!=last()"> 
           <xsl:value-of select="$separator"/> 
          </xsl:if> 
         </xsl:for-each> 
        </xsl:copy> 
       </xsl:for-each>    
      </xsl:copy> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

Résultat

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <book id="bk101"> 
    <author>Gambardella, Matthew &amp; Corets, Eva &amp; Galos, Mike</author> 
    <title>XML Developer's Guide &amp; Oberon's Legacy &amp; Visual Studio 7: A Comprehensive Guide</title> 
    <genre>Computer &amp; Fantasy &amp; Computer</genre> 
    <price>44.95 &amp; 5.95 &amp; 49.95</price> 
    <publish_date>2000-10-01 &amp; 2001-03-10 &amp; 2001-04-16</publish_date> 
    <description>An in-depth look at creating applications 
     with XML. &amp; In post-apocalypse England, the mysterious 
     agent known only as Oberon helps to create a new life 
     for the inhabitants of London. Sequel to Maeve 
     Ascendant. &amp; Microsoft Visual Studio 7 is explored in depth, 
     looking at how Visual Basic, Visual C++, C#, and ASP+ are 
     integrated into a comprehensive development 
     environment.</description> 
    </book> 
    <book id="bk102"> 
    <author>Ralls, Kim &amp; Corets, Eva</author> 
    <title>Midnight Rain &amp; The Sundered Grail</title> 
    <genre>Fantasy &amp; Fantasy</genre> 
    <price>5.95 &amp; 5.95</price> 
    <publish_date>2000-12-16 &amp; 2001-09-10</publish_date> 
    <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world. &amp; The two daughters of Maeve, half-sisters, 
     battle one another for control of England. Sequel to 
     Oberon's Legacy.</description> 
    </book> 
    <book id="bk103"> 
    <author>Corets, Eva</author> 
    <title>Maeve Ascendant</title> 
    <genre>Fantasy</genre> 
    <price>5.95</price> 
    <publish_date>2000-11-17</publish_date> 
    <description>After the collapse of a nanotechnology 
     society in England, the young survivors lay the 
     foundation for a new society.</description> 
    </book> 
</catalog>