Plain text to odt with dynamic elements - libreoffice

I am trying to figure out a way to create odt files with dynamic elements from a plain text file and avoid using any kind of scripts. While doing some testing, I can easily convert an odt file to plain text using libre office command line tool. However, when trying to convert from plain text to odt format, dynamic elements are lost. I searched the documentation but nothing specifies the expected format when converting.
I tried using the command libreoffice --convert-to odt sampleFile.txt but dynamic elements are not recognized.
Considering the following .txt file that contains
${Parent.child}
Can I convert it to a working .odt file ?
UPDATE:
I was able to do so using the XML format but it is not the desired way of doing this because the XML format requires/generate a lot of elements.
Sample file:
<?xml version="1.0" encoding="UTF-8"?>
<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2" office:mimetype="application/vnd.oasis.opendocument.text">
<office:meta><meta:document-statistic meta:character-count="108" meta:image-count="0" meta:object-count="0" meta:page-count="1" meta:paragraph-count="5" meta:table-count="0" meta:word-count="5"/><dc:date>2021-09-02T10:44:35</dc:date><meta:editing-duration>PT00H00M00S</meta:editing-duration><meta:editing-cycles>1</meta:editing-cycles><meta:generator>LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-3</meta:generator></office:meta>
<office:settings>
<config:config-item-set config:name="ooo:view-settings">
<config:config-item config:name="ViewAreaTop" config:type="long">0</config:config-item>
<config:config-item config:name="ViewAreaLeft" config:type="long">0</config:config-item>
<config:config-item config:name="ViewAreaWidth" config:type="long">0</config:config-item>
<config:config-item config:name="ViewAreaHeight" config:type="long">0</config:config-item>
<config:config-item config:name="ShowRedlineChanges" config:type="boolean">true</config:config-item>
<config:config-item config:name="InBrowseMode" config:type="boolean">false</config:config-item>
</config:config-item-set>
<config:config-item-set config:name="ooo:configuration-settings">
<config:config-item config:name="PrintPaperFromSetup" config:type="boolean">false</config:config-item>
<config:config-item config:name="PrintFaxName" config:type="string"/>
<config:config-item config:name="PrintSingleJobs" config:type="boolean">false</config:config-item>
<config:config-item config:name="PrintProspectRTL" config:type="boolean">false</config:config-item>
<config:config-item config:name="PrintProspect" config:type="boolean">false</config:config-item>
<!-- ... and more -->
</config:config-item-set>
</office:settings>
<office:scripts>
<office:script script:language="ooo:Basic">
<ooo:libraries xmlns:ooo="http://openoffice.org/2004/office" xmlns:xlink="http://www.w3.org/1999/xlink"/>
</office:script>
</office:scripts>
<!-- ... truncated styles -->
<office:body>
<office:text>
<text:sequence-decls>
<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
</text:sequence-decls>
<text:p text:style-name="Standard"><text:text-input text:description="jooscript">${Parent.child}</text:text-input></text:p>
</office:text>
</office:body>
</office:document>

Related

How in Powershell see all XML Levels

I have a test xml and I want to get the value from this line ATTRIBUTE NAME="News- offers_OPT_EMAIL">F
so I can check for the value F or T
if I do below I can get the title but how do I get the above line value.
[xml]$xml = Get-Content testFile.xml
$xml
$xml.CUSTOMERS.CUSTOMER.NAME.TITLE
sample XML code
<?xml version="1.0" encoding="UTF-8"?>
<CUSTOMERS xml:lang="en">
<CUSTOMER CREATED_DATE="2018-01-01 05:18:53.0" GROUP_ID="" ID="95656565">
<NAME>
<TITLE>M</TITLE>
<FIRST_NAME>Joe</FIRST_NAME>
<LAST_NAME>Smith</LAST_NAME>
</NAME>
<GENDER/>
<DATE_OF_BIRTH/>
<ADDRESS>
<ADDRESS_LINE_1>1 White House</ADDRESS_LINE_1>
<ADDRESS_LINE_2>Old Ave</ADDRESS_LINE_2>
<ADDRESS_LINE_3/>
<TOWNCITY>LONDON</TOWNCITY>
<COUNTY/>
<POSTCODE>18659</POSTCODE>
<COUNTRY>France</COUNTRY>
</ADDRESS>
<ADDRESS>
<ADDRESS_LINE_1>175 avenue de la division Leclerc</ADDRESS_LINE_1>
<ADDRESS_LINE_2/>
<ADDRESS_LINE_3/>
<TOWNCITY>Antony</TOWNCITY>
<COUNTY/>
<POSTCODE>92160</POSTCODE>
<COUNTRY>France</COUNTRY>
</ADDRESS>
<CONTACT_DETAILS>
<TELEPHONE MARKETING_OPT_IN="F" TYPE="MOBILE">0123456789</TELEPHONE>
<EMAIL MARKETING_OPT_IN="F">johnsmith#gmail.com</EMAIL>
</CONTACT_DETAILS>
<ATTRIBUTE NAME="News- offers_OPT_EMAIL">F</ATTRIBUTE>
<NOTE>NA</NOTE>
</CUSTOMER>
You could use SelectSingleNode or SelectNodes with an XPath expression. There are several options to achieve what you want, depending on your intention, but this would be one way to do it:
# finde the nodes
$nodes = $xml.SelectNodes("//*[local-name()='ATTRIBUTE'][#NAME='News- offers_OPT_EMAIL']")
# get value
$nodes.InnerText
Or if the value of the attribute doesn't matter, simply do:
$xml.customers.customer.attribute.'#text'

