2016-12-28 96 views
0

雖然我將輸入DITA文件轉換爲XML輸出,但將單個父標記的<p class="- topic/p " outputclass="MLU_Intro">等多個相同元素轉換爲多個相同標記。使用XSLT創建DITA到XML輸出將多個相同元素置於子元素的父元素下

我是XSLT新手,請任何人指導我解決上述問題。

我輸入XML文件是:

<!DOCTYPE topic PUBLIC "urn:pubid:com.staywell.doctypes:doctypes:dita:topic" "topic.dtd"> 
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:r="http://www.rsuitecms.com/rsuite/ns/metadata" class="- topic/topic " ditaarch:DITAArchVersion="1.2" domains="(topic hi-d) (topic indexing-d)(topic d4p_formatting-d)a(props d4p_renditionTarget)(topic d4p_math-d)(topic d4p_variables-d)(topic d4p_verse-d)(topic learningInteractionBase2-d learning2-d+learning-d)(topic learningBase+learningInteractionBase-d)(topic learningInteractionBase-d)(topic learningInteractionBase2-d)(topic xml-d)a(base rsuiteIdAtt)(topic sdClassification-d) " id="d43e3" outputclass="DOCTYPE-MLU" r:rsuiteId="6514" xml:lang="en-US"> 
    <title class="- topic/title " outputclass="MLU">All About the A1C</title> 
    <body class="- topic/body "> 
    <p class="- topic/p " outputclass="MLU_Code">TP_02A</p> 
    <p class="- topic/p " outputclass="MLU_Type">Horizontal Click &amp; Learn</p> 
    </body> 
    <topic class="- topic/topic " ditaarch:DITAArchVersion="1.2" domains="(topic hi-d) (topic indexing-d)(topic d4p_formatting-d)a(props d4p_renditionTarget)(topic d4p_math-d)(topic d4p_variables-d)(topic d4p_verse-d)(topic learningInteractionBase2-d learning2-d+learning-d)(topic learningBase+learningInteractionBase-d)(topic learningInteractionBase-d)(topic learningInteractionBase2-d)(topic xml-d)a(base rsuiteIdAtt)(topic sdClassification-d) " id="d43e15" outputclass="TOPIC-MLU-Header" r:rsuiteId="6515" xml:lang="en-US"> 
    <title class="- topic/title " outputclass="MLU-Header">Header</title> 
    <body class="- topic/body "> 
     <p class="- topic/p " outputclass="MLU_Category">Management</p> 
     <p class="- topic/p " outputclass="MLU_Banner_Display">Yes</p> 
     ** 
     <p class="- topic/p " outputclass="MLU_Intro">It’s great that.</p> 
     <p class="- topic/p " outputclass="MLU_Intro"> 
     <ph class="- topic/ph " outputclass="Bold">Why the A1C</ph> 
     </p> 
     <p class="- topic/p " outputclass="MLU_Intro">An A1C test.</p> 
     <p class="- topic/p " outputclass="MLU_Intro"> 
     <ph class="- topic/ph " outputclass="Bold">What to</ph> 
     <ph class="- topic/ph " outputclass="Bold">Results</ph> 
     </p> 
     <p class="- topic/p " outputclass="MLU_Intro">An A1C test result.</p> 
     ** 
     <p class="- topic/p " outputclass="MLU_Directions">CLICK ON EACH ICON TO LEARN MORE.</p> 
    </body> 
    </topic> 
</topic> 

