Skip to content

Add support for OPTIONAL in Table and Conditional #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: vNext
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions OpenXmlPowerTools/DocumentAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ private static string ValidatePerSchema(XElement element)
<xs:element name='Table'>
<xs:complexType>
<xs:attribute name='Select' type='xs:string' use='required' />
<xs:attribute name='Optional' type='xs:boolean' use='optional' />
</xs:complexType>
</xs:element>
</xs:schema>",
Expand Down Expand Up @@ -521,6 +522,7 @@ private static string ValidatePerSchema(XElement element)
<xs:attribute name='Select' type='xs:string' use='required' />
<xs:attribute name='Match' type='xs:string' use='optional' />
<xs:attribute name='NotMatch' type='xs:string' use='optional' />
<xs:attribute name='Optional' type='xs:boolean' use='optional' />
</xs:complexType>
</xs:element>
</xs:schema>",
Expand Down Expand Up @@ -692,6 +694,10 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
}
if (tableData.Count() == 0)
return CreateContextErrorMessage(element, "Table Select returned no data", templateError);

var optionalString = (string)element.Attribute(PA.Optional);
bool optional = (optionalString != null && optionalString.ToLower() == "true");

XElement table = element.Element(W.tbl);
XElement protoRow = table.Elements(W.tr).Skip(1).FirstOrDefault();
var footerRowsBeforeTransform = table
Expand Down Expand Up @@ -720,7 +726,7 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
string newValue = null;
try
{
newValue = EvaluateXPathToString(d, xPath, false);
newValue = EvaluateXPathToString(d, xPath, optional);
}
catch (XPathException e)
{
Expand Down Expand Up @@ -750,6 +756,8 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
string xPath = (string)element.Attribute(PA.Select);
var match = (string)element.Attribute(PA.Match);
var notMatch = (string)element.Attribute(PA.NotMatch);
var optionalString = (string)element.Attribute(PA.Optional);
bool optional = (optionalString != null && optionalString.ToLower() == "true");

if (match == null && notMatch == null)
return CreateContextErrorMessage(element, "Conditional: Must specify either Match or NotMatch", templateError);
Expand All @@ -760,7 +768,7 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr

try
{
testValue = EvaluateXPathToString(data, xPath, false);
testValue = EvaluateXPathToString(data, xPath, optional);
}
catch (XPathException e)
{
Expand Down