Fenixq
Fenixq

Reputation: 3

Script for downloading XML and generating new one with XPath

I have an xml file which is updated every few hours by an e-commerce platform. I would like to generate a separate xml file with xpath filter.

XPath code is one line.

Which language should I use to generate that xml? Where can I find any template to make it work?

Ok, so i got that xml file:

 <o id="17" url="url" price="15.00" avail="1" weight="0" stock="3" set="0" 
 basket="0">
 <cat><![CDATA[ category ]]></cat>
 <name><![CDATA[ name ]]></name>
 <imgs><main url="url"/></imgs>
 <desc><![CDATA[description]]></desc>
 <attrs><a name="text"><![CDATA[ Dev ]]></a>
 <a name="Code"><![CDATA[ ]]></a>
 <a name="EAN"><![CDATA[ EAN ]]></a>
 </attrs>

 <o id="18" url="url" price="15.00" avail="1" weight="0" stock="3" set="0" 
 basket="0">
 <cat><![CDATA[ category2 ]]></cat>
 <name><![CDATA[ name ]]></name>
 <imgs><main url="url"/></imgs>
 <desc><![CDATA[description]]></desc>
 <attrs><a name="text"><![CDATA[ Dev ]]></a>
 <a name="Code"><![CDATA[ ]]></a>
 <a name="EAN"><![CDATA[ EAN ]]></a>
 </attrs>

There is many categories of products, but i need in seperate xml only products from "category2" with whole structure of the product. I found this to make this manually:

//o[normalize-space(cat) = 'category2']

Platform updating file every few hours with new products. So i would like to have script that download this file, automatically generate new xml with only category2.

Upvotes: 0

Views: 317

Answers (1)

Parfait
Parfait

Reputation: 107577

Simply use the special-purpose language, XSLT, designed to transform XML files to run needed XPath expression. Specifically call the identity transform to copy entire document as is and remove all other nodes that do not fit criteria with an empty template.

Nearly every modern programming language (Java, C#, Python, PHP, Perl, R, VB, even Bash and PowerShell) and some software (Excel, SAS, etc.) maintains support for XSLT 1.0 usually via an XML library or module. Additionally, dedicated processors can run your XSLT script including Saxon, Xalan, xsltproc. See tag page for more info.

XSLT (save as .xsl file, a special .xml file)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- IDENTITY TRANSFORM -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- REMOVE NODES -->
  <xsl:template match="o[normalize-space(cat) != 'category2']"/>

</xsl:stylesheet>

Online Demo


A few XSLT processor examples:

Xalan command line (with Java compiler)

java org.apache.xalan.xslt.Process -IN source.xml -XSL script.xsl -OUT output.xml

Saxon command lines (with Java compiler)

java net.sf.saxon.Transform -s:source.xml -xsl:script.xsl -o:output.xml

java -jar dir/saxon9he.jar -s:source.xml -xsl:script.xsl -o:output.xml

xsltproc command line (for Unix machines)

xsltproc myScript.xsl Output.xml > myDesiredResult.xml

Powershell script (for Windows machines)

$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;

$xslt.Load("C:\Path\To\Script.xsl");
$xslt.Transform("C:\Path\To\Input.xml", "C:\Path\To\Output.xml");

Upvotes: 2

Related Questions