Skip to content

Commit 44ec9da

Browse files
committed
Add support for OPTIONAL in Table and Conditional
1 parent 135af90 commit 44ec9da

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

OpenXmlPowerTools/DocumentAssembler.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ private static string ValidatePerSchema(XElement element)
483483
<xs:element name='Table'>
484484
<xs:complexType>
485485
<xs:attribute name='Select' type='xs:string' use='required' />
486+
<xs:attribute name='Optional' type='xs:boolean' use='optional' />
486487
</xs:complexType>
487488
</xs:element>
488489
</xs:schema>",
@@ -521,6 +522,7 @@ private static string ValidatePerSchema(XElement element)
521522
<xs:attribute name='Select' type='xs:string' use='required' />
522523
<xs:attribute name='Match' type='xs:string' use='optional' />
523524
<xs:attribute name='NotMatch' type='xs:string' use='optional' />
525+
<xs:attribute name='Optional' type='xs:boolean' use='optional' />
524526
</xs:complexType>
525527
</xs:element>
526528
</xs:schema>",
@@ -692,6 +694,10 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
692694
}
693695
if (tableData.Count() == 0)
694696
return CreateContextErrorMessage(element, "Table Select returned no data", templateError);
697+
698+
var optionalString = (string)element.Attribute(PA.Optional);
699+
bool optional = (optionalString != null && optionalString.ToLower() == "true");
700+
695701
XElement table = element.Element(W.tbl);
696702
XElement protoRow = table.Elements(W.tr).Skip(1).FirstOrDefault();
697703
var footerRowsBeforeTransform = table
@@ -720,7 +726,7 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
720726
string newValue = null;
721727
try
722728
{
723-
newValue = EvaluateXPathToString(d, xPath, false);
729+
newValue = EvaluateXPathToString(d, xPath, optional);
724730
}
725731
catch (XPathException e)
726732
{
@@ -750,6 +756,8 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
750756
string xPath = (string)element.Attribute(PA.Select);
751757
var match = (string)element.Attribute(PA.Match);
752758
var notMatch = (string)element.Attribute(PA.NotMatch);
759+
var optionalString = (string)element.Attribute(PA.Optional);
760+
bool optional = (optionalString != null && optionalString.ToLower() == "true");
753761

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

761769
try
762770
{
763-
testValue = EvaluateXPathToString(data, xPath, false);
771+
testValue = EvaluateXPathToString(data, xPath, optional);
764772
}
765773
catch (XPathException e)
766774
{

0 commit comments

Comments
 (0)