XSL我所用:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" encoding="UTF-8" doctype-system="../DTDs/MLU.dtd" indent="yes" /> 
    <xsl:strip-space elements="*" /> 

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

    <xsl:template match="topic[@outputclass = 'DOCTYPE-MLU']"> 
    <MLU> 
     <clickLearnHorizontal> 
     <xsl:apply-templates /> 
     </clickLearnHorizontal> 
    </MLU> 
    </xsl:template> 

    <xsl:template match="topic[@outputclass = 'TOPIC-MLU-Header']"> 
    <header> 
     <trackingSettings> 
     <urlcode> 
      <xsl:value-of select="//p[@outputclass = 'MLU_Code']" /> 
     </urlcode> 
     <apiurl>http://mlucenter.com/like/api</apiurl> 
     </trackingSettings> 
     <page> 
     <xsl:value-of select="text()" /> 
     </page> 
     <banner> 
     <enabled>true</enabled> 
     <text> 
      <xsl:value-of select="//title[@outputclass = 'MLU']" /> 
     </text> 
     </banner> 
     <xsl:apply-templates /> 
    </header> 
    </xsl:template> 

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

    <xsl:template match="p[@outputclass = 'MLU_Code']" /> 
    <xsl:template match="title[@outputclass = 'MLU']" /> 
    <xsl:template match="title[@outputclass = 'MLU-Header']" /> 
    <xsl:template match="p[@outputclass = 'MLU_Condition']" /> 
    <xsl:template match="p[@outputclass = 'MLU_Type']" /> 
    <xsl:template match="p[@outputclass = 'MLU_Category']" /> 
    <xsl:template match="p[@outputclass = 'MLU_Banner_Display']" /> 
    <xsl:template match="p[@outputclass = 'MLU_Banner_Color']" /> 

    <xsl:template match="p"> 
    <introText> 
     <text> 
     <xsl:for-each select="p[@outputclass = 'MLU_Intro']"> 
      <p> 
      <xsl:apply-templates /> 
      </p> 
     </xsl:for-each> 
     </text> 
    </introText> 
    </xsl:template> 

    <!--<xsl:template match="p[@outputclass = 'MLU_Intro']"> </xsl:template>--> 

    <xsl:template match="p[@outputclass = 'MLU_Directions']"> 
    <directionText> 
     <text> 
     <xsl:value-of select="//p[@outputclass = 'MLU_Directions']" /> 
     </text> 
    </directionText> 
    </xsl:template> 

    <xsl:template match="title[@outputclass = 'MLU-Body']" /> 
    <xsl:template match="title" /> 
    <xsl:template match="ph"> 
    <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="p[@outputclass = 'MLU_Item_Button']" /> 
    <xsl:template match="p[@outputclass = 'MLU_Item_Title_Position']" /> 
    <xsl:template match="bodydiv[@outputclass = 'Item_Defaults']" /> 

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

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

    <!-- 
    <xsl:template match="p[@outputclass = 'MLU_Intro']"> 
    <p> 
    <xsl:apply-templates/> 
    </p> 
    </xsl:template> 
    --> 

    <xsl:template match="image" /> 
    <xsl:template match="@MLU_Banner_Display" /> 
    <xsl:template match="@class"> 
    <!--<xsl:apply-templates/>--> 
    </xsl:template> 

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

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

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

    <xsl:template match="ph[@outputclass = 'Bold']"> 
    <strong> 
     <xsl:apply-templates /> 
    </strong> 
    </xsl:template> 
</xsl:stylesheet> 

輸出XML,我得到如下:

<!DOCTYPE MLU 
    SYSTEM "../DTDs/MLU.dtd"> 
<MLU> 
    <clickLearnHorizontal> 
    <header> 
     <trackingSettings> 
     <urlcode>TP_02A</urlcode> 
     <apiurl>http://mlucenter.com/like/api</apiurl> 
     </trackingSettings> 
     <page /> 
     <banner> 
     <enabled>true</enabled> 
     <text>All About the A1C</text> 
     </banner> 
     ** 
     <p>It’s great.</p> 
     <p> 
     <strong>Why the A1C</strong> 
     </p> 
     <p>An A1C test.</p> 
     <p> 
     <strong>What to Expect</strong> 
     <strong>Results</strong> 
     </p> 
     <p>An A1C test.</p> 
     ** 
     <directionText> 
     <text>CLICK ON EACH ICON TO LEARN MORE.</text> 
     </directionText> 
    </header> 
    </clickLearnHorizontal> 
</MLU> 

但是我預期的輸出爲:

<MLU> 
    <clickLearnHorizontal> 
    <header> 
     <trackingSettings> 
     <urlcode>TP_02A</urlcode> 
     <apiurl>http://mlucenter.com/like/api</apiurl> 
     </trackingSettings> 
     <page /> 
     <banner> 
     <enabled>true</enabled> 
     <text>All About the A1C</text> 
     </banner> 
     ** 
     <introText> 
     <text> 
      <p>It’s great.</p> 
      <p> 
      <strong>Why the A1C</strong> 
      </p> 
      <p>An A1C test.</p> 
      <p> 
      <strong>What to Expect</strong> 
      <strong>Results</strong> 
      </p> 
      <p>An A1C test.</p> 
     </text> 
     </introText> 
     ** 
     <directionText> 
     <text>CLICK ON EACH ICON TO LEARN MORE.</text> 
     </directionText> 
    </header> 
    </clickLearnHorizontal> 
</MLU> 
+0

