2017-04-10 9 views
0

xmlを別のxml形式に変換する必要があります。ここで、すべてのセクションと章のIDを親要素のジャーナルコンテンツと順番に並べる必要があります。私はのようにユニークなIDがXSLTを使用して要素のIDを挿入します

の下に言及した出力を期待してい

<?xml version="1.0" encoding="UTF-8"?><book> 
    <journalcontent id="001"><section>Fir</section></journalcontent> 
    <journalcontent id="001"><chapter>sec</chapter></journalcontent> 
    <journalcontent id="002"><section>thir</section></journalcontent> 
    <journalcontent id="002"><chapter>four</chapter></journalcontent> 
    <journalcontent id="003"><section>ff</section></journalcontent> 
    <journalcontent id="003"><chapter>66</chapter></journalcontent> 
</book> 

以下の出力を得た

<?xml version="1.0" encoding="UTF-8"?> 
<book> 
    <footnote> 
<journal> 
    <section>Fir</section> sum <chapter>sec</chapter> 
    </journal> 
</footnote> 

<footnote> 
    <journal> 
     <section>thir</section> sum <chapter>four</chapter> 
    </journal> 
</footnote> 

<footnote> 
    <journal> 
     <section>ff</section> sum <chapter>66</chapter> 
    </journal> 
    </footnote> 
</book> 

は、私は、XSLTを下回ることで試みたが、出力に含まは

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:template match="book"> 
     <book> 
      <xsl:apply-templates/> 
     </book> 
    </xsl:template> 
    <xsl:template match="//footnote"> 
      <xsl:variable name="seq"> 
      <xsl:number format="001" level="any"/> 
     </xsl:variable> 
     <xsl:for-each select="journal"> 
      <xsl:if test="section"> 
       <journalcontent> 
        <xsl:attribute name="id"> 
         <xsl:value-of select="$seq"/> 
        </xsl:attribute> 
        <xsl:copy-of select="section"/> 
       </journalcontent> 
      </xsl:if> 
      <xsl:if test="chapter"> 
       <journalcontent> 
        <xsl:attribute name="id"> 
         <xsl:value-of select="$seq"/> 
        </xsl:attribute> 
        <xsl:copy-of select="chapter"/> 
       </journalcontent> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

corectではありません

<?xml version="1.0" encoding="UTF-8"?> 
    <book> 
     <journalcontent id="001"><section>Fir</section></journalcontent> 
     <journalcontent id="002"><chapter>sec</chapter></journalcontent> 
     <journalcontent id="003"><section>thir</section></journalcontent> 
     <journalcontent id="004"><chapter>four</chapter></journalcontent> 
     <journalcontent id="005"><section>ff</section></journalcontent> 
    <journalcontent id="006"><chapter>66</chapter></journalcontent> 
    </book> 

誰でも手伝ってください私

答えて

0

あなたがjournal要素でsectionchapterに番号をしようとしているとして、あなたはjournal要素に合わせてテンプレートを変更した場合、それは良いかもしれない唯一の3があり、そのうちのfootnote要素に番号を基づかています、およびその後の任意のレベル

section又は chapter要素の数を数え、シーケンス番号を取得するために、そしてこの

<xsl:template match="journal"> 
    <xsl:for-each select="section|chapter"> 

chapter又はsection要素を選択

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:strip-space elements="*" /> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="book"> 
     <book> 
      <xsl:apply-templates/> 
     </book> 
    </xsl:template> 

    <xsl:template match="journal"> 
     <xsl:for-each select="section|chapter"> 
      <journalcontent> 
       <xsl:attribute name="id"> 
        <xsl:number format="001" count="section|chapter" level="any"/> 
       </xsl:attribute> 
       <xsl:copy-of select="."/> 
      </journalcontent> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 
このXSLTをお試しください
関連する問題