2

I need to parse xsl along with its parameters using VBA code. I can use VBA code from the below link(for your reference) but only thing is I need to pass XSLT Parameters through VBA code.

VBA code : Hperlink

My (ds_test.xsl) file

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

My input (Metadata.xml) file

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

My Expected Output in format of XML (output.xml)

Note : Here is my question - In runtime I should pass XSL parameter's value through Excel VBA code as I mentioned earlier. Let us assume that I am giving the below parameter value

VBA code : Hperlink

  1. $job = "PXJ_TEST1"
  2. $src = "Metadata.xml"
  3. $spath = "TEST1"

output.xml should be in below format

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

1

For future readers who land on this page, below is a concrete implementation of passing parameters to XSLT from VBA using OP's inputs:

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
Sign up to request clarification or add additional context in comments.

Comments

0

If you need to set parameters then the simple approach with transformNode does not work, you need to use the API presented in http://msdn.microsoft.com/en-us/library/ms762799%28v=vs.85%29.aspx (the page only has JScript and C++ unfortunately but of course you can use the same API and objects with VBScript or VBA). Then you can use the method http://msdn.microsoft.com/en-us/library/ms762312%28v=vs.85%29.aspx addParameter to set a parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.