[Java][XSLT] Replace Map values in all the XML










0















I need to replace all occurrences of Java map keys to their values.
I'm using xslt 1.0 and don't know how to proceed.



For example:
In java I have a Map,



Map<String, String> myMap = new HashMap<String,String>();
myMap.add("valueToReplace1","valueReplaced1");
myMap.add("valueToReplace2","valueReplaced2");
myMap.add("valueToReplace3","valueReplaced3");


An example of input XML



<Root>
<attribute1>I want to replace valueToReplace1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueToReplace2
</subAttribute1>
</attribute2>
<attribute3>valueToReplace3</attribute3>
</Root>


And what I expect:



<Root>
<attribute1>I want to replace valueReplaced1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueReplaced2
</subAttribute1>
</attribute2>
<attribute3>valueReplaced3</attribute3>
</Root>


for nom my xls file look like this:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="old" />
<xsl:param name="new" />

<xsl:output method="xml" />

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()" name="text" priority="5">

<xsl:param name="pString" select="." />
<xsl:choose>
<xsl:when test="$old and contains($pString,$old)">
<xsl:value-of
select="concat(substring-before($pString,$old),$new)" />
<xsl:call-template name="text">
<xsl:with-param name="pString"
select="substring-after($pString,$old)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString" />
</xsl:otherwise>
</xsl:choose>

</xsl:template>


variables old new would be the key/value of the map.



Someone have an idea on how to do that ?



Thanks in advance.










share|improve this question






















  • If you are using Java, why are you restricted to XSLT 1.0? In the Java world Saxon 9 is available on Sourceforge and Maven to use XSLT 2 or 3. If you need to use XSLT 1, then it seems that exslt.org/str/functions/replace/str.replace.template.xsl (described at exslt.org/str/functions/replace/index.html) implements replacing various values in one go, you would just need to make sure you get your Java Map into the XSLT as a parameter. It is not clear whether you are looking for details on the Java side or on the XSLT side.

    – Martin Honnen
    Nov 14 '18 at 15:35











  • @MartinHonnen Thank you for your answer, I was trying to add as less dependencies as possible and since I work on existing code I was trying to not impact it too much, but maybe the use of XSLT 2 or 3 worth it. I need mode detail for the XSLT side, for java i just need to add the map to parameter. I will try to watch how to use Saxon 9 instead of actual xml transform. Futhermore I don't find any example on how my map can be use with str:replace with XSLT 1. Have you any hint ?

    – Laurent
    Nov 15 '18 at 9:21











  • I am not sure whether you expect to be able to pass in your Java Map to XSLT, I rather think that won't work unless you use Saxon 9.8 or 9.9 where there is, with the XPath 3.1 map type, a direct conversion from Java types to XSLT/Path types possible. As for the use of the str:replace template with XSLT 1, I have tried that at xsltransform.hikmatu.com/bFDb2BL, it performs the multiple replacements one an input of the search and replacement terms as an XML/XSLT type (e.g. a result tree fragment) is used.

    – Martin Honnen
    Nov 15 '18 at 9:35











  • As for using Saxon 9 and XSLT 3 with XPath 3.1 where you can directly convert a Java Map<String, String> map to an XSLT/XPath 3.1 map(xs:string, xs:string) parameter using saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/…, see xsltfiddle.liberty-development.net/eiZQaGi/0.

    – Martin Honnen
    Nov 15 '18 at 9:40











  • @MartinHonnen So after several tries, I finally used Saxon 9 and XSLT and its working well, thanks a lot !

    – Laurent
    Nov 15 '18 at 15:35















0















I need to replace all occurrences of Java map keys to their values.
I'm using xslt 1.0 and don't know how to proceed.



For example:
In java I have a Map,



Map<String, String> myMap = new HashMap<String,String>();
myMap.add("valueToReplace1","valueReplaced1");
myMap.add("valueToReplace2","valueReplaced2");
myMap.add("valueToReplace3","valueReplaced3");


An example of input XML



<Root>
<attribute1>I want to replace valueToReplace1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueToReplace2
</subAttribute1>
</attribute2>
<attribute3>valueToReplace3</attribute3>
</Root>


And what I expect:



<Root>
<attribute1>I want to replace valueReplaced1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueReplaced2
</subAttribute1>
</attribute2>
<attribute3>valueReplaced3</attribute3>
</Root>


for nom my xls file look like this:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="old" />
<xsl:param name="new" />

<xsl:output method="xml" />

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()" name="text" priority="5">

