I am trying to read/update/delete the XML file on the basis of value found.
I have a XML with name 123456.xml with below format.
<ps>
<p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
<p n="277" u="/ae/english/plan_book/plan_and_book.aspx"/>
<p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
</ps>
Now my new method in java will get Filepath (c://java/Files/12345.xml), n(277 - the value which will be checked in file) and U ("/de/english/plan_book/plan_and_book.aspx").
Logic for my java method will as below, but really don't know how to write.
Adding/Appending Method Logic:
c://java/Files/12345.xml<p n="777" u="/ao/english/plan_book/plan_and_book.aspx"/>).Deleting Method Logic:
c://java/Files/12345.xmlWould appreciate if you share some good example or links for above implementaion.
Thanks.
This kind of processing is usually easier and simpler (no regEx required) to specify in XSLT than in an imperative language.
The XSLT transformation below can be used directly, or it can give an idea how to implement the same algorithm in another language:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pAction" select="'delete'"/>
<xsl:param name="pN" select="277"/>
<xsl:param name="pU" select="'/de/english/plan_book/plan_and_book.aspx'"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ps">
<ps>
<xsl:apply-templates select=
"*[not($pAction = 'delete')]
|
*[$pAction = 'delete' and not(@n = $pN)]
"/>
<xsl:if test="$pAction = 'add' and not(p[@n = $pN])">
<p n="{$pN}" u="{$pU}"/>
</xsl:if>
</ps>
</xsl:template>
<xsl:template match="p">
<xsl:choose>
<xsl:when test="not(@n = $pN)">
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="not($pAction = 'delete')">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<ps>
<p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
<p n="277" u="/ae/english/plan_book/plan_and_book.aspx"/>
<p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
</ps>
the wanted, correct result is produced:
<ps>
<p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
<p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
</ps>
when the parameter $pAction is changed to:
<xsl:param name="pAction" select="''add'"/>
then the result of the transformation is the same XML document (unchanged).
When the parameter is:
<xsl:param name="pAction" select="''add'"/>
and the XML document is:
<ps>
<p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
<p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
</ps>
then the result is:
<ps>
<p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
<p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
<p n="277" u="/de/english/plan_book/plan_and_book.aspx"/>
</ps>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With