0
votes

I have the following XML file:

<?xml version="1.0" encoding="UTF-8"?>
<contentSet>
    <inlineXML contenttype="application/xhtml+xml" xml:lang="en">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title/>
            </head>
            <body>
                <p>EDITORS PLEASE NOTE, RESENDING TO CHANGE CERTAIN LOCATIONS FROM MOGADISHU, SOMALIA TO NAIROBI, KENYA.</p>
                <p>Years of conflict, and the threat of al Qaeda-linked Islamists battling to overthrow the government, leave many Somalis dependent on aid.  But an application called e-Shop has changed that dynamic says the World Food Programme.</p>
                <p>SHOWS: MOGADISHU, SOMALIA (RECENT) (REUTERS - ACCESS ALL)</p>
                <p>1. VARIOUS OF STOREKEEPER DISPATCHING ORDERS</p>
                <p>2. MAN CARRYING ORDER TO A WAITING DELIVERY TRUCK</p>
                <p>3. DELIVERY TRUCK ARRIVING AT AN IDP (INTERNALLY DISPLACED PERSONS) CAMP</p>
                <p>4. MAN OFFLOADING DELIVERIES AS WORLD FOOD PROGRAMME (WFP) SUPERVISORS LOOK ON</p>
                <p>5. VARIOUS OF MUSLIMA ABDIRAHMAN AND HER CHILDREN AT THEIR HOME</p>
                <p>6. (SOUNDBITE) (Somali) MUSLIMA ABDIRAHMAN, E-SHOP USER, SAYING:</p>
                <p>"It has become dangerous to go to the shops because we are afraid of catching the coronavirus. We were lucky to get this application. We enjoy online shopping; it also saves time and we get the food we want"</p>
                <p>7. VARIOUS OF WFP TECHNICIAN HELPING A WOMAN MANAGE HER ACCOUNT ON THE "E-SHOP" APPLICATION</p>
                <p>8. VARIOUS OF ILHAN MOHAMED GIVING BREAKFAST TO HER CHILDREN AT HOME</p>
                <p>9. ILHAN PLAYING WITH HER BABY</p>
                <p>10. (SOUNDBITE) (Somali) ILHAN MOHAMED, E-SHOP USER, SAYING:</p>
                <p>"I order food that I need on the day such as rice or spaghetti. The delivery team brings the food to my home and the shop owner withdraws the money from my card."</p>
                <p>NAIROBI, KENYA (RECENT) (REUTERS - ACCESS ALL)</p>
                <p>11. VARIOUS OF CESAR ARROYO, COUNTRY DIRECTOR, WFP SOMALIA</p>
                <p>12. (SOUNDBITE) (English) CESAR ARROYO, COUNTRY DIRECTOR, WFP SOMALIA:</p>
                <p>“The e-Shop is a system that if it works in Somalia, it can work anywhere else. Somalia offers all the challenges that can actually be the testing ground for a system like this.”</p>
                <p>MOGADISHU, SOMALIA (RECENT) (REUTERS - ACCESS ALL)</p>
                <p>13. VARIOUS OF MUSLIMA AND ILHAN SITTING WITH THEIR CHILDREN UNDER A TREE</p>
                <p>NAIROBI, KENYA (RECENT) (REUTERS - ACCESS ALL)</p>
                <p>14. (SOUNDBITE) (English) CESAR ARROYO, COUNTRY DIRECTOR, WFP SOMALIA:</p>
                <p>“Many years ago when we were handing food, the people accept the food but you could see in their body language how the dignity wasn’t there because when you are receiving food you are without the opportunity of discussing or challenge, you don’t have that strength and you can see the body language of these beneficiaries have changed now tremendously because now with the cash base assistance that we provide, they are the ones deciding where and what to buy.”</p>
                <p>MOGADISHU, SOMALIA (RECENT) (REUTERS - ACCESS ALL)</p>
                <p>15. E-SHOP REPRESENTATIVE SHOWING IDP HOW TO USE THE APP</p>
                <p>16. VARIOUS OF PEOPLE AT IDP CAMP</p>
                <p>17. CHILDREN AT THE CAMP</p>
                <p>STORY: Muslima Abdirahman, a Somali mother of nine, escaped death twice in the past two years while shopping for groceries in the city of Mogadishu, where Islamist insurgents regularly carry out suicide bombings on busy streets and markets.</p>
                <p>She traded the dangerous errand for online shopping earlier this year, thanks to the World Food Programme's "humanitarian e-commerce" system in Somalia -- part of its global shift from giving out food to cash.</p>
                <p>Originally conceived as a way to protect women in Somalia from security threats -- be it bombings in the capital or harassment in a camp -- it is also helping avoid large crowds at aid distribution sites during the COVID-19 pandemic.</p>
                <p>Abdirahman compares the prices of products like cooking oil and salt offered by retailers on an app called e-Shop created by WFP, spending the $60 stipend she receives from the agency as she sees fit. This frees time for working as a cleaner and childcare, she said.</p>
                <p>"It has become dangerous to go to the shops because we are afraid of catching the coronavirus. We were lucky to have this application. We enjoy online shopping; it also saves time and we get the food we want."</p>
                <p>Her neighbour Ilhan Adow, who also uses the WFP app, says she uses the hours she saves to breastfeed her children, do chores and rest.</p>
                <p>"I order food that I need on the day such as rice or spaghetti. The delivery team brings the food to my home and the shop owner withdraws the money from my card," Adow said.</p>
                <p>Though WFP launched e-Shop in 2018, it introduced home delivery in April, the month after COVID-19 hit Somalia.</p>
                <p>Years of conflict, and the threat of al Qaeda-linked Islamists battling to overthrow the government, leave many Somalis dependent on aid.</p>
                <p>But e-Shop has changed the dynamic, said Cesar Arroyo, head of WFP in the country.</p>
                <p>“Years ago, when we were handing out food, people accepted it but you could see in their body language how the dignity wasn’t there,” he said. “Now…they are the ones deciding where and what to buy.”</p>
                <p>Hundreds of retailers sell on the app. Transactions are recorded to comply with the government tax system. Purchases are verified on delivery using biometrics.</p>
                <p>Six million dollars in transactions, and more than 51,000 deliveries, have gone through the system.</p>
                <p>It operates in Mogadishu, including in camps for people forced from their homes by violence, and in all Somalia’s main towns.</p>
                <p>WFP has long used electronic vouchers for distributing cash aid for Syrian refugees, but Somalia is the first place where it has used an app and home delivery system.</p>
                <p>The U.N. agency, which won the Nobel Peace Prize this year, hopes to roll out online shopping elsewhere, said Arroyo. “If it works in Somalia it can work anywhere else.”</p>
                <p>Nearly 100,000 out of the 500,000 people receiving cash aid monthly from the U.N. in Somalia are using e-Shop. The number of app users recently shot up, from 47,000 at the end of March, before home delivery launched, to 94,000 at the end of October.</p>
            </body>
        </html>
    </inlineXML>