<xsl:param name="pString" select="." />
<xsl:choose>
<xsl:when test="$old and contains($pString,$old)">
<xsl:value-of
select="concat(substring-before($pString,$old),$new)" />
<xsl:call-template name="text">
<xsl:with-param name="pString"
select="substring-after($pString,$old)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString" />
</xsl:otherwise>
</xsl:choose>

</xsl:template>


variables old new would be the key/value of the map.



Someone have an idea on how to do that ?



Thanks in advance.










share|improve this question






















  • If you are using Java, why are you restricted to XSLT 1.0? In the Java world Saxon 9 is available on Sourceforge and Maven to use XSLT 2 or 3. If you need to use XSLT 1, then it seems that exslt.org/str/functions/replace/str.replace.template.xsl (described at exslt.org/str/functions/replace/index.html) implements replacing various values in one go, you would just need to make sure you get your Java Map into the XSLT as a parameter. It is not clear whether you are looking for details on the Java side or on the XSLT side.

    – Martin Honnen
    Nov 14 '18 at 15:35











  • @MartinHonnen Thank you for your answer, I was trying to add as less dependencies as possible and since I work on existing code I was trying to not impact it too much, but maybe the use of XSLT 2 or 3 worth it. I need mode detail for the XSLT side, for java i just need to add the map to parameter. I will try to watch how to use Saxon 9 instead of actual xml transform. Futhermore I don't find any example on how my map can be use with str:replace with XSLT 1. Have you any hint ?

    – Laurent
    Nov 15 '18 at 9:21











  • I am not sure whether you expect to be able to pass in your Java Map to XSLT, I rather think that won't work unless you use Saxon 9.8 or 9.9 where there is, with the XPath 3.1 map type, a direct conversion from Java types to XSLT/Path types possible. As for the use of the str:replace template with XSLT 1, I have tried that at xsltransform.hikmatu.com/bFDb2BL, it performs the multiple replacements one an input of the search and replacement terms as an XML/XSLT type (e.g. a result tree fragment) is used.

    – Martin Honnen
    Nov 15 '18 at 9:35











  • As for using Saxon 9 and XSLT 3 with XPath 3.1 where you can directly convert a Java Map<String, String> map to an XSLT/XPath 3.1 map(xs:string, xs:string) parameter using saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/…, see xsltfiddle.liberty-development.net/eiZQaGi/0.

    – Martin Honnen
    Nov 15 '18 at 9:40











  • @MartinHonnen So after several tries, I finally used Saxon 9 and XSLT and its working well, thanks a lot !

    – Laurent
    Nov 15 '18 at 15:35













0












0








0








I need to replace all occurrences of Java map keys to their values.
I'm using xslt 1.0 and don't know how to proceed.



For example:
In java I have a Map,



Map<String, String> myMap = new HashMap<String,String>();
myMap.add("valueToReplace1","valueReplaced1");
myMap.add("valueToReplace2","valueReplaced2");
myMap.add("valueToReplace3","valueReplaced3");


An example of input XML



<Root>
<attribute1>I want to replace valueToReplace1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueToReplace2
</subAttribute1>
</attribute2>
<attribute3>valueToReplace3</attribute3>
</Root>


And what I expect:



<Root>
<attribute1>I want to replace valueReplaced1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueReplaced2
</subAttribute1>
</attribute2>
<attribute3>valueReplaced3</attribute3>
</Root>


for nom my xls file look like this:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="old" />
<xsl:param name="new" />

<xsl:output method="xml" />

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()" name="text" priority="5">

<xsl:param name="pString" select="." />
<xsl:choose>
<xsl:when test="$old and contains($pString,$old)">
<xsl:value-of
select="concat(substring-before($pString,$old),$new)" />
<xsl:call-template name="text">
<xsl:with-param name="pString"
select="substring-after($pString,$old)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString" />
</xsl:otherwise>
</xsl:choose>

</xsl:template>


variables old new would be the key/value of the map.



Someone have an idea on how to do that ?



Thanks in advance.










share|improve this question














I need to replace all occurrences of Java map keys to their values.
I'm using xslt 1.0 and don't know how to proceed.



For example:
In java I have a Map,



Map<String, String> myMap = new HashMap<String,String>();
myMap.add("valueToReplace1","valueReplaced1");
myMap.add("valueToReplace2","valueReplaced2");
myMap.add("valueToReplace3","valueReplaced3");


An example of input XML



<Root>
<attribute1>I want to replace valueToReplace1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueToReplace2
</subAttribute1>
</attribute2>
<attribute3>valueToReplace3</attribute3>
</Root>


And what I expect:



<Root>
<attribute1>I want to replace valueReplaced1</attribute1>
<attribute2>
<subAttribute1>I want to replace valueReplaced2
</subAttribute1>
</attribute2>
<attribute3>valueReplaced3</attribute3>
</Root>


