2016-11-09 1 views
1

同じタイプの入れ子になった要素が多数あるXMLファイルがあります。私は名前属性値でそれらを並べ替えることができるようにしたい。XSLスタイルシートを使用した名前属性による並べ替え

<configurations> 
    <configuration xmlns="http://locomotive/bypass/docx" name="managed"> 
     <configuration name="tracking-component" class="singular"> 
      <configuration name="data-component"/> 
      <configuration name="modal"/> 
      <configuration name="node"/> 
     </configuration> 
     <configuration name="network-component"> 
      <configuration name="disks"/> 
      <configuration name="cycles"/> 
     </configuration> 
    </configuration> 
</configurations> 

私はこのスタイルシートを試してみましたが、それは何も出力しませんでした:

ここでの目標は、次のようにソートする上記のファイルを持っているファイル

<configurations> 
    <configuration xmlns="http://locomotive/bypass/docx" name="managed"> 
     <configuration name="tracking-component"> 
      <configuration name="disks"/> 
      <configuration name="cycles"/> 
     </configuration> 
     <configuration name="network-component" class="singular"> 
      <configuration name="data-component"/> 
      <configuration name="node"/> 
      <configuration name="modal"/> 
     </configuration> 
    </configuration> 
</configurations> 

の一例ですここで

<xsl:stylesheet version="1.0">  
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

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

    <xsl:template match="//*[local-name() = 'Configuration']"> 
     <xsl:copy> 
      <xsl:apply-templates select="Configuration/@name"> 
       <xsl:sort select="."/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

は私が作るしようとしているものです:

<configurations> 
    <configuration xmlns="http://locomotive/bypass/docx" name="managed"> 
     <configuration name="network" class="singular"> 
      <configuration name="data-component"/>  
      <configuration name="modal"/> 
      <configuration name="node"/> 
     </configuration> 
     <configuration name="tracking-component"> 
      <configuration name="cycles"/> 
      <configuration name="disks"/> 
     </configuration> 
    </configuration> 
</configurations> 

network-component属性が、右のアルファベット順にtracking-component属性の前に表示されるようになりました。 network-componenttracking-componentの構成属性もアルファベット順にソートされています。

助けてください。

+0

私は処理命令 ''を追加することでXMLセクションの字下げを改善し、XSLTセクションを墨塗りしました。 – zx485

答えて

0

このようにそれを試してみてください。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns1="http://locomotive/bypass/docx" > 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="ns1:configuration"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates select="ns1:configuration"> 
      <xsl:sort select="@name"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

  1. XMLは大文字と小文字が区別される:Configurationは等しくないを行います;

  2. 名前空間の要素を処理する適切な方法は、ローカル名のみを使用して名前空間を無視するのではなく、次のように接頭辞を使用することです。

  3. 結果はあなたの質問に示されている結果とは異なりますが、それでも正しいと思います。

+0

ありがとうございます。内容は印刷されますが、名前でソートされることはありません。 – BreenDeen

+0

問題は、親構成要素を含む外部構成が内部の構成要素と一緒に書き込まれ、結果としてフラットな構造になることです。内部要素もソートされていますが、ソートは失われます。親要素も同様に記述されているからです。 – BreenDeen

+0

@BreenDeenいいえ、それはどうなるでしょうか。あなたはそれがここで働いているのを見ることができます:http://xsltransform.net/ejivdHb –

関連する問題