Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

@secdev02
Forked fromleoloobeek/Numbers.Xml
Last activeFebruary 9, 2024 19:55
    • Star(0)You must be signed in to star a gist
    • Fork(1)You must be signed in to fork a gist

    Select an option

    Save secdev02/e0f764120b62e0085e649757c073af72 to your computer and use it in GitHub Desktop.
    XSLT C# Examples
    <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts">
    <msxsl:scriptlanguage="C#"implements-prefix="user">
    <![CDATA[
    public double circumference(double radius){
    double pi = 3.14;
    double circ = pi*radius*2;
    return circ;
    }
    public int getem(string message) {
    Console.WriteLine(message);
    return 0;
    }
    ]]>
    </msxsl:script>
    <xsl:templatematch="data">
    <circles>
    <xsl:for-eachselect="circle">
    <circle>
    <xsl:copy-ofselect="node()"/>
    <circumference>
    <xsl:value-ofselect="user:getem(radius)"/>
    </circumference>
    </circle>
    </xsl:for-each>
    </circles>
    </xsl:template>
    </xsl:stylesheet>
    <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts">
    <msxsl:scriptlanguage="C#"implements-prefix="user">
    <![CDATA[
    public double circumference(double radius){
    double pi = 3.14;
    double circ = pi*radius*2;
    Console.WriteLine("Hey There");
    return circ;
    }
    ]]>
    </msxsl:script>
    <xsl:templatematch="data">
    <circles>
    <xsl:for-eachselect="circle">
    <circle>
    <xsl:copy-ofselect="node()"/>
    <circumference>
    <xsl:value-ofselect="user:circumference(radius)"/>
    </circumference>
    </circle>
    </xsl:for-each>
    </circles>
    </xsl:template>
    </xsl:stylesheet>
    # Can probably compact further
    $xslt=New-Object System.Xml.Xsl.XslCompiledTransform
    $xslt_settings=New-Object System.Xml.Xsl.Xsltsettings
    $xslt_settings.EnableScript=$true
    $xslt.Load("https://gist.githubusercontent.com/secdev02/e0f764120b62e0085e649757c073af72/raw/1a614d760aab80e582e28bc8b17bfb8ca7a4559f/calc.xsl",$xslt_settings,$(new-object System.Xml.XmlUrlResolver))
    $xslt.Transform("https://gist.githubusercontent.com/secdev02/e0f764120b62e0085e649757c073af72/raw/1a614d760aab80e582e28bc8b17bfb8ca7a4559f/Numbers.Xml","dumb.txt")
    <?xml version='1.0'?>
    <data>
    <circle>
    <radius>12</radius>
    </circle>
    <circle>
    <radius>37.5</radius>
    </circle>
    </data>
    usingSystem;
    usingSystem.IO;
    usingSystem.Xml;
    usingSystem.Xml.XPath;
    usingSystem.Xml.Xsl;
    publicclassSample{
    privateconstStringfilename="number.xml";
    privateconstStringstylesheet="calc.xsl";
    publicstaticvoidMain(){
    // Compile the style sheet.
    XsltSettingsxslt_settings=newXsltSettings();
    xslt_settings.EnableScript=true;
    XslCompiledTransformxslt=newXslCompiledTransform();
    xslt.Load(stylesheet,xslt_settings,newXmlUrlResolver());
    // Load the XML source file.
    XPathDocumentdoc=newXPathDocument(filename);
    // Create an XmlWriter.
    XmlWriterSettingssettings=newXmlWriterSettings();
    settings.OmitXmlDeclaration=true;
    settings.Indent=true;
    XmlWriterwriter=XmlWriter.Create("output.xml",settings);
    // Execute the transformation.
    xslt.Transform(doc,writer);
    writer.Close();
    }
    }
    <Projectxmlns="http://schemas.microsoft.com/developer/msbuild/2003"ToolsVersion="4.0">
    <TargetName="Example">
    <ItemGroup>
    <XmlFilesInclude="https://gist.githubusercontent.com/caseysmithrc/8e58d11bc99e496a19424fbe5a99175f/raw/eac5a3114138e54f628489dbd50fb08fac5f2a32/Numbers.Xml" />
    </ItemGroup>
    <PropertyGroup>
    <XslFile>https://gist.githubusercontent.com/caseysmithrc/8e58d11bc99e496a19424fbe5a99175f/raw/eac5a3114138e54f628489dbd50fb08fac5f2a32/calc.xsl</XslFile>
    </PropertyGroup>
    <XslTransformation
    OutputPaths="blah"
    XmlInputPaths="%(XmlFiles.Identity)"
    XslInputPath="$(XslFile)"
    />
    </Target>
    </Project>
    Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp