2010-11-08 7 views
0

J'ai besoin d'aide pour trier un fichier XML basé sur des nœuds xml et encore une chose est que je dois placer leurs balises de commentaire correspondant à ce nœud.Trier un fichier XML basé sur des nœuds XML et des commentaires XML en utilisant XSLT

Par exemple: fichier XML d'entrée est

<?xml version="1.0" encoding="UTF-8"?> 
<!-- catalog main --> 
<catalog> 
<!-- first book id --> 
<book id="bk101"> 
    <!-- author name 1 --> 
    <author>Gambardella, Matthew</author> 
    <!-- title name 1 --> 
    <title>XML Developer's Guide</title> 
    <genre>Computer</genre> 
    <!-- price 1 --> 
    <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> 


<!-- languages --> 
<Languages> 
    <!-- java comment --> 
    <java>Galos, Mike</java> 
    <c>Visual Studio 7: A Comprehensive Guide</c> 
    <!-- dotnet comment --> 
    <dotnet>Computer</dotnet> 
    <!-- description --> 
    <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> 
</Languages> 

<!-- ABC book --> 
<ABC> 
    <!-- ABC author --> 
    <author1>Galos, Mike</author1> 
    <title1>Visual Studio 7: A Comprehensive Guide</title1> 
    <!-- ABC genre --> 
    <genre1>Computer</genre1> 
    <price1>49.95</price1> 
    <publish_date>2001-04-16</publish_date> 
    <!-- ABC description --> 
    <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> 
</ABC> 
<!-- ABC1 book --> 
<abc1> 
    <!-- ABC1 author --> 
    <author1>Galos, Mike</author1> 
    <title1>Visual Studio 7: A Comprehensive Guide</title1> 
    <!-- ABC1 genre --> 
    <genre1>Computer</genre1> 
    <price1>49.95</price1> 
    <publish_date>2001-04-16</publish_date> 
    <!-- ABC1 description --> 
    <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> 
</abc1> 
</catalog> 

XML ATTENDUS DE SORTIE FICHIER DOIT ÊTRE:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- catalog main --> 
<catalog> 
<!-- ABC book --> 
<ABC> 
    <!-- ABC author --> 
    <author1>Galos, Mike</author1> 
    <!-- ABC description --> 
    <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> 
    <!-- ABC genre --> 
    <genre1>Computer</genre1> 
    <price1>49.95</price1> 
    <publish_date>2001-04-16</publish_date> 
    <title1>Visual Studio 7: A Comprehensive Guide</title1> 
</ABC> 
<!-- ABC1 book --> 
<abc1> 
    <!-- ABC1 author --> 
    <author1>Galos, Mike</author1> 
    <!-- ABC1 description --> 
    <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> 
    <!-- ABC1 genre --> 
    <genre1>Computer</genre1> 
    <price1>49.95</price1> 
    <publish_date>2001-04-16</publish_date> 
    <title1>Visual Studio 7: A Comprehensive Guide</title1> 
</abc1> 
<!-- first book id --> 
<book id="bk101"> 
    <!-- author name 1 --> 
    <author>Gambardella, Matthew</author> 
    <description>An in-depth look at creating applications 
     with XML.</description> 
    <genre>Computer</genre> 
    <!-- price 1 --> 
    <price>44.95</price> 
    <publish_date>2000-10-01</publish_date> 
    <!-- title name 1 --> 
    <title>XML Developer's Guide</title> 
</book> 
<Book id="bk102"> 
    <author>Ralls, Kim</author> 
    <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world.</description> 
    <genre>Fantasy</genre> 
    <price>5.95</price> 
    <publish_date>2000-12-16</publish_date> 
    <title>Midnight Rain</title> 
</Book> 
<!-- languages --> 
<Languages> 
    <c>Visual Studio 7: A Comprehensive Guide</c> 
    <!-- description --> 
    <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> 
    <!-- dotnet comment --> 
    <dotnet>Computer</dotnet> 
    <!-- java comment --> 
    <java>Galos, Mike</java> 
</Languages> 
</catalog> 
+0

aide en utilisant le code XSLT –

Répondre

0

Il semble que je l'ai résolu ce problème avec une mise en œuvre plus ..
est en dessous du nouveau code: -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="*|@*"> 
     <xsl:copy-of select="preceding-sibling::node()[1]/self::comment()"/> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:apply-templates select="node()"> 
       <xsl:sort select="name()"/> 
       <xsl:sort select="@*"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

+0

Quelqu'un peut-il suggérer dans le code ci-dessus en ajoutant une nouvelle ligne de code qui fait le tri avec le nom d'attribut? –

+0

l'ordre de préférence de tri est basé sur 1. noeud 2. nom de l'attribut 3. valeur d'attribut –

2

Cette feuille de style:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="*|@*"> 
     <xsl:copy-of select="preceding-sibling::node()[1]/self::comment()"/> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:apply-templates select="node()"> 
       <xsl:sort select="name()"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

Sortie:

<!-- catalog main --> 
<catalog> 
    <!-- ABC book --> 
    <ABC> 
     <!-- ABC author --> 
     <author1>Galos, Mike</author1> 
     <!-- ABC description --> 
     <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> 
     <!-- ABC genre --> 
     <genre1>Computer</genre1> 
     <price1>49.95</price1> 
     <publish_date>2001-04-16</publish_date> 
     <title1>Visual Studio 7: A Comprehensive Guide</title1> 
    </ABC> 
    <!-- ABC1 book --> 
    <abc1> 
     <!-- ABC1 author --> 
     <author1>Galos, Mike</author1> 
     <!-- ABC1 description --> 
     <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> 
     <!-- ABC1 genre --> 
     <genre1>Computer</genre1> 
     <price1>49.95</price1> 
     <publish_date>2001-04-16</publish_date> 
     <title1>Visual Studio 7: A Comprehensive Guide</title1> 
    </abc1> 
    <!-- first book id --> 
    <book id="bk101"> 
     <!-- author name 1 --> 
     <author>Gambardella, Matthew</author> 
     <description>An in-depth look at creating applications 
     with XML.</description> 
     <genre>Computer</genre> 
     <!-- price 1 --> 
     <price>44.95</price> 
     <publish_date>2000-10-01</publish_date> 
     <!-- title name 1 --> 
     <title>XML Developer's Guide</title> 
    </book> 
    <Book id="bk102"> 
     <author>Ralls, Kim</author> 
     <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world.</description> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-12-16</publish_date> 
     <title>Midnight Rain</title> 
    </Book> 
    <!-- languages --> 
    <Languages> 
     <c>Visual Studio 7: A Comprehensive Guide</c> 
     <!-- description --> 
     <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> 
     <!-- dotnet comment --> 
     <dotnet>Computer</dotnet> 
     <!-- java comment --> 
     <java>Galos, Mike</java> 
    </Languages> 
</catalog> 
+0

+1 pour une bonne réponse. –

Questions connexes