2011-06-29 42 views
1

我發現在應用具有xml輸出的模式時如何佈局XSL非常困難。XSLT初學者問題

目前,我有C#代碼運行:

  static void Main(string[] args) 
      { 
     XslCompiledTransform myXslTransform; 
     myXslTransform = new XslCompiledTransform(); 
     myXslTransform.Load("testxls.xsl"); 
     myXslTransform.Transform("1BFC.xml", "testoutput.xml"); 
     //Console.ReadLine(); 
        } 

而且我X​​LS看起來像

 <?xml version="1.0" encoding="windows-1252"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output indent="yes" method="xml" /> 
    <xsl:template match="/"> 
    <xsl:apply-templates /> 
    </xsl:template> 

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

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

    <xsl:template match="GlobalParam"> 
    <GlobalParam> 
    <xsl:attribute name="name"> 
     <xsl:value-of select="@name" /> 
    </xsl:attribute> 
    <xsl:value-of select="."/> 
    </GlobalParam> 
    <GlobalParam> 
    <xsl:attribute name="value"> 
     <xsl:value-of select="@value" /> 
    </xsl:attribute> 
    <xsl:value-of select="."/> 
    </GlobalParam> 
    </xsl:template> 

哪些工作,但不產生輸出,我想:輸出的樣子:

  <FileImport> 
      <Global> 
     <GlobalParam name="RollName"></GlobalParam><GlobalParam value="SA2 10:00:00:00"></GlobalParam> 
     <GlobalParam name="TapeOrg"></GlobalParam><GlobalParam value="10:00:00:00">     </GlobalParam> 
     <GlobalParam name="ReadStart"></GlobalParam><GlobalParam value="00:00:00:00"> </GlobalParam> 
     <GlobalParam name="ReadDuration"></GlobalParam><GlobalParam value="00:02:26:18"></GlobalParam> 

我想是所有包含在全球標籤內,所以我想它看起來像:

 <Global> 
     <GlobalParam name="RollName" value="SA2" /> 
     <GlobalParam name="TapeOrg" value="10:00:00:00" /> 
     <GlobalParam name="ReadStart" value="00:00:00:00" /> 
     <GlobalParam name="ReadDuration" value="00:00:21:07" /> 
     </Global> 

似乎無法找到講解XML 2 XML XSL的任何信息。我不能把它包含它的權利..感謝您的任何幫助或指針。

+0

你能編輯的問題,並添加一個示例XML源文件好嗎?也許你在源代碼中使用的'1BFC.xml'? – andyb

+0

如果你想要一個'GlobalParam'實例下的屬性,你爲什麼要在你的變換中創建兩個單獨的元素實例? –

回答

1

你的XSLT應該是這樣的:

<?xml version="1.0" encoding="windows-1252"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output indent="yes" method="xml" /> 

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

    <xsl:template match="GlobalParam"> 
     <GlobalParam> 
      <xsl:attribute name="name"> 
       <xsl:value-of select="@name" /> 
      </xsl:attribute> 
      <xsl:attribute name="value"> 
       <xsl:value-of select="@value" /> 
      </xsl:attribute> 
     </GlobalParam> 
    </xsl:template> 
</xsl:stylesheet> 
0

從value屬性中刪除<GlobalParam>標籤。

0

乍一看,它看起來像有一對夫婦的問題在這裏。我相信你需要在全局模板中應用GlobalParam模板(使用xsl:apply-templates)。在GlobalParam模板中,您需要擺脫中間部分,關閉並重新打開GlobalParam標籤,以便將名稱和值包含在單個元素中。

0

我想你想:

<xsl:template match="GlobalParam"> 
<xsl:copy> 
    <xsl:copy-of select="@name|@value"/> 
</xsl:copy> 
</xsl:template> 
2

或者,如果你願意的話,

<xsl:template match="GlobalParam"> 
    <GlobalParam name="{@name}" value="{@value}"/> 
</xsl:template>