diff --git a/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs b/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs index cf62d2f0..075ee13e 100644 --- a/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs +++ b/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs @@ -116,7 +116,8 @@ public class DaTests [InlineData("DA264-InvalidRunLevelRepeat.docx", "DA-Data.xml", true)] [InlineData("DA265-RunLevelRepeatWithWhiteSpaceBefore.docx", "DA-Data.xml", false)] [InlineData("DA266-RunLevelRepeat-NoData.docx", "DA-Data.xml", true)] - + [InlineData("DA300-TableWithContentInCells.docx", "DA-Data.xml", false)] + public void DA101(string name, string data, bool err) { DirectoryInfo sourceDir = new DirectoryInfo("../../../../TestFiles/"); diff --git a/OpenXmlPowerTools/DocumentAssembler.cs b/OpenXmlPowerTools/DocumentAssembler.cs index f4d9673e..7f94a948 100644 --- a/OpenXmlPowerTools/DocumentAssembler.cs +++ b/OpenXmlPowerTools/DocumentAssembler.cs @@ -717,10 +717,18 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr XElement paragraph = tc.Elements(W.p).FirstOrDefault(); XElement cellRun = paragraph.Elements(W.r).FirstOrDefault(); string xPath = paragraph.Value; + bool cellIsOptional = false; + + // There is probably a much better way of doing this, + // similar to the ContentReplacementTransform used for the footer + // but it means you probably couldn't use the simple xpath text + // and you would need to always use tag in each cell! + TableCellContent(paragraph.Value, out xPath, out cellIsOptional); + string newValue = null; try { - newValue = EvaluateXPathToString(d, xPath, false); + newValue = EvaluateXPathToString(d, xPath, cellIsOptional); } catch (XPathException e) { @@ -781,6 +789,33 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr return node; } + /// + /// Convert the cell content to data using 2 different formats + /// 1. the text is xpath + /// 2. the text has the format + /// + /// Paragraph text + /// The resulting xpath of paragraph or Select attribute value + /// Incase 2 the value of the Optional attribute if it exists otherwise false + private static void TableCellContent(string para, out string xpath, out bool optional) + { + XElement element; + try + { + element = XElement.Parse(para); + //the next few lines are used a couple of time in the ContentReplacementTransform method + xpath = (string)element.Attribute(PA.Select); + var optionalString = (string)element.Attribute(PA.Optional); + optional = (optionalString != null && optionalString.ToLower() == "true"); + } + catch (Exception) + { + //can't be processed as Content so just assume xpath + xpath = para; + optional = false; + } + } + private static object CreateContextErrorMessage(XElement element, string errorMessage, TemplateError templateError) { XElement para = element.Descendants(W.p).FirstOrDefault(); diff --git a/README.md b/README.md index 86eb8d3e..c2e37797 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -NuGet Feed for CI build: https://ci.appveyor.com/nuget/open-xml-powertools +NuGet Feed for CI build: https://ci.appveyor.com/nuget/open-xml-powertools -No NuGet.org feed at this time. We are working on it. +[https://github.com/EricWhiteDev/Open-Xml-PowerTools](https://github.com/EricWhiteDev/Open-Xml-PowerTools) News ==== diff --git a/TestFiles/DA300-TableWithContentInCells.docx b/TestFiles/DA300-TableWithContentInCells.docx new file mode 100644 index 00000000..75cc9bc6 Binary files /dev/null and b/TestFiles/DA300-TableWithContentInCells.docx differ