XSLT, Python insert a python list of tuples on xslt attributes?











up vote
0
down vote

favorite












I have the following problem: I want to extend the img element from an XML source with the attributes 'width' and 'height' in an XSLT transformation. The values of the attributes should be read from a Python list of tuples (the list consists of 2-tuples). The number of img elements in the XML source corresponds with the number of tuples in the list.
The aim is to display the images differently in the target document of an XSL-FO transformation, depending on the size, resolution and aspect ratio (I´m using Apache FOP, can´t use Antenna).
I tried a solution with a regex replacement in Python, but can't get any further.



Here is my regex-line, after I have opened the source document an read it with read() in python.



doc = re.sub(r'(<v:imagedata.*?.jpg")', r'1 width="{value-of tuple-data}" height="{value-of tuple-data}"', str(doc))


I don't know how to insert the values in the replacing pattern. Can I do this with iterating over two lists? But regex in XML-files is not a very good idea. That doesn't seem to be the right way. I know there are Python extensions for XSLT. But this seems quite complex. Maybe my problem is easier to solve, but I don't see it. In which direction do I have to think further? I am happy about help.



EDIT



The source file is large and deeply nested (wordML).
The lines I want to transform are like:



 <v:imagedata src="bilder_web/image123.jpg"/>


And here is a sample line of the list of tuples



[(840,330),(328,301),(1087,189),(744,600),...]


where first value is width, second = height.



XSLT-Result should be:



<v:imagedata src="bilder_web/image123.jpg" width="first value-of tuple, e.g. 840" height="second value-of tuple, e.g. 330"/>


and so on.










share|improve this question




















  • 1




    Can you add more code than just the regex line? Like sample xml input, list of tuples, and sample of resulting xsl-fo?
    – Daniel Haley
    Nov 18 at 1:09






  • 1




    XSLT does not know what a "Python list of tuples" is. But it can tokenize a string passed to it as parameter, and match the position of a token in the string to the position of img element in the XML document.
    – michael.hor257k
    Nov 18 at 7:28










  • @michael.hor257k This could lead me to a solution, I think, but I did not complete understand this comment. Could you please describe it more detailed? Tokenizing is clear to me. But how would I pass the list of tuples as a string to XSLT? With a param-Attribute? And how do I match the position of the token correlating to the img element?
    – Pjoern
    Nov 18 at 9:02












  • @DanielHaley I edited my question with more samples. I provided a XSLT-snippet as sample for result (no XSL-FO, because what I need here is a prepared XML-file (with XSLT) as source for XSL-FO)
    – Pjoern
    Nov 18 at 9:10










  • You pass the parameter as part of calling the XSL transformation from your application.
    – michael.hor257k
    Nov 18 at 9:28















up vote
0
down vote

favorite












I have the following problem: I want to extend the img element from an XML source with the attributes 'width' and 'height' in an XSLT transformation. The values of the attributes should be read from a Python list of tuples (the list consists of 2-tuples). The number of img elements in the XML source corresponds with the number of tuples in the list.
The aim is to display the images differently in the target document of an XSL-FO transformation, depending on the size, resolution and aspect ratio (I´m using Apache FOP, can´t use Antenna).
I tried a solution with a regex replacement in Python, but can't get any further.



Here is my regex-line, after I have opened the source document an read it with read() in python.



doc = re.sub(r'(<v:imagedata.*?.jpg")', r'1 width="{value-of tuple-data}" height="{value-of tuple-data}"', str(doc))


I don't know how to insert the values in the replacing pattern. Can I do this with iterating over two lists? But regex in XML-files is not a very good idea. That doesn't seem to be the right way. I know there are Python extensions for XSLT. But this seems quite complex. Maybe my problem is easier to solve, but I don't see it. In which direction do I have to think further? I am happy about help.



EDIT



The source file is large and deeply nested (wordML).
The lines I want to transform are like:



 <v:imagedata src="bilder_web/image123.jpg"/>


And here is a sample line of the list of tuples



[(840,330),(328,301),(1087,189),(744,600),...]


where first value is width, second = height.



XSLT-Result should be:



<v:imagedata src="bilder_web/image123.jpg" width="first value-of tuple, e.g. 840" height="second value-of tuple, e.g. 330"/>


and so on.










