用于xls转换的Excel VBA编码以及参数

时间:2014-01-29 12:21:46

标签: xml vba xslt excel-vba xslt-2.0

我需要使用VBA代码解析xsl及其参数。 我可以使用以下链接中的VBA代码(供您参考),但我唯一需要通过VBA代码传递XSLT参数。

VBA code : Hperlink

我的(ds_test.xsl)档案

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:param name="job" />
<xsl:param name="src" />
<xsl:param name="spath" />
<xsl:template match="/">
  <DSExport>
     <Job>
        <xsl:attribute name="Identifier">
           <xsl:value-of select="$job" />
        </xsl:attribute>
        <Record Identifier="V25S0P1" Type="CustomOutput" Readonly="0">
           <Collection Name="Columns" Type="OutputColumn">
              <xsl:copy-of select="document($src)//Record[@Identifier=$spath]//SubRecord" />
           </Collection>
        </Record>
     </Job>
  </DSExport>
</xsl:template>
</xsl:stylesheet> 

我的输入(Metadata.xml)文件

<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<Header CharacterSet="CP1252" ExportingTool="IBM InfoSphere DataStage Export" ToolVersion="8" ServerName="HCL-BOEING-DS" ToolInstanceID="EFBI_BAL_OPT" Date="2014-01-21" Time="19.09.04" ServerVersion="9.1" />
<TableDefinitions>
  <Record Identifier="TEST1" DateModified="2013-12-23" TimeModified="11.01.03" Type="MetaTable" Readonly="0">
     <Collection Name="Columns" Type="MetaColumn">
        <SubRecord>
           <Property Name="Name">BEMSID</Property>
           <Property Name="Description">BEMSID: string[max=10]</Property>
           <Property Name="SqlType">12</Property>
           <Property Name="Precision">10</Property>
        </SubRecord>
        <SubRecord>
           <Property Name="Name">EMPL_NM</Property>
           <Property Name="Description">EMPL_NM: string[max=18]</Property>
           <Property Name="SqlType">12</Property>
           <Property Name="Precision">18</Property>
        </SubRecord>
     </Collection>
  </Record>
</TableDefinitions>
</DSExport>

我的预期输出格式为XML(output.xml)

注意:这是我的问题 - 在运行时我应该通过Excel VBA代码传递XSL参数的值,如前所述。我们假设我给出了以下参数值

VBA code : Hperlink

  1. $ job =“PXJ_TEST1”
  2. $ src =“Metadata.xml”
  3. $ spath =“TEST1”
  4. output.xml 应采用以下格式

    <?xml version="1.0" encoding="UTF-8"?>
    <DSExport>
    <Job Identifier="PXJ_TEST1">
        <Record Identifier="V25S0P1" Type="CustomOutput" Readonly="0">
            <Collection Name="Columns" Type="OutputColumn">
                <SubRecord>
                    <Property Name="Name">BEMSID</Property>
                    <Property Name="Description">BEMSID: string[max=10]</Property>
                    <Property Name="SqlType">12</Property>
                    <Property Name="Precision">10</Property>
                </SubRecord>
                <SubRecord>
                    <Property Name="Name">EMPL_NM</Property>
                    <Property Name="Description">EMPL_NM: string[max=18]</Property>
                    <Property Name="SqlType">12</Property>
                    <Property Name="Precision">18</Property>
                </SubRecord>
            </Collection>
        </Record>
    </Job>
    </DSExport>
    

2 个答案:

答案 0 :(得分:0)

如果你需要设置参数,那么使用transformNode的简单方法不起作用,你需要使用http://msdn.microsoft.com/en-us/library/ms762799%28v=vs.85%29.aspx中提供的API(该页面只有JScript和C ++,但当然你可以使用相同的API和带有VBScript或VBA的对象)。然后,您可以使用http://msdn.microsoft.com/en-us/library/ms762312%28v=vs.85%29.aspx addParameter方法设置参数。

答案 1 :(得分:0)

对于将来登陆此页面的读者,以下是使用OP输入将参数从VBA传递到XSLT的具体实现:

Sub XSLTransform()
    ' REFERENCE Microsoct XML, v6.0
    Dim xmlDoc As New MSXML2.DOMDocument60, newDoc As New MSXML2.DOMDocument60
    Dim xslDoc As New MSXML2.FreeThreadedDOMDocument60
    Dim xslTemp As New MSXML2.XSLTemplate60
    Dim xslProc As Object

    ' LOAD XML AND XSL FILES
    xmlDoc.async = False
    xmlDoc.Load "C:\Path\To\Input.xml"

    xslDoc.async = False
    xslDoc.setProperty "AllowDocumentFunction", True
    xslDoc.Load "C:\Path\To\XSLT_Script.xsl"

    ' INITIALIZE NEEDED OBJECTS
    Set xslTemp.stylesheet = xslDoc
    Set xslProc = xslTemp.createProcessor()

    With xslProc
       .input = xmlDoc
       .addParameter "job", "PXJ_TEST1"             ' ADD PARAMETER(S)
       .addParameter "src", "MultiParamXSLT.xml"
       .addParameter "spath", "TEST1"

       .transform                                   ' TRANSFORM XML
       newDoc.LoadXML .output                       ' LOAD RESULT TREE
       newDoc.Save "C:\Path\To\Output.xml"          ' SAVE OUTPUT TO FILE
    End With

    Set xmlDoc = Nothing: Set newDoc = Nothing
    Set xslDoc = Nothing: Set xslTemp = Nothing: Set xslProc = Nothing
End Sub