开发者

XSLT Beginner question

I am finding it quite hard to find information on how to layout XSL when applying a schema with an xml output.

Currently I have c# code to run:

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

And my xls looks like

     <?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="GlobalPara开发者_运维问答m">
   <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>

Which works but does not produce the output I would like: the output looks like:

          <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>

What I would like is all contained within the global tag so I would like it to look like:

        <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>

Just can't seem to find any info on explaining xml 2 xml XSL. I must not be containing it right.. Thanks for any help or pointers.


Or if you prefer,

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


Your XSLT should look like this:

<?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>


Remove the <GlobalParam> tags from the value attribute.


At first glance, it looks like there's a couple of issues here. I believe you need to apply the GlobalParam template within the Global template (using xsl:apply-templates). In the GlobalParam template you need to get rid of the middle section where you close and reopen the GlobalParam tag in order to have the name and value contained within a single element.


I think you want:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