share|improve this question




















  • 1




    Can you add more code than just the regex line? Like sample xml input, list of tuples, and sample of resulting xsl-fo?
    – Daniel Haley
    Nov 18 at 1:09






  • 1




    XSLT does not know what a "Python list of tuples" is. But it can tokenize a string passed to it as parameter, and match the position of a token in the string to the position of img element in the XML document.
    – michael.hor257k
    Nov 18 at 7:28










  • @michael.hor257k This could lead me to a solution, I think, but I did not complete understand this comment. Could you please describe it more detailed? Tokenizing is clear to me. But how would I pass the list of tuples as a string to XSLT? With a param-Attribute? And how do I match the position of the token correlating to the img element?
    – Pjoern
    Nov 18 at 9:02












  • @DanielHaley I edited my question with more samples. I provided a XSLT-snippet as sample for result (no XSL-FO, because what I need here is a prepared XML-file (with XSLT) as source for XSL-FO)
    – Pjoern
    Nov 18 at 9:10










  • You pass the parameter as part of calling the XSL transformation from your application.
    – michael.hor257k
    Nov 18 at 9:28













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following problem: I want to extend the img element from an XML source with the attributes 'width' and 'height' in an XSLT transformation. The values of the attributes should be read from a Python list of tuples (the list consists of 2-tuples). The number of img elements in the XML source corresponds with the number of tuples in the list.
The aim is to display the images differently in the target document of an XSL-FO transformation, depending on the size, resolution and aspect ratio (I´m using Apache FOP, can´t use Antenna).
I tried a solution with a regex replacement in Python, but can't get any further.



Here is my regex-line, after I have opened the source document an read it with read() in python.



doc = re.sub(r'(<v:imagedata.*?.jpg")', r'1 width="{value-of tuple-data}" height="{value-of tuple-data}"', str(doc))


I don't know how to insert the values in the replacing pattern. Can I do this with iterating over two lists? But regex in XML-files is not a very good idea. That doesn't seem to be the right way. I know there are Python extensions for XSLT. But this seems quite complex. Maybe my problem is easier to solve, but I don't see it. In which direction do I have to think further? I am happy about help.



EDIT



The source file is large and deeply nested (wordML).
The lines I want to transform are like:



 <v:imagedata src="bilder_web/image123.jpg"/>


And here is a sample line of the list of tuples



[(840,330),(328,301),(1087,189),(744,600),...]


where first value is width, second = height.



XSLT-Result should be:



<v:imagedata src="bilder_web/image123.jpg" width="first value-of tuple, e.g. 840" height="second value-of tuple, e.g. 330"/>


and so on.










share|improve this question















I have the following problem: I want to extend the img element from an XML source with the attributes 'width' and 'height' in an XSLT transformation. The values of the attributes should be read from a Python list of tuples (the list consists of 2-tuples). The number of img elements in the XML source corresponds with the number of tuples in the list.
The aim is to display the images differently in the target document of an XSL-FO transformation, depending on the size, resolution and aspect ratio (I´m using Apache FOP, can´t use Antenna).
I tried a solution with a regex replacement in Python, but can't get any further.



Here is my regex-line, after I have opened the source document an read it with read() in python.



doc = re.sub(r'(<v:imagedata.*?.jpg")', r'1 width="{value-of tuple-data}" height="{value-of tuple-data}"', str(doc))


I don't know how to insert the values in the replacing pattern. Can I do this with iterating over two lists? But regex in XML-files is not a very good idea. That doesn't seem to be the right way. I know there are Python extensions for XSLT. But this seems quite complex. Maybe my problem is easier to solve, but I don't see it. In which direction do I have to think further? I am happy about help.



EDIT



The source file is large and deeply nested (wordML).
The lines I want to transform are like:



 <v:imagedata src="bilder_web/image123.jpg"/>


And here is a sample line of the list of tuples



[(840,330),(328,301),(1087,189),(744,600),...]


where first value is width, second = height.



XSLT-Result should be:



<v:imagedata src="bilder_web/image123.jpg" width="first value-of tuple, e.g. 840" height="second value-of tuple, e.g. 330"/>


and so on.







python xml xslt attributes tuples






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 8:50

























asked Nov 18 at 0:31









Pjoern

8116




