java.lang.Object
com.aspose.words.XmlMapping
public class XmlMapping
To learn more, visit the Structured Document Tags or Content Control documentation article. Example:
Document doc = new Document();
// Construct an XML part that contains text and add it to the document's CustomXmlPart collection.
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
// Create a structured document tag that will display the contents of our CustomXmlPart.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
// Set a mapping for our structured document tag. This mapping will instruct
// our structured document tag to display a portion of the XML part's text contents that the XPath points to.
// In this case, it will be contents of the the second "<text>" element of the first "<root>" element: "Text element #2".
tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[2]", "xmlns:ns='http://www.w3.org/2001/XMLSchema'");
Assert.assertTrue(tag.getXmlMapping().isMapped());
Assert.assertEquals(tag.getXmlMapping().getCustomXmlPart(), xmlPart);
Assert.assertEquals(tag.getXmlMapping().getXPath(), "/root[1]/text[2]");
Assert.assertEquals(tag.getXmlMapping().getPrefixMappings(), "xmlns:ns='http://www.w3.org/2001/XMLSchema'");
// Add the structured document tag to the document to display the content from our custom part.
doc.getFirstSection().getBody().appendChild(tag);
doc.save(getArtifactsDir() + "StructuredDocumentTag.XmlMapping.docx");
| Property Getters/Setters Summary | ||
|---|---|---|
CustomXmlPart | getCustomXmlPart() | |
| Returns the custom XML data part to which the parent structured document tag is mapped. | ||
boolean | isMapped() | |
Returns true if the parent structured document tag is successfully mapped to XML data.
|
||
java.lang.String | getPrefixMappings() | |
|
Returns XML namespace prefix mappings to evaluate the |
||
java.lang.String | getStoreItemId() | |
|
Specifies the custom XML data identifier for the custom XML data part which
shall be used to evaluate the |
||
java.lang.String | getXPath() | |
| Returns the XPath expression, which is evaluated to find the custom XML node that is mapped to the parent structured document tag. | ||
| Method Summary | ||
|---|---|---|
void | delete() | |
| Deletes mapping of the parent structured document to XML data. | ||
boolean | setMapping(CustomXmlPart customXmlPart, java.lang.String xPath, java.lang.String prefixMapping) | |
| Sets a mapping between the parent structured document tag and an XML node of a custom XML data part. | ||
| Property Getters/Setters Detail |
|---|
getCustomXmlPart | |
public CustomXmlPart getCustomXmlPart() | |
Example:
Shows how to set XML mappings for custom XML parts.Document doc = new Document(); // Construct an XML part that contains text and add it to the document's CustomXmlPart collection. String xmlPartId = UUID.randomUUID().toString(); String xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>"; CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent); // Create a structured document tag that will display the contents of our CustomXmlPart. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); // Set a mapping for our structured document tag. This mapping will instruct // our structured document tag to display a portion of the XML part's text contents that the XPath points to. // In this case, it will be contents of the the second "<text>" element of the first "<root>" element: "Text element #2". tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[2]", "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); Assert.assertTrue(tag.getXmlMapping().isMapped()); Assert.assertEquals(tag.getXmlMapping().getCustomXmlPart(), xmlPart); Assert.assertEquals(tag.getXmlMapping().getXPath(), "/root[1]/text[2]"); Assert.assertEquals(tag.getXmlMapping().getPrefixMappings(), "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); // Add the structured document tag to the document to display the content from our custom part. doc.getFirstSection().getBody().appendChild(tag); doc.save(getArtifactsDir() + "StructuredDocumentTag.XmlMapping.docx");
isMapped | |
public boolean isMapped() | |
true if the parent structured document tag is successfully mapped to XML data.
Example:
Shows how to set XML mappings for custom XML parts.Document doc = new Document(); // Construct an XML part that contains text and add it to the document's CustomXmlPart collection. String xmlPartId = UUID.randomUUID().toString(); String xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>"; CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent); // Create a structured document tag that will display the contents of our CustomXmlPart. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); // Set a mapping for our structured document tag. This mapping will instruct // our structured document tag to display a portion of the XML part's text contents that the XPath points to. // In this case, it will be contents of the the second "<text>" element of the first "<root>" element: "Text element #2". tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[2]", "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); Assert.assertTrue(tag.getXmlMapping().isMapped()); Assert.assertEquals(tag.getXmlMapping().getCustomXmlPart(), xmlPart); Assert.assertEquals(tag.getXmlMapping().getXPath(), "/root[1]/text[2]"); Assert.assertEquals(tag.getXmlMapping().getPrefixMappings(), "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); // Add the structured document tag to the document to display the content from our custom part. doc.getFirstSection().getBody().appendChild(tag); doc.save(getArtifactsDir() + "StructuredDocumentTag.XmlMapping.docx");
getPrefixMappings | |
public java.lang.String getPrefixMappings() | |
Example:
Shows how to set XML mappings for custom XML parts.Document doc = new Document(); // Construct an XML part that contains text and add it to the document's CustomXmlPart collection. String xmlPartId = UUID.randomUUID().toString(); String xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>"; CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent); // Create a structured document tag that will display the contents of our CustomXmlPart. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); // Set a mapping for our structured document tag. This mapping will instruct // our structured document tag to display a portion of the XML part's text contents that the XPath points to. // In this case, it will be contents of the the second "<text>" element of the first "<root>" element: "Text element #2". tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[2]", "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); Assert.assertTrue(tag.getXmlMapping().isMapped()); Assert.assertEquals(tag.getXmlMapping().getCustomXmlPart(), xmlPart); Assert.assertEquals(tag.getXmlMapping().getXPath(), "/root[1]/text[2]"); Assert.assertEquals(tag.getXmlMapping().getPrefixMappings(), "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); // Add the structured document tag to the document to display the content from our custom part. doc.getFirstSection().getBody().appendChild(tag); doc.save(getArtifactsDir() + "StructuredDocumentTag.XmlMapping.docx");
getStoreItemId | |
public java.lang.String getStoreItemId() | |
Example:
Shows how to get the custom XML data identifier of an XML part.
Document doc = new Document(getMyDir() + "Custom XML part in structured document tag.docx");
// Structured document tags have IDs in the form of GUIDs.
StructuredDocumentTag tag = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
Assert.assertEquals("{F3029283-4FF8-4DD2-9F31-395F19ACEE85}", tag.getXmlMapping().getStoreItemId());getXPath | |
public java.lang.String getXPath() | |
Example:
Shows how to set XML mappings for custom XML parts.Document doc = new Document(); // Construct an XML part that contains text and add it to the document's CustomXmlPart collection. String xmlPartId = UUID.randomUUID().toString(); String xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>"; CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent); // Create a structured document tag that will display the contents of our CustomXmlPart. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); // Set a mapping for our structured document tag. This mapping will instruct // our structured document tag to display a portion of the XML part's text contents that the XPath points to. // In this case, it will be contents of the the second "<text>" element of the first "<root>" element: "Text element #2". tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[2]", "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); Assert.assertTrue(tag.getXmlMapping().isMapped()); Assert.assertEquals(tag.getXmlMapping().getCustomXmlPart(), xmlPart); Assert.assertEquals(tag.getXmlMapping().getXPath(), "/root[1]/text[2]"); Assert.assertEquals(tag.getXmlMapping().getPrefixMappings(), "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); // Add the structured document tag to the document to display the content from our custom part. doc.getFirstSection().getBody().appendChild(tag); doc.save(getArtifactsDir() + "StructuredDocumentTag.XmlMapping.docx");
| Method Detail |
|---|
delete | |
public void delete() | |
Example:
Shows how to set XML mappings for custom XML parts.Document doc = new Document(); // Construct an XML part that contains text and add it to the document's CustomXmlPart collection. String xmlPartId = UUID.randomUUID().toString(); String xmlPartContent = "<root><text>Text element #1</text><text>Text element #2</text></root>"; CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent); // Create a structured document tag that will display the contents of our CustomXmlPart. StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); // Set a mapping for our structured document tag. This mapping will instruct // our structured document tag to display a portion of the XML part's text contents that the XPath points to. // In this case, it will be contents of the the second "<text>" element of the first "<root>" element: "Text element #2". tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[2]", "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); Assert.assertTrue(tag.getXmlMapping().isMapped()); Assert.assertEquals(tag.getXmlMapping().getCustomXmlPart(), xmlPart); Assert.assertEquals(tag.getXmlMapping().getXPath(), "/root[1]/text[2]"); Assert.assertEquals(tag.getXmlMapping().getPrefixMappings(), "xmlns:ns='http://www.w3.org/2001/XMLSchema'"); // Add the structured document tag to the document to display the content from our custom part. doc.getFirstSection().getBody().appendChild(tag); doc.save(getArtifactsDir() + "StructuredDocumentTag.XmlMapping.docx");
setMapping | |
public boolean setMapping(CustomXmlPart customXmlPart, java.lang.String xPath, java.lang.String prefixMapping) throws java.lang.Exception | |
customXmlPart - A custom XML data part to map to.xPath - An XPath expression to find the XML node.prefixMapping - XML namespace prefix mappings to evaluate the XPath.Example:
Shows how to create a structured document tag with custom XML data.
Document doc = new Document();
// Construct an XML part that contains data and add it to the document's collection.
// If we enable the "Developer" tab in Microsoft Word,
// we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
String xmlPartId = UUID.randomUUID().toString();
String xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(xmlPartId, xmlPartContent);
Assert.assertEquals(xmlPart.getData(), xmlPartContent.getBytes());
Assert.assertEquals(xmlPart.getId(), xmlPartId);
// Below are two ways to refer to XML parts.
// 1 - By an index in the custom XML part collection:
Assert.assertEquals(xmlPart, doc.getCustomXmlParts().get(0));
// 2 - By GUID:
Assert.assertEquals(xmlPart, doc.getCustomXmlParts().getById(xmlPartId));
// Add an XML schema association.
xmlPart.getSchemas().add("http://www.w3.org/2001/XMLSchema");
// Clone a part, and then insert it into the collection.
CustomXmlPart xmlPartClone = xmlPart.deepClone();
xmlPartClone.setId(UUID.randomUUID().toString());
doc.getCustomXmlParts().add(xmlPartClone);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 2);
// Iterate through the collection and print the contents of each part.
Iterator<CustomXmlPart> enumerator = doc.getCustomXmlParts().iterator();
int index = 0;
while (enumerator.hasNext()) {
CustomXmlPart customXmlPart = enumerator.next();
System.out.println(MessageFormat.format("XML part index {0}, ID: {1}", index, customXmlPart.getId()));
System.out.println(MessageFormat.format("\tContent: {0}", customXmlPart.getData()));
index++;
}
// Use the "RemoveAt" method to remove the cloned part by index.
doc.getCustomXmlParts().removeAt(1);
Assert.assertEquals(doc.getCustomXmlParts().getCount(), 1);
// Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
CustomXmlPartCollection customXmlParts = doc.getCustomXmlParts().deepClone();
customXmlParts.clear();
// Create a structured document tag that will display our part's contents and insert it into the document body.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
tag.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", "");
doc.getFirstSection().getBody().appendChild(tag);
doc.save(getArtifactsDir() + "StructuredDocumentTag.CustomXml.docx");