</contentSet>

I would like to retrieve everything in between <body> and </body>. I need to preserve the <p> and </p>.

Here's my XSL file:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://iptc.org/std/nar/2006-10-01/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="ns0 xs fn">
    <xsl:output method="xml" encoding="UTF-8" byte-order-mark="no"/>
    <xsl:template match="/">
        
        <xsl:variable name="var1_map_of_newsMessage" as="xs:string*">
            <xsl:for-each select="ns0:contentSet/ns0:inlineXML/ns1:html/ns1:body">
                <xsl:sequence select="fn:string(.)"/>
            </xsl:for-each>
        </xsl:variable>

        <xsl:variable name="body_content">       
            <xsl:sequence select="($var1_map_of_newsMessage)[(fn:position() = xs:integer('1'))]"/>      
        </xsl:variable> 

        <xsl:element name="body">
            <xsl:value-of select="$body_content"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

I get this when doing the translation: Unable to generate the XML document using the provided XML/XSL input. Errors were reported during stylesheet compilation

Any idea how I can achieve this? I looked everywhere and used Altova mapforce to help me but nothing seems to work. Thank you for taking the time to read my post.

2
While asking a question you need to provide a minimal reproducible example: (1) Input XML. (2) Your logic, and XSLT that tried to implement it. (3) Desired output. (4) XSLT processor and its version.Yitzhak Khabinsky
Did you write that XSLT code? It references namespaces that are not even in your input document and elements that are also not in your input.Sebastien
You are using <xsl:value-of select="$prefix"/>, but I don’t see it being declared.Siebe Jongebloed
Sorry about that, I copied/pasted the wrong xsl code. XML is more complex and I cleared what wasn't needed for my question, prefix and other namespace are part of the more complex XML. I have wrote most of it, however the variable "var1_map_of_newsMessage" comes from altova mapforce tool.Brice
Use a tool that provides meaningful error messages - e.g. xsltfiddle.liberty-development.net/pNEj9dhmichael.hor257k

2 Answers

2
votes

Firstly, the message Errors were reported during stylesheet compilation means what it says. There's a detailed error message somewhere, and you're not seeing it, because it's been logged or reported to some destination that you're not seeing. You need to look at how you are invoking the stylesheet compilation (via API or command line) and where you are directing the error messages.

Pasting the stylesheet into Oxygen immediately reveals the error "Namespace prefix 'ns1' has not been declared". But your comments above say that the code you posted isn't your real code (which makes me wonder why you bothered posting it), and if you don't post your real code then we can't tell you what's wrong with it.

Once you get your stylesheet to compile, there are a few errors in its logic:

  • The variable var1_map_of_newsMessage will contain a single string because there is only one body element. I think you probably wanted a string for each paragraph. Or perhaps you wanted the paragraph elements themselves, since you say you want to retain the markup.

  • You can replace the predicate [(fn:position() = xs:integer('1'))] with the predicate [1].

  • The variable declaration of bodyContent is creating an unnecessary copy of a large amount of data. To avoid the copy, either (a) declare the variable type as="node()*", or (b) use an xsl:variable element with a select attribute and no child elements.

0
votes

I would like to retrieve everything in between <body> and </body>. I need to preserve the <p> and </p>.

Is there a reason why you cannot do simply:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/contentSet">
    <xsl:copy-of select="inlineXML/x:html/x:body"/>
</xsl:template>

</xsl:stylesheet>

Or, if you don't want to preserve the namespace:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/contentSet">
    <xsl:apply-templates select="inlineXML/x:html/x:body"/>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>