8116








  • 1




    Can you add more code than just the regex line? Like sample xml input, list of tuples, and sample of resulting xsl-fo?
    – Daniel Haley
    Nov 18 at 1:09






  • 1




    XSLT does not know what a "Python list of tuples" is. But it can tokenize a string passed to it as parameter, and match the position of a token in the string to the position of img element in the XML document.
    – michael.hor257k
    Nov 18 at 7:28










  • @michael.hor257k This could lead me to a solution, I think, but I did not complete understand this comment. Could you please describe it more detailed? Tokenizing is clear to me. But how would I pass the list of tuples as a string to XSLT? With a param-Attribute? And how do I match the position of the token correlating to the img element?
    – Pjoern
    Nov 18 at 9:02












  • @DanielHaley I edited my question with more samples. I provided a XSLT-snippet as sample for result (no XSL-FO, because what I need here is a prepared XML-file (with XSLT) as source for XSL-FO)
    – Pjoern
    Nov 18 at 9:10










  • You pass the parameter as part of calling the XSL transformation from your application.
    – michael.hor257k
    Nov 18 at 9:28














  • 1




    Can you add more code than just the regex line? Like sample xml input, list of tuples, and sample of resulting xsl-fo?
    – Daniel Haley
    Nov 18 at 1:09






  • 1




    XSLT does not know what a "Python list of tuples" is. But it can tokenize a string passed to it as parameter, and match the position of a token in the string to the position of img element in the XML document.
    – michael.hor257k
    Nov 18 at 7:28










  • @michael.hor257k This could lead me to a solution, I think, but I did not complete understand this comment. Could you please describe it more detailed? Tokenizing is clear to me. But how would I pass the list of tuples as a string to XSLT? With a param-Attribute? And how do I match the position of the token correlating to the img element?
    – Pjoern
    Nov 18 at 9:02












  • @DanielHaley I edited my question with more samples. I provided a XSLT-snippet as sample for result (no XSL-FO, because what I need here is a prepared XML-file (with XSLT) as source for XSL-FO)
    – Pjoern
    Nov 18 at 9:10










  • You pass the parameter as part of calling the XSL transformation from your application.
    – michael.hor257k
    Nov 18 at 9:28








1




1




Can you add more code than just the regex line? Like sample xml input, list of tuples, and sample of resulting xsl-fo?
– Daniel Haley
Nov 18 at 1:09




Can you add more code than just the regex line? Like sample xml input, list of tuples, and sample of resulting xsl-fo?
– Daniel Haley
Nov 18 at 1:09




1




1




XSLT does not know what a "Python list of tuples" is. But it can tokenize a string passed to it as parameter, and match the position of a token in the string to the position of img element in the XML document.
– michael.hor257k
Nov 18 at 7:28




XSLT does not know what a "Python list of tuples" is. But it can tokenize a string passed to it as parameter, and match the position of a token in the string to the position of img element in the XML document.
– michael.hor257k
Nov 18 at 7:28












@michael.hor257k This could lead me to a solution, I think, but I did not complete understand this comment. Could you please describe it more detailed? Tokenizing is clear to me. But how would I pass the list of tuples as a string to XSLT? With a param-Attribute? And how do I match the position of the token correlating to the img element?
– Pjoern
Nov 18 at 9:02






@michael.hor257k This could lead me to a solution, I think, but I did not complete understand this comment. Could you please describe it more detailed? Tokenizing is clear to me. But how would I pass the list of tuples as a string to XSLT? With a param-Attribute? And how do I match the position of the token correlating to the img element?
– Pjoern
Nov 18 at 9:02














@DanielHaley I edited my question with more samples. I provided a XSLT-snippet as sample for result (no XSL-FO, because what I need here is a prepared XML-file (with XSLT) as source for XSL-FO)
– Pjoern
Nov 18 at 9:10




@DanielHaley I edited my question with more samples. I provided a XSLT-snippet as sample for result (no XSL-FO, because what I need here is a prepared XML-file (with XSLT) as source for XSL-FO)
– Pjoern
Nov 18 at 9:10












You pass the parameter as part of calling the XSL transformation from your application.
– michael.hor257k
Nov 18 at 9:28




You pass the parameter as part of calling the XSL transformation from your application.
– michael.hor257k
Nov 18 at 9:28












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Consider the following simplified example:



XML