INVALID_REQUEST: Field [order.avsDetails.billToFirstname] was not in charset [ISO-8859-1]

For some Reasons when I use OnTap MasterCard Extension, Any Arabic characters in shippment addresses throws an error:
INVALID_REQUEST: Field [order.avsDetails.billToFirstname] was not in charset [ISO-8859-1]
The extension link :
https://marketplace.magento.com/ontap-module-mastercard.html
Please help.
You can try encoding the data generated in the Builders (inside the Gateway/Request folder) by using plugins.
You can read more how to create plugins here that perform the encoding on all the fields in the builders when needed.
You will create a new module that is doing the modifications needed on the extension you took from the market.
To define your builder in this case your di.xml will look something like:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="\OnTap\MasterCard\Gateway\Request\ShippingDataBuilder">
<plugin name="jsparo_ontap_mastercard_gateway_request_shippingdatabuilder" type="Jsparo\MasterCard\Plugin\Gateway\Request\ShippingDataBuilder" sortOrder="1"/>
</type>
</config>
And the Plugin/Gateway/Request/ShippingDataBuilder.php that you will be something like:
<?php
namespace Jsparo\MasterCard\Plugin\Gateway\Request;
class ShippingDataBuilder {
public function afterBuild(array $subject, $result) {
array_walk_recursive($result, function(&$value) {
$value = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
}
return $result;
}
}
You will have to do this for all the builders that generate incorrect data.

Magento 2 Currency symbol is not showing

I have 2 stores English and Arabic. Default Store is Arabic and default currency is SAR. Currency symbol is showing fine on English Store but not showing anywhere on Arabic store. It only showing price like this 44 on product listing page and on single product page.
I fixed it.
Posting my answer may be it can help someone.
edit this file.
vendor\magento\zendframework1\library\Zend\Locale\Data\ar_SA.xml
and remove following code.
<numbers>
<currencyFormats numberSystem="latn">
<currencyFormatLength>
<currencyFormat type="standard">
<pattern>¤#0.00</pattern>
</currencyFormat>
</currencyFormatLength>
</currencyFormats>
</numbers>
UPDATED ANSWER ========
I got better solution instead editing core file you can do it with observer.
Vendor/Module/etc/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="currency_display_options_forming">
<observer name="change_currency_position" instance="Vendor\Module\Model\Observer\ChangeCurrencyPosition" />
</event>
</config>
and observer File.
use Magento\Framework\Event\ObserverInterface;
class ChangeCurrencyPosition implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$currencyOptions = $observer->getEvent()->getCurrencyOptions();
$currencyOptions->setData('position', \Magento\Framework\Currency::RIGHT);
return $this;
}
}
Need to change the 'position' to RIGHT.
Its a bug in 2.4.3:
src\vendor\magento\module-directory\Model\Currency.php
Comment out these lines:
if ($this->canUseNumberFormatter($options)) {
return $this->formatCurrency($price, $options);
}
Then:
php -dmemory_limit=6G bin/magento setup:upgrade
php -dmemory_limit=6G bin/magento setup:di:compile
php -dmemory_limit=6G bin/magento setup:static-content:deploy -f
php -dmemory_limit=6G bin/magento cache:flush
Editing core is bad, we can create preference.
If #Ask4Tec's solution is not working
Try this
Copy the currency and numbers block from en.xml file and paste it into the ar_SA.xml file
Clean and flush cache
Check after hard refresh
Update ar_SA.xml like below:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ldml SYSTEM "../../common/dtd/ldml.dtd">
<!-- Copyright © 1991-2013 Unicode, Inc.
CLDR data files are interpreted according to the LDML specification (http://unicode.org/reports/tr35/)
For terms of use, see http://www.unicode.org/copyright.html
-->
<ldml>
<identity>
<version number="$Revision: 9287 $"/>
<generation date="$Date: 2013-08-28 21:32:04 -0500 (Wed, 28 Aug 2013) $"/>
<language type="ar"/>
<territory type="SA"/>
</identity>
<dates>
<calendars>
<calendar type="islamic">
<dateTimeFormats>
<availableFormats>
<dateFormatItem id="Md" draft="contributed">M/d</dateFormatItem>
<dateFormatItem id="MEd" draft="contributed">E, M/d</dateFormatItem>
<dateFormatItem id="MMMd" draft="contributed">MMM d</dateFormatItem>
<dateFormatItem id="MMMEd" draft="contributed">E, MMM d</dateFormatItem>
</availableFormats>
</dateTimeFormats>
</calendar>
</calendars>
</dates>
<numbers>
<symbols numberSystem="latn">
<decimal>.</decimal>
<group>,</group>
<list>;</list>
<percentSign>%</percentSign>
<plusSign>+</plusSign>
<minusSign>-</minusSign>
<exponential>E</exponential>
<superscriptingExponent>×</superscriptingExponent>
<perMille>‰</perMille>
<infinity>∞</infinity>
<nan>NaN</nan>
</symbols>
<decimalFormats numberSystem="latn">
<decimalFormatLength>
<decimalFormat>
<pattern>#,##0.###</pattern>
</decimalFormat>
</decimalFormatLength>
</decimalFormats>
<currencyFormats numberSystem="latn">
<currencyFormatLength>
<currencyFormat type="standard">
<pattern>¤#,##0.00</pattern>
</currencyFormat>
<currencyFormat type="accounting">
<pattern>¤#,##0.00;(¤#,##0.00)</pattern>
</currencyFormat>
</currencyFormatLength>
<unitPattern count="one">{0} {1}</unitPattern>
<unitPattern count="other">{0} {1}</unitPattern>
</currencyFormats>
<currencies>
<currency type="USD">
<displayName>US Dollar</displayName>
<displayName count="one">US dollar</displayName>
<displayName count="other">US dollars</displayName>
<symbol>$</symbol>
</currency>
</currencies>
</numbers>
</ldml>

How to insert schemalocation in a xml document via DOM

i create a xml document with JAXP and search a way to insert the schemalocation.
At the moment my application produces:
<?xml version="1.0" encoding="UTF-8"?>
<root>
...
</root>
But i need:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="namespaceURL"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="namespaceURL pathToMySchema.xsd">
...
</root>
My code:
StreamResult result = new StreamResult(writer);
Document doc = getDocument();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.METHOD, "xml");
trans.setOutputProperty(OutputKeys.VERSION, "1.0");
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
DOMSource source = new DOMSource(depl.getAsElement(doc));
trans.transform(source, result);
Thanks for your time,
Kasten
In XML data model namespace nodes are not actually read from parent element but each element has its own namespace nodes. Therefore simply adding a new default namespace to root element doesn't work but results in a document like this
<root xmlns="namespaceURL">
<child xmlns=""/>
...
</root>
Notice the appearing of empty default namespace xmlns="" on the child element(s). What actually needs to be done is to modify the namespace of every node or to create a new document with the desired default namespace and copy the contents, element and attribute names etc. of the old document to the new one. These can be done by recursively going through the original document. With Java DOM implementation this can be laborious, I've heard. One short cut might be to read the document with a namespace-unaware DOM and then add as attribute the new default namespace. Other solution is to change the namespace with an XSLT transformation, which seems quite suitable in this case, since you actually are already generating the output via XSLT transformation.
Use this XSLT stylesheet to add a new default namespace and the schema location to the root element. This stylesheet preserves old namespaces but adds all elements to new default namespace if they previously were in no-namespace.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!-- Template to add a default namespace to a document -->
<!-- Elements without a namespace are "moved" to default namespace -->
<!-- Elements with a namespace are copied as such -->
<!-- string for default namespace uri and schema location -->
<xsl:variable name="ns" select="'namespaceURL'"/>
<xsl:variable name="schemaLoc" select="'namespaceURL pathToMySchema.xsd'"/>
<!-- template for root element -->
<!-- adds default namespace and schema location -->
<xsl:template match="/*" priority="1">
<xsl:element name="{local-name()}" namespace="{$ns}">
<xsl:attribute name="xsi:schemaLocation"
namespace="http://www.w3.org/2001/XMLSchema-instance">
<xsl:value-of select="$schemaLoc"/>
</xsl:attribute>
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
<!--template for elements without a namespace -->
<xsl:template match="*[namespace-uri() = '']">
<xsl:element name="{local-name()}" namespace="{$ns}">
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
<!--template for elements with a namespace -->
<xsl:template match="*[not(namespace-uri() = '')]">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<!--template to copy attributes, text, PIs and comments -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Instead of creating the transformer with
Transformer trans = transfac.newTransformer();
(which creates and stylesheet that does an identy transformation), create an XSLT input source and give it as a parameter to newTransformer()
javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
Transformer trans = transFact.newTransformer(xsltSource);
where xsltFile is a File object pointing to that XSLT file.
Set the output properties as you wish and call transform() as in your sample code. The result should be what you desired, but I have not tested this in Java. The given XSLT file is tested for some trivial cases and there is a sample input and output at the end of this answer.
Some minor notes:
The original document object is not modified in this process. The new default namespace only appears in the output of the transform() method.
The namespace prefix for schema-instance namespace is usually xsi:, not xs: as in your example code (xs: is used in schema definitions (as well as xsd:)) .
Sample input and output for the XSLT stylesheet shown above
Input:
<root>
<child>text</child>
<child attribute="attr-value"/>
<?pi-target pi-content?>
<nsx:ns-child xmlns:nsx="ns1x">
<no-ns-child>text</no-ns-child>
<!-- comment -->
<nsx:ns-child nsx:ns-attribute="nsx-attr-value">text</nsx:ns-child>
</nsx:ns-child>
<defns-child xmlns="default-ns">
<def-child attr="val">text</def-child>
<child xmlns=""/>
</defns-child>
<child>text</child>
</root>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="namespaceURL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="namespaceURL pathToMySchema.xsd">
<child>text</child>
<child attribute="attr-value"/>
<?pi-target pi-content?>
<nsx:ns-child xmlns:nsx="ns1x">
<no-ns-child>text</no-ns-child>
<!-- comment -->
<nsx:ns-child nsx:ns-attribute="nsx-attr-value">text</nsx:ns-child>
</nsx:ns-child>
<defns-child xmlns="default-ns">
<def-child attr="val">text</def-child>
<child xmlns="namespaceURL"/>
</defns-child>
<child>text</child>
</root>
You can add the namespaces in the root when creating the document.
String NS_URL = "namespaceURL";
doc = builder.newDocument();
Element root = doc.createElementNS(NS_URL, "root");
root.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance",
"xs:schemaLocation", NS_URL + " pathToMySchema.xsd");
doc.appendChild(root);
Then for each element added to the doc instead of createElement() use createElementNS()
doc.createElementNS(NS_URL, name);
This results in what you were looking for.
<root
xmlns="namespaceURL"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="namespaceURL pathToMySchema.xsd"
>
Here's how to give a hint to the parser in order to solve your problem:
http://bytes.com/topic/java/answers/16892-xerces-how-perfrom-schema-validations-without-using-xsi-schemalocation
It goes like this:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLocation",
"http://www.example.com/Report.xsd");
Here is a validation example with some source code. It might help you.
http://www.ibm.com/developerworks/xml/library/x-tipvalschm/
(If all comes to worse, you can always search-and-replace. I know it is not the ideal solution, but the javax.xml.transform.OutputKeys doesn't seem to have a member related to the schemalocation attribute.)

remove the spaces betwwen the tags in local xml file in iphone sdk

I am having an xml named test11.xml now when i pasrse the data into CXML there are spaces in the parsed xml i want to remove these space ..
Kindly Find my xml data as below
thanks
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve"><o:DocumentProperties></o:DocumentProperties><w:fonts><w:defaultFonts w:ascii="Times New Roman" w:fareast="Times New Roman" w:h-ansi="Times New Roman" w:cs="Times New Roman"/></w:fonts><w:docPr></w:docPr><w:body><wx:sect><w:p><w:pPr><w:ind w:left="560" w:first-line="-560"/></w:pPr><w:r><w:rPr><w:rFonts w:ascii="Menlo" w:h-ansi="Menlo" w:cs="Menlo"/><wx:font wx:val="Menlo"/><w:sz w:val="22"/><w:sz-cs w:val="22"/><w:b/></w:rPr><w:t><CXMLDocument 0x5d16270 [0x5d1b0e0]> <?xml version="1.0"?></w:t></w:r></w:p><w:p><w:pPr><w:ind w:left="560" w:first-line="-560"/></w:pPr><w:r><w:rPr><w:rFonts w:ascii="Menlo" w:h-ansi="Menlo" w:cs="Menlo"/><wx:font wx:val="Menlo"/><w:sz w:val="22"/><w:sz-cs w:val="22"/><w:b/></w:rPr><w:t><feed
One of the many options i suggest is read the xml in a string then use the function of the string replaceOccuranceOfString to replace space with blank and then parse the data into cxml.
Happy coding...