XSLT初学者问题

时间:2011-06-29 13:46:40

标签: c# xml xslt

我发现在应用带有xml输出的模式时,很难找到有关如何布局XSL的信息。

目前我有c#代码要运行:

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

我的xls看起来像

     <?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的任何信息。我一定不能正确地包含它..感谢任何帮助或指示。

5 个答案:

答案 0 :(得分:2)

或者,如果您愿意,

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

答案 1 :(得分: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>

答案 2 :(得分:0)

从值属性中删除<GlobalParam>标记。

答案 3 :(得分:0)

乍一看,这里看起来有几个问题。我相信您需要在Global模板中应用GlobalParam模板(使用xsl:apply-templates)。在GlobalParam模板中,您需要删除关闭的中间部分并重新打开GlobalParam标记,以便将名称和值包含在单个元素中。

答案 4 :(得分:0)

我想你想要:

<xsl:template match="GlobalParam">
 <xsl:copy>
  <xsl:copy-of select="@name|@value"/>
 </xsl:copy>
</xsl:template>
相关问题