<root>
<chapter id="1">
<imagedata src="a.jpg"/>
<para>
<imagedata src="b.jpg"/>
<imagedata src="c.jpg"/>
</para>
<para>
<imagedata src="d.jpg"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg"/>
<imagedata src="f.jpg"/>
</para>
<imagedata src="g.jpg"/>
</chapter>
</root>


XSLT 1.0 (+ assuming support for EXSLT str:tokenize() extension function)



<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="image-dimensions-string"></xsl:param>
<xsl:variable name="image-dimensions" select="str:tokenize($image-dimensions-string, ';')" />

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

<xsl:template match="imagedata">
<xsl:variable name="i" select="count(preceding::imagedata) + 1" />
<xsl:variable name="dimensions" select="str:tokenize($image-dimensions[$i], ',')" />
<imagedata src="{@src}" width="{$dimensions[1]}" height="{$dimensions[2]}"/>
</xsl:template>

</xsl:stylesheet>


When this transformation is called with a string parameter named image-dimensions-string containing:



100,150;200,250;300,350;400,450;500,550;600,650;700,750


the result will be:



Result



<?xml version="1.0" encoding="UTF-8"?>
<root>
<chapter id="1">
<imagedata src="a.jpg" width="100" height="150"/>
<para>
<imagedata src="b.jpg" width="200" height="250"/>
<imagedata src="c.jpg" width="300" height="350"/>
</para>
<para>
<imagedata src="d.jpg" width="400" height="450"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg" width="500" height="550"/>
<imagedata src="f.jpg" width="600" height="650"/>
</para>
<imagedata src="g.jpg" width="700" height="750"/>
</chapter>
</root>





share|improve this answer























  • Perfect! The transformation worked right away. I only had to switch from Saxon to Xalan as XSLT processor because Saxon (PE, EE) does not support the str.tokenize function of EXSLT as far as I can see.
    – Pjoern
    Nov 18 at 20:58








  • 2




    Saxon supports XSLT 2.0 which has a native tokenize() function. I assumed you were using libxslt because of the Python tag.
    – michael.hor257k
    Nov 18 at 22:06










  • Ok, didn´t know that. So I can switch back to Saxon.
    – Pjoern
    Nov 18 at 22:40











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',
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%2f53356855%2fxslt-python-insert-a-python-list-of-tuples-on-xslt-attributes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Consider the following simplified example:



XML



<root>
<chapter id="1">
<imagedata src="a.jpg"/>
<para>
<imagedata src="b.jpg"/>
<imagedata src="c.jpg"/>
</para>
<para>
<imagedata src="d.jpg"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg"/>
<imagedata src="f.jpg"/>
</para>
<imagedata src="g.jpg"/>
</chapter>
</root>


XSLT 1.0 (+ assuming support for EXSLT str:tokenize() extension function)



<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="image-dimensions-string"></xsl:param>
<xsl:variable name="image-dimensions" select="str:tokenize($image-dimensions-string, ';')" />

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

<xsl:template match="imagedata">
<xsl:variable name="i" select="count(preceding::imagedata) + 1" />
<xsl:variable name="dimensions" select="str:tokenize($image-dimensions[$i], ',')" />
<imagedata src="{@src}" width="{$dimensions[1]}" height="{$dimensions[2]}"/>
</xsl:template>

</xsl:stylesheet>


When this transformation is called with a string parameter named image-dimensions-string containing:



100,150;200,250;300,350;400,450;500,550;600,650;700,750


the result will be:



Result



<?xml version="1.0" encoding="UTF-8"?>
<root>
<chapter id="1">
<imagedata src="a.jpg" width="100" height="150"/>
<para>
<imagedata src="b.jpg" width="200" height="250"/>
<imagedata src="c.jpg" width="300" height="350"/>
</para>
<para>
<imagedata src="d.jpg" width="400" height="450"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg" width="500" height="550"/>
<imagedata src="f.jpg" width="600" height="650"/>
</para>
<imagedata src="g.jpg" width="700" height="750"/>
</chapter>
</root>