for nom my xls file look like this:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="old" />
<xsl:param name="new" />

<xsl:output method="xml" />

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()" name="text" priority="5">

<xsl:param name="pString" select="." />
<xsl:choose>
<xsl:when test="$old and contains($pString,$old)">
<xsl:value-of
select="concat(substring-before($pString,$old),$new)" />
<xsl:call-template name="text">
<xsl:with-param name="pString"
select="substring-after($pString,$old)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString" />
</xsl:otherwise>
</xsl:choose>

</xsl:template>


variables old new would be the key/value of the map.



Someone have an idea on how to do that ?



Thanks in advance.







java xml xslt xslt-1.0






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:37









LaurentLaurent

14




14












  • If you are using Java, why are you restricted to XSLT 1.0? In the Java world Saxon 9 is available on Sourceforge and Maven to use XSLT 2 or 3. If you need to use XSLT 1, then it seems that exslt.org/str/functions/replace/str.replace.template.xsl (described at exslt.org/str/functions/replace/index.html) implements replacing various values in one go, you would just need to make sure you get your Java Map into the XSLT as a parameter. It is not clear whether you are looking for details on the Java side or on the XSLT side.

    – Martin Honnen
    Nov 14 '18 at 15:35











  • @MartinHonnen Thank you for your answer, I was trying to add as less dependencies as possible and since I work on existing code I was trying to not impact it too much, but maybe the use of XSLT 2 or 3 worth it. I need mode detail for the XSLT side, for java i just need to add the map to parameter. I will try to watch how to use Saxon 9 instead of actual xml transform. Futhermore I don't find any example on how my map can be use with str:replace with XSLT 1. Have you any hint ?

    – Laurent
    Nov 15 '18 at 9:21











  • I am not sure whether you expect to be able to pass in your Java Map to XSLT, I rather think that won't work unless you use Saxon 9.8 or 9.9 where there is, with the XPath 3.1 map type, a direct conversion from Java types to XSLT/Path types possible. As for the use of the str:replace template with XSLT 1, I have tried that at xsltransform.hikmatu.com/bFDb2BL, it performs the multiple replacements one an input of the search and replacement terms as an XML/XSLT type (e.g. a result tree fragment) is used.

    – Martin Honnen
    Nov 15 '18 at 9:35











  • As for using Saxon 9 and XSLT 3 with XPath 3.1 where you can directly convert a Java Map<String, String> map to an XSLT/XPath 3.1 map(xs:string, xs:string) parameter using saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/…, see xsltfiddle.liberty-development.net/eiZQaGi/0.

    – Martin Honnen
    Nov 15 '18 at 9:40











  • @MartinHonnen So after several tries, I finally used Saxon 9 and XSLT and its working well, thanks a lot !

    – Laurent
    Nov 15 '18 at 15:35

















  • If you are using Java, why are you restricted to XSLT 1.0? In the Java world Saxon 9 is available on Sourceforge and Maven to use XSLT 2 or 3. If you need to use XSLT 1, then it seems that exslt.org/str/functions/replace/str.replace.template.xsl (described at exslt.org/str/functions/replace/index.html) implements replacing various values in one go, you would just need to make sure you get your Java Map into the XSLT as a parameter. It is not clear whether you are looking for details on the Java side or on the XSLT side.

    – Martin Honnen
    Nov 14 '18 at 15:35











  • @MartinHonnen Thank you for your answer, I was trying to add as less dependencies as possible and since I work on existing code I was trying to not impact it too much, but maybe the use of XSLT 2 or 3 worth it. I need mode detail for the XSLT side, for java i just need to add the map to parameter. I will try to watch how to use Saxon 9 instead of actual xml transform. Futhermore I don't find any example on how my map can be use with str:replace with XSLT 1. Have you any hint ?

    – Laurent
    Nov 15 '18 at 9:21











  • I am not sure whether you expect to be able to pass in your Java Map to XSLT, I rather think that won't work unless you use Saxon 9.8 or 9.9 where there is, with the XPath 3.1 map type, a direct conversion from Java types to XSLT/Path types possible. As for the use of the str:replace template with XSLT 1, I have tried that at xsltransform.hikmatu.com/bFDb2BL, it performs the multiple replacements one an input of the search and replacement terms as an XML/XSLT type (e.g. a result tree fragment) is used.

    – Martin Honnen
    Nov 15 '18 at 9:35











  • As for using Saxon 9 and XSLT 3 with XPath 3.1 where you can directly convert a Java Map<String, String> map to an XSLT/XPath 3.1 map(xs:string, xs:string) parameter using saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/…, see xsltfiddle.liberty-development.net/eiZQaGi/0.

    – Martin Honnen
    Nov 15 '18 at 9:40











  • @MartinHonnen So after several tries, I finally used Saxon 9 and XSLT and its working well, thanks a lot !

    – Laurent
    Nov 15 '18 at 15:35
