如果您收到更具體的東西一個可以接受的答案,接受它會很有禮貌。 – zx485

回答

0

您可以使用for-each-group group-adjacent如下:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" encoding="UTF-8" doctype-system="../DTDs/MLU.dtd" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="topic[@outputclass = 'DOCTYPE-MLU']"> 
     <MLU> 
      <clickLearnHorizontal> 
       <xsl:apply-templates/> 
      </clickLearnHorizontal> 
     </MLU> 
    </xsl:template> 
    <xsl:template match="topic[@outputclass = 'TOPIC-MLU-Header']"> 
     <header> 
      <trackingSettings> 
       <urlcode> 
        <xsl:value-of select="//p[@outputclass = 'MLU_Code']"/> 
       </urlcode> 
       <apiurl>http://mlucenter.com/like/api</apiurl> 
      </trackingSettings> 
      <page><xsl:value-of select="text()" /></page> 
      <banner> 
       <enabled>true</enabled> 
       <text> 
        <xsl:value-of select="//title[@outputclass = 'MLU']"/> 
       </text> 
      </banner> 
      <xsl:apply-templates/> 
     </header> 
    </xsl:template> 
    <xsl:template match="body"> 
     <xsl:for-each-group select="*" group-adjacent="boolean(self::p[@outputclass = 'MLU_Intro'])"> 
      <xsl:choose> 
       <xsl:when test="current-grouping-key()"> 
        <introText> 
         <text> 
          <xsl:apply-templates select="current-group()"/> 
         </text> 
        </introText> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:apply-templates select="current-group()"/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each-group> 
    </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Code']"> </xsl:template> 
    <xsl:template match="title[@outputclass = 'MLU']"> </xsl:template> 
    <xsl:template match="title[@outputclass = 'MLU-Header']"> </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Condition']"> </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Type']"> </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Category']"> </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Banner_Display']"> </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Banner_Color']"> </xsl:template> 

    <xsl:template match="p[@outputclass = 'MLU_Intro']"> 
     <p><xsl:apply-templates/></p> 
    </xsl:template> 

    <xsl:template match="p[@outputclass = 'MLU_Directions']"> 
     <directionText> 
      <text> 
       <xsl:value-of select="//p[@outputclass = 'MLU_Directions']"/> 
      </text> 
     </directionText> 
    </xsl:template> 
    <xsl:template match="title[@outputclass = 'MLU-Body']"> </xsl:template> 
    <xsl:template match="title"> </xsl:template> 
    <xsl:template match="ph"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Item_Button']"> </xsl:template> 
    <xsl:template match="p[@outputclass = 'MLU_Item_Title_Position']"> </xsl:template> 
    <xsl:template match="bodydiv[@outputclass = 'Item_Defaults']"> </xsl:template> 
    <xsl:template match="bodydiv"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="p"> 
     <p> 
      <xsl:apply-templates/> 
     </p> 
    </xsl:template> 

    <xsl:template match="image"></xsl:template> 
    <xsl:template match="@MLU_Banner_Display"> </xsl:template> 
    <xsl:template match="@class"> 
     <!--<xsl:apply-templates/>--> 
    </xsl:template> 
    <xsl:template match="ul"> 
     <ul> 
      <xsl:apply-templates/> 
     </ul> 
    </xsl:template> 
    <xsl:template match="ol"> 
     <ol> 
      <xsl:apply-templates/> 
     </ol> 
    </xsl:template> 
    <xsl:template match="li"> 
     <li> 
      <xsl:apply-templates/> 
     </li> 
    </xsl:template> 
    <xsl:template match="ph[@outputclass = 'Bold']"> 
     <strong> 
      <xsl:apply-templates/> 
     </strong> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感謝你給我的幫助,所以我可以去更進一步的任務 –

+0

@VijayanKaruppaiah,如果你的問題解決了,那麼請參閱http://meta.stackexchange.com/questions/5234/how-does-accepting-an-回答如何接受答案以表明問題已解決。 ! –

0

您目前擁有兩個模板match="p"。第二個正在使用。

在XSLT中,就像在CSS中一樣,如果兩個規則可以匹配給定節點,則最後定義的規則將勝出。

的措施來解決:

  • 無論是刪除第二個模板,或
  • 重新安排你的模板,或者
  • 讓他們中的一個匹配比p
+0

< - 的 <的xsl:for-每個選擇= 「P [@outputclass = 'MLU_Intro']」>

- > –

+0

我不明白,評論。 – Tomalak

+0

對不起,這裏貼錯了 –

相關問題