2011-06-29 46 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>