0

I have an XML file with this structure:

<DetailTxt>
    <Text>
        <span>Some Text</span>
    </Text>
    <TextComplement Kind="Owner" MarkLbl="1">
        <ComplCaption>
            Caption 1
        </ComplCaption>
        <ComplBody>
            Body 1
        </ComplBody>
    </TextComplement>
    <Text>
        <span>More Text</span>
    </Text>
</DetailTxt>

Here is the part of the XSLT that is relevant here:

<xsl:template match="*[local-name() = 'DetailTxt']">
    <xsl:apply-templates select="*[local-name() = 'Text']"/>
</xsl:template>

<xsl:template match="*[local-name() = 'Text']">
    <item name="{local-name()}">
        <richtext>
            <par>
                <run>
                    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
                    <xsl:apply-templates/>
                    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
                </run>
            </par>
        </richtext>
    </item> 
    <item name="{local-name()}">
       <richtext>
            <par>
                <run>
                    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
                    <xsl:value-of select="concat('[', ../TextComplement/@Kind, ../TextComplement/@MarkLbl,']')" />
                    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
                </run>
            </par>
        </richtext>
    </item>
</xsl:template>

I expect the output to look like this:

<item name="Text">
    <richtext>
        <par>
            <run><![CDATA[
                <span>Some Text</span>
            </p>]]></run>
        </par>
    </richtext>
</item>
<item name="Text">
    <richtext>
        <par>
            <run><![CDATA[[Owner1]]]></run>
        </par>
    </richtext>
</item>

But the line using the TextComplement XPath looks like this:

            <run><![CDATA[[]]]></run>

All values from TextComplement are missing. Whats wrong with the XPath here?

EDIT: I completely reworked my question and put in a CONCRETE question resulting from the first answer. That kind of invalidates the first answer but IMHO improves the question.

Tode
  • 11,795
  • 18
  • 34
  • 1
    Have you tried any XSLT? If you have XSLT to produce the second part then I don't understand why the first part is a problem. Anyway, XSLT uses XPath as its expression language and concatenating to values can be done with the `concat` function `concat(@Kind, @MarkLbl)`. – Martin Honnen Apr 29 '19 at 17:09
  • Where is your code and what is the error message? – ceving Apr 30 '19 at 06:55
  • You are absolutely right: If this was a question from my area of ​​expertise, I would answer the same. But unfortunately I don't have enough skill to even find out the beginning point... But the answer helped a lot. – Tode Apr 30 '19 at 07:55

2 Answers2

1

Not sure how the XSLT looks like but you can try adding the following template with the concat() function for getting the output.

<xsl:template match="Text">
    <document version="9.0" form="Form1">
        <item name="{local-name()}">
            <xsl:copy-of select="span" />
        </item>
        <item name="{local-name()}">
            <span>
                <xsl:value-of select="concat('[', ../TextComplement/@Kind, ../TextComplement/@MarkLbl, ']')" />
            </span>
        </item>
    </document>
</xsl:template>

This template is applied to the <Text> node and the ../ is used to go up one level and then access the attributes of <TextComplement> using the XPath.

The output of the template when applied to your XML will look like.

<document form="Form1" version="9.0">
    <item name="Text">
        <span>Some Text</span>
    </item>
    <item name="Text">
        <span>[Owner1]</span>
    </item>
</document>

The same template will also get applied to the <Text> node having More Text content and produce similar output.

Aniket V
  • 3,183
  • 2
  • 15
  • 27
  • Thank you for pushing me in the right direction. At the moment the brackets in my tests are empty as if the contents could not be read from that path, but I think I can start with that! – Tode Apr 30 '19 at 08:24
  • If the brackets in your tests are empty, it would mean an incorrect XPath. The XPath in above solution is based on the input provided and may differ if you have a different input XML. – Aniket V Apr 30 '19 at 08:33
  • The input is exactly as given in the example. The only difference in my live code is the template selection: `` instead of your `` – Tode Apr 30 '19 at 08:35
  • I used your answer to rewrite my question. Sorry to partly invalidate this answer, but this already helped to get a starting point. Thank you for that! – Tode Apr 30 '19 at 09:04
  • The problem with the ancestor selector (IMHO): I can have multiple Text / TextComplement- Pairs in one DetailTxt, and I need them to stick together. – Tode Apr 30 '19 at 09:09
  • As your answer finally helped me find the solution, I accepted your answer. – Tode May 03 '19 at 11:27
-1

I found a solution myself for the concrete question. I quess this is IBM Notes / LotusScript specific issue.

When using the selector

../TextComplement/@Kind

the parser returned an empty string. I changed to

../*[local-name() = 'TextComplement']/@Kind

and later (more concrete) to:

./following-sibling::*[local-name() = 'TextComplement']/@Kind

And that worked. I personally see no difference in these notations, but it seams that internally they are handled differently.

Tode
  • 11,795
  • 18
  • 34
  • Apparently your XML is in a *namespace* (unlike the one posted in the question). The solution is to use the namespace, not to disregard it - see, for example: https://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628. In any case, this has nothing to do with the question as asked. – michael.hor257k May 03 '19 at 13:21