0
votes

I am a beginner to xslt and would very much appreciate your help for the below-mentioned issue. Inside node <case>, I want to select <s> nodes between its first <beginning> and first <end> sibling nodes , the second <beginning> and the second <end> and respectively the nth beginning and the nth node; and add the attribute @attribute:unsolved to them. Meanwhile, I want to keep the rest of the xml file intact. My input and desired output are shown below. Thanks a lot for your help.

XML INPUT:

<content>
    <issue>1</issue>
    <status>to be solved</status>
</content> 
<case>
    <beginning>problem</beginning>
    <s>this</s>
    <s>is</s>
    <s>a</s>
    <s>problem</s>
    <end>problem</end>
    <s>no</s>
    <s>problem</s>
    <s>here</s>
    <s>.</s>
    <beginning>problem</beginning>
    <s>problem</s>
    <s>again</s>
    <end>problem</end>
    <s>no</s>
    <s>issue</s>
</case>

Desired output:

<content>
    <issue>1</issue>
    <status>to be solved</status>
</content>
<case>
    <beginning>problem</beginning>
    <s attribute='unsolved'>this</s>
    <s attribute='unsolved'>is</s>
    <s attribute='unsolved'>a</s>
    <s attribute='unsolved'>problem</s>
    <end>problem</end>
    <s>no</s>
    <s>problem</s>
    <s>here</s>
    <s>.</s>
    <beginning>problem</beginning>
    <s attribute='unsolved'>problem</s>
    <s attribute='unsolved'>again</s>
    <end>problem</end>
    <s>no</s>
    <s>issue</s>
</case>
1
Can you share the code what you have tried. What programming language are you using to achieve this?Hussain Patel
I'm trying to write an XSLT stylesheet for this transformation from one XML file to another XML file.Lena S
Assuming you can use XSLT 2.0 or 3.0 look at any example using xsl:for-each-group group-starting-with="beginning", the spec has w3.org/TR/xslt-30/#grouping-examples.Martin Honnen
Does all <s> tags have the attribute as 'unsolved'?DMC19
no! I just want to add the attribute unsolved to the nodes which are between <beginning> and <end> nodes. The rest of <s> nodes remain unchanged.Lena S

1 Answers

0
votes

If I understand what you mean, then your XPath will be show like:

`//beginning/following::s[./following::end]
[
count(.|//end/following::s[./following::beginning])
!=
count(//end/following::s[./following::beginning])
]`

Please see this is picture