If you are using Java, why are you restricted to XSLT 1.0? In the Java world Saxon 9 is available on Sourceforge and Maven to use XSLT 2 or 3. If you need to use XSLT 1, then it seems that exslt.org/str/functions/replace/str.replace.template.xsl (described at exslt.org/str/functions/replace/index.html) implements replacing various values in one go, you would just need to make sure you get your Java Map into the XSLT as a parameter. It is not clear whether you are looking for details on the Java side or on the XSLT side.

– Martin Honnen
Nov 14 '18 at 15:35





If you are using Java, why are you restricted to XSLT 1.0? In the Java world Saxon 9 is available on Sourceforge and Maven to use XSLT 2 or 3. If you need to use XSLT 1, then it seems that exslt.org/str/functions/replace/str.replace.template.xsl (described at exslt.org/str/functions/replace/index.html) implements replacing various values in one go, you would just need to make sure you get your Java Map into the XSLT as a parameter. It is not clear whether you are looking for details on the Java side or on the XSLT side.

– Martin Honnen
Nov 14 '18 at 15:35













@MartinHonnen Thank you for your answer, I was trying to add as less dependencies as possible and since I work on existing code I was trying to not impact it too much, but maybe the use of XSLT 2 or 3 worth it. I need mode detail for the XSLT side, for java i just need to add the map to parameter. I will try to watch how to use Saxon 9 instead of actual xml transform. Futhermore I don't find any example on how my map can be use with str:replace with XSLT 1. Have you any hint ?

– Laurent
Nov 15 '18 at 9:21





@MartinHonnen Thank you for your answer, I was trying to add as less dependencies as possible and since I work on existing code I was trying to not impact it too much, but maybe the use of XSLT 2 or 3 worth it. I need mode detail for the XSLT side, for java i just need to add the map to parameter. I will try to watch how to use Saxon 9 instead of actual xml transform. Futhermore I don't find any example on how my map can be use with str:replace with XSLT 1. Have you any hint ?

– Laurent
Nov 15 '18 at 9:21













I am not sure whether you expect to be able to pass in your Java Map to XSLT, I rather think that won't work unless you use Saxon 9.8 or 9.9 where there is, with the XPath 3.1 map type, a direct conversion from Java types to XSLT/Path types possible. As for the use of the str:replace template with XSLT 1, I have tried that at xsltransform.hikmatu.com/bFDb2BL, it performs the multiple replacements one an input of the search and replacement terms as an XML/XSLT type (e.g. a result tree fragment) is used.

– Martin Honnen
Nov 15 '18 at 9:35





I am not sure whether you expect to be able to pass in your Java Map to XSLT, I rather think that won't work unless you use Saxon 9.8 or 9.9 where there is, with the XPath 3.1 map type, a direct conversion from Java types to XSLT/Path types possible. As for the use of the str:replace template with XSLT 1, I have tried that at xsltransform.hikmatu.com/bFDb2BL, it performs the multiple replacements one an input of the search and replacement terms as an XML/XSLT type (e.g. a result tree fragment) is used.

– Martin Honnen
Nov 15 '18 at 9:35













As for using Saxon 9 and XSLT 3 with XPath 3.1 where you can directly convert a Java Map<String, String> map to an XSLT/XPath 3.1 map(xs:string, xs:string) parameter using saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/…, see xsltfiddle.liberty-development.net/eiZQaGi/0.

– Martin Honnen
Nov 15 '18 at 9:40





As for using Saxon 9 and XSLT 3 with XPath 3.1 where you can directly convert a Java Map<String, String> map to an XSLT/XPath 3.1 map(xs:string, xs:string) parameter using saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/…, see xsltfiddle.liberty-development.net/eiZQaGi/0.

– Martin Honnen
Nov 15 '18 at 9:40













@MartinHonnen So after several tries, I finally used Saxon 9 and XSLT and its working well, thanks a lot !

– Laurent
Nov 15 '18 at 15:35





@MartinHonnen So after several tries, I finally used Saxon 9 and XSLT and its working well, thanks a lot !

– Laurent
Nov 15 '18 at 15:35












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302693%2fjavaxslt-replace-map-values-in-all-the-xml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302693%2fjavaxslt-replace-map-values-in-all-the-xml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党