share|improve this answer























  • Perfect! The transformation worked right away. I only had to switch from Saxon to Xalan as XSLT processor because Saxon (PE, EE) does not support the str.tokenize function of EXSLT as far as I can see.
    – Pjoern
    Nov 18 at 20:58








  • 2




    Saxon supports XSLT 2.0 which has a native tokenize() function. I assumed you were using libxslt because of the Python tag.
    – michael.hor257k
    Nov 18 at 22:06










  • Ok, didn´t know that. So I can switch back to Saxon.
    – Pjoern
    Nov 18 at 22:40















up vote
1
down vote



accepted










Consider the following simplified example:



XML



<root>
<chapter id="1">
<imagedata src="a.jpg"/>
<para>
<imagedata src="b.jpg"/>
<imagedata src="c.jpg"/>
</para>
<para>
<imagedata src="d.jpg"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg"/>
<imagedata src="f.jpg"/>
</para>
<imagedata src="g.jpg"/>
</chapter>
</root>


XSLT 1.0 (+ assuming support for EXSLT str:tokenize() extension function)



<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="image-dimensions-string"></xsl:param>
<xsl:variable name="image-dimensions" select="str:tokenize($image-dimensions-string, ';')" />

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

<xsl:template match="imagedata">
<xsl:variable name="i" select="count(preceding::imagedata) + 1" />
<xsl:variable name="dimensions" select="str:tokenize($image-dimensions[$i], ',')" />
<imagedata src="{@src}" width="{$dimensions[1]}" height="{$dimensions[2]}"/>
</xsl:template>

</xsl:stylesheet>


When this transformation is called with a string parameter named image-dimensions-string containing:



100,150;200,250;300,350;400,450;500,550;600,650;700,750


the result will be:



Result



<?xml version="1.0" encoding="UTF-8"?>
<root>
<chapter id="1">
<imagedata src="a.jpg" width="100" height="150"/>
<para>
<imagedata src="b.jpg" width="200" height="250"/>
<imagedata src="c.jpg" width="300" height="350"/>
</para>
<para>
<imagedata src="d.jpg" width="400" height="450"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg" width="500" height="550"/>
<imagedata src="f.jpg" width="600" height="650"/>
</para>
<imagedata src="g.jpg" width="700" height="750"/>
</chapter>
</root>





share|improve this answer























  • Perfect! The transformation worked right away. I only had to switch from Saxon to Xalan as XSLT processor because Saxon (PE, EE) does not support the str.tokenize function of EXSLT as far as I can see.
    – Pjoern
    Nov 18 at 20:58








  • 2




    Saxon supports XSLT 2.0 which has a native tokenize() function. I assumed you were using libxslt because of the Python tag.
    – michael.hor257k
    Nov 18 at 22:06










  • Ok, didn´t know that. So I can switch back to Saxon.
    – Pjoern
    Nov 18 at 22:40













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Consider the following simplified example:



XML



<root>
<chapter id="1">
<imagedata src="a.jpg"/>
<para>
<imagedata src="b.jpg"/>
<imagedata src="c.jpg"/>
</para>
<para>
<imagedata src="d.jpg"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg"/>
<imagedata src="f.jpg"/>
</para>
<imagedata src="g.jpg"/>
</chapter>
</root>


XSLT 1.0 (+ assuming support for EXSLT str:tokenize() extension function)



<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="image-dimensions-string"></xsl:param>
<xsl:variable name="image-dimensions" select="str:tokenize($image-dimensions-string, ';')" />

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

<xsl:template match="imagedata">
<xsl:variable name="i" select="count(preceding::imagedata) + 1" />
<xsl:variable name="dimensions" select="str:tokenize($image-dimensions[$i], ',')" />
<imagedata src="{@src}" width="{$dimensions[1]}" height="{$dimensions[2]}"/>
</xsl:template>

</xsl:stylesheet>


When this transformation is called with a string parameter named image-dimensions-string containing:



100,150;200,250;300,350;400,450;500,550;600,650;700,750


the result will be:



Result



<?xml version="1.0" encoding="UTF-8"?>
<root>
<chapter id="1">
<imagedata src="a.jpg" width="100" height="150"/>
<para>
<imagedata src="b.jpg" width="200" height="250"/>
<imagedata src="c.jpg" width="300" height="350"/>
</para>
<para>
<imagedata src="d.jpg" width="400" height="450"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg" width="500" height="550"/>
<imagedata src="f.jpg" width="600" height="650"/>
</para>
<imagedata src="g.jpg" width="700" height="750"/>
</chapter>
</root>





share|improve this answer














Consider the following simplified example:



XML



<root>
<chapter id="1">
<imagedata src="a.jpg"/>
<para>
<imagedata src="b.jpg"/>
<imagedata src="c.jpg"/>
</para>
<para>
<imagedata src="d.jpg"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg"/>
<imagedata src="f.jpg"/>
</para>
<imagedata src="g.jpg"/>
</chapter>
</root>


XSLT 1.0 (+ assuming support for EXSLT str:tokenize() extension function)



<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="image-dimensions-string"></xsl:param>
<xsl:variable name="image-dimensions" select="str:tokenize($image-dimensions-string, ';')" />

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

<xsl:template match="imagedata">
<xsl:variable name="i" select="count(preceding::imagedata) + 1" />
<xsl:variable name="dimensions" select="str:tokenize($image-dimensions[$i], ',')" />
<imagedata src="{@src}" width="{$dimensions[1]}" height="{$dimensions[2]}"/>
</xsl:template>

</xsl:stylesheet>


When this transformation is called with a string parameter named image-dimensions-string containing:



100,150;200,250;300,350;400,450;500,550;600,650;700,750


the result will be:



Result



<?xml version="1.0" encoding="UTF-8"?>
<root>
<chapter id="1">
<imagedata src="a.jpg" width="100" height="150"/>
<para>
<imagedata src="b.jpg" width="200" height="250"/>
<imagedata src="c.jpg" width="300" height="350"/>
</para>
<para>
<imagedata src="d.jpg" width="400" height="450"/>
</para>
</chapter>
<chapter id="2">
<para>
<imagedata src="e.jpg" width="500" height="550"/>
<imagedata src="f.jpg" width="600" height="650"/>
</para>
<imagedata src="g.jpg" width="700" height="750"/>
</chapter>
</root>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 9:57

























answered Nov 18 at 9:26









michael.hor257k

72.4k42234




72.4k42234












  • Perfect! The transformation worked right away. I only had to switch from Saxon to Xalan as XSLT processor because Saxon (PE, EE) does not support the str.tokenize function of EXSLT as far as I can see.
    – Pjoern
    Nov 18 at 20:58








  • 2




    Saxon supports XSLT 2.0 which has a native tokenize() function. I assumed you were using libxslt because of the Python tag.
    – michael.hor257k
    Nov 18 at 22:06










  • Ok, didn´t know that. So I can switch back to Saxon.
    – Pjoern
    Nov 18 at 22:40


















  • Perfect! The transformation worked right away. I only had to switch from Saxon to Xalan as XSLT processor because Saxon (PE, EE) does not support the str.tokenize function of EXSLT as far as I can see.
    – Pjoern
    Nov 18 at 20:58








  • 2




    Saxon supports XSLT 2.0 which has a native tokenize() function. I assumed you were using libxslt because of the Python tag.
    – michael.hor257k
    Nov 18 at 22:06










  • Ok, didn´t know that. So I can switch back to Saxon.
    – Pjoern
    Nov 18 at 22:40
















Perfect! The transformation worked right away. I only had to switch from Saxon to Xalan as XSLT processor because Saxon (PE, EE) does not support the str.tokenize function of EXSLT as far as I can see.
– Pjoern
Nov 18 at 20:58






Perfect! The transformation worked right away. I only had to switch from Saxon to Xalan as XSLT processor because Saxon (PE, EE) does not support the str.tokenize function of EXSLT as far as I can see.
– Pjoern
Nov 18 at 20:58






2




2




Saxon supports XSLT 2.0 which has a native tokenize() function. I assumed you were using libxslt because of the Python tag.
– michael.hor257k
Nov 18 at 22:06




Saxon supports XSLT 2.0 which has a native tokenize() function. I assumed you were using libxslt because of the Python tag.
– michael.hor257k
Nov 18 at 22:06












Ok, didn´t know that. So I can switch back to Saxon.
– Pjoern
Nov 18 at 22:40




Ok, didn´t know that. So I can switch back to Saxon.
– Pjoern
Nov 18 at 22:40


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53356855%2fxslt-python-insert-a-python-list-of-tuples-on-xslt-attributes%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

Costa Masnaga

Fotorealismo

Sidney Franklin