| | 1 | | namespace Songhay.Xml; |
| | 2 | |
|
| | 3 | | public static partial class XmlUtility |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Returns a <see cref="string"/> |
| | 7 | | /// for the transformation of the XSLT file |
| | 8 | | /// and the XML file. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="xslPath"> |
| | 11 | | /// Output path to the XSLT file. |
| | 12 | | /// </param> |
| | 13 | | /// <param name="xmlPath"> |
| | 14 | | /// Output path to the XML file. |
| | 15 | | /// </param> |
| | 16 | | public static string? LoadXslTransform(string? xslPath, string? xmlPath) => |
| 0 | 17 | | LoadXslTransform(xslPath, xslArgs: null, xmlPath); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Returns a <see cref="string"/> |
| | 21 | | /// for the transformation of the XSLT file |
| | 22 | | /// and the XML file. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="xslPath"> |
| | 25 | | /// Output path to the XSLT file. |
| | 26 | | /// </param> |
| | 27 | | /// <param name="commandName"> |
| | 28 | | /// The value for the <code>cmd</code>-parameter convention. |
| | 29 | | /// </param> |
| | 30 | | /// <param name="xmlPath"> |
| | 31 | | /// Output path to the XML file. |
| | 32 | | /// </param> |
| | 33 | | public static string? LoadXslTransform(string? xslPath, string? commandName, string? xmlPath) |
| 0 | 34 | | { |
| 0 | 35 | | var xslArgs = GetXsltArgumentList(commandName); |
| | 36 | |
|
| 0 | 37 | | return LoadXslTransform(xslPath, xslArgs, xmlPath); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Returns a <see cref="string"/> |
| | 42 | | /// for the transformation of the XSLT file |
| | 43 | | /// and the XML file. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="xslPath"> |
| | 46 | | /// Output path to the XSLT file. |
| | 47 | | /// </param> |
| | 48 | | /// <param name="xslArgs"> |
| | 49 | | /// The <see cref="XsltArgumentList"/>. |
| | 50 | | /// </param> |
| | 51 | | /// <param name="xmlPath"> |
| | 52 | | /// Output path to the XML file. |
| | 53 | | /// </param> |
| | 54 | | public static string? LoadXslTransform(string? xslPath, XsltArgumentList? xslArgs, string? xmlPath) |
| 0 | 55 | | { |
| 0 | 56 | | xslPath.ThrowWhenNullOrWhiteSpace(); |
| 0 | 57 | | xmlPath.ThrowWhenNullOrWhiteSpace(); |
| | 58 | |
|
| 0 | 59 | | xslArgs ??= new XsltArgumentList(); |
| | 60 | |
|
| 0 | 61 | | XslCompiledTransform xslt = new XslCompiledTransform(false); |
| 0 | 62 | | xslt.Load(xslPath); |
| | 63 | |
|
| 0 | 64 | | using MemoryStream ms = new MemoryStream(); |
| 0 | 65 | | using (XmlReader reader = XmlReader.Create(xmlPath)) |
| 0 | 66 | | { |
| 0 | 67 | | XmlWriter writer = XmlWriter.Create(ms); |
| 0 | 68 | | xslt.Transform(reader, xslArgs, writer, null); |
| 0 | 69 | | } |
| | 70 | |
|
| 0 | 71 | | var ret = GetText(ms); |
| | 72 | |
|
| 0 | 73 | | return ret; |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Returns a <see cref="string"/> |
| | 78 | | /// for the transformation of the XSLT file |
| | 79 | | /// and the specified <see cref="IXPathNavigable"/> in memory. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="xslPath"> |
| | 82 | | /// Output path to the XSLT file. |
| | 83 | | /// </param> |
| | 84 | | /// <param name="commandName"> |
| | 85 | | /// The value for the <code>cmd</code>-parameter convention. |
| | 86 | | /// </param> |
| | 87 | | /// <param name="xmlFragment"> |
| | 88 | | /// A well-formed XML <see cref="string"/>. |
| | 89 | | /// </param> |
| | 90 | | /// <remarks> |
| | 91 | | /// This convention is used in ASP.NET Web applications. |
| | 92 | | /// </remarks> |
| | 93 | | public static string? LoadXslTransformForMemoryInput(string? xslPath, string? commandName, |
| | 94 | | string? xmlFragment) |
| 0 | 95 | | { |
| 0 | 96 | | var xslArgs = GetXsltArgumentList(commandName); |
| | 97 | |
|
| 0 | 98 | | return LoadXslTransformForMemoryInput(xslPath, xslArgs, xmlFragment); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Returns a <see cref="string"/> |
| | 103 | | /// for the transformation of the XSLT file |
| | 104 | | /// and the specified <see cref="IXPathNavigable"/> in memory. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="xslPath"> |
| | 107 | | /// Output path to the XSLT file. |
| | 108 | | /// </param> |
| | 109 | | /// <param name="commandName"> |
| | 110 | | /// The value for the <code>cmd</code>-parameter convention. |
| | 111 | | /// </param> |
| | 112 | | /// <param name="navigableXml"> |
| | 113 | | /// The <see cref="IXPathNavigable"/> in memory. |
| | 114 | | /// </param> |
| | 115 | | /// <remarks> |
| | 116 | | /// This convention is used in ASP.NET Web applications. |
| | 117 | | /// </remarks> |
| | 118 | | public static string? LoadXslTransformForMemoryInput(string? xslPath, string? commandName, |
| | 119 | | IXPathNavigable navigableXml) |
| 0 | 120 | | { |
| 0 | 121 | | ArgumentNullException.ThrowIfNull(navigableXml); |
| | 122 | |
|
| 0 | 123 | | var navigator = navigableXml.CreateNavigator().ToReferenceTypeValueOrThrow(); |
| 0 | 124 | | var xmlFragment = navigator.OuterXml; |
| 0 | 125 | | var xslArgs = GetXsltArgumentList(commandName); |
| | 126 | |
|
| 0 | 127 | | return LoadXslTransformForMemoryInput(xslPath, xslArgs, xmlFragment); |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Returns a <see cref="string"/> |
| | 132 | | /// for the transformation of the XSLT file |
| | 133 | | /// and the XML in-memory fragment. |
| | 134 | | /// </summary> |
| | 135 | | /// <param name="xslPath"> |
| | 136 | | /// Output path to the XSLT file. |
| | 137 | | /// </param> |
| | 138 | | /// <param name="xslArgs"> |
| | 139 | | /// The <see cref="XsltArgumentList"/>. |
| | 140 | | /// </param> |
| | 141 | | /// <param name="xmlFragment"> |
| | 142 | | /// A well-formed XML <see cref="string"/>. |
| | 143 | | /// </param> |
| | 144 | | /// <remarks> |
| | 145 | | /// This convention is used in ASP.NET Web applications. |
| | 146 | | /// </remarks> |
| | 147 | | public static string? LoadXslTransformForMemoryInput(string? xslPath, XsltArgumentList? xslArgs, |
| | 148 | | string? xmlFragment) |
| 0 | 149 | | { |
| 0 | 150 | | xslPath.ThrowWhenNullOrWhiteSpace(); |
| 0 | 151 | | xmlFragment.ThrowWhenNullOrWhiteSpace(); |
| | 152 | |
|
| 0 | 153 | | xslArgs ??= new XsltArgumentList(); |
| | 154 | |
|
| 0 | 155 | | var xslt = new XslCompiledTransform(false); |
| 0 | 156 | | xslt.Load(xslPath); |
| | 157 | |
|
| 0 | 158 | | using MemoryStream ms = new MemoryStream(); |
| 0 | 159 | | using (StringReader sr = new StringReader(xmlFragment)) |
| 0 | 160 | | { |
| 0 | 161 | | XmlReader reader = XmlReader.Create(sr); |
| 0 | 162 | | XmlWriter writer = XmlWriter.Create(ms); |
| 0 | 163 | | xslt.Transform(reader, xslArgs, writer, null); |
| 0 | 164 | | } |
| | 165 | |
|
| 0 | 166 | | var ret = GetText(ms); |
| | 167 | |
|
| 0 | 168 | | return ret; |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | static XsltArgumentList GetXsltArgumentList(string? commandName) |
| 0 | 172 | | { |
| 0 | 173 | | XsltArgumentList xslArgs = new XsltArgumentList(); |
| | 174 | |
|
| 0 | 175 | | if (!string.IsNullOrWhiteSpace(commandName)) |
| | 176 | | //CONVENTION: XSL templates use a parameter called “cmd”: |
| 0 | 177 | | xslArgs.AddParam("cmd", string.Empty, commandName); |
| | 178 | |
|
| 0 | 179 | | return xslArgs; |
| 0 | 180 | | } |
| | 181 | | } |