3

I need to perform an XML XSLT conversion. I am new to this and have read a lot of tutorials but have come across one that is a bit challenging for me and hoping someone can point me in the right direction.

I have two elements, for example:

<linum class="topic/linum secondAtt/secondVal">Blah</linum>
<linum class="topic/linum">Blah</linum>

If the linum class attribute has a second value, i must add another attribute to that element using the second value of that attribute. If it only has one then I leave it as is.

So the output for the above two the output would be:

<linum class="topic/linum secondAtt/secondVal" newAttribute="secondVal">Blah</linum>
<linum class="topic/linum">Blah</linum>

I hope I have explained this well, if not hopefully my output is clear as only the first element is transformed by adding a new attribute with the second value.

I appreciate any help I am given!

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • What exactly does "has a second value" mean? I see only a single value in both. – michael.hor257k Sep 21 '16 at 14:59
  • first element class value = "topic/linum secondAtt/secondVal" second element class value = "topic/linum" the first element has two values, topic linum and secondAtt/secondVal. the second element only has one, topic/linum. if the element has two values, i need to get the second part of the second value and add it as a new attribute to that same element as shown in my output example – HelpMeWithXSLT Sep 21 '16 at 15:00
  • Can there be a third "value"? If yes, what should the result be in such case? – michael.hor257k Sep 21 '16 at 15:18
  • In DITA every elements has its own class attribute that is unique in whole DTD or schema. So it is not a usual situation that “linum” element has two different @class attribute. If you are new to DITA processing stylesheet, it is better to refer to the DITA specification “2.5.3.6 class attribute rules and syntax” (http://docs.oasis-open.org/dita/dita/v1.3/os/part3-all-inclusive/archSpec/base/specialization-class-attribute.html#classatt). Or you should consult your specialization DTD (or schema) how class attribute is defined in it. (Because “linum” is not a standard DITA element.) – Toshihiko Makita Sep 22 '16 at 12:13
  • Also referencing element name directly in xsl:template/@match is not recommended way. You can examine how they are handled in DITA stylesheet in lot of examples in GitHub DITA-OT plug-ins (https://github.com/dita-ot/dita-ot/tree/develop/src/main/plugins). – Toshihiko Makita Sep 22 '16 at 12:13

1 Answers1

1

The following will work for your example, where the class attribute contains no more than two space-delimited values:

XSLT 1.0

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

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

<xsl:template match="linum[contains(@class, ' ')]">
    <linum class="{@class}" newAttribute="{substring-after(substring-after(@class, ' '), '/')}">
        <xsl:apply-templates/>
    </linum>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • thank you for your response michael, it is much appreciated. i will try this out today and report back when i have a chance. – HelpMeWithXSLT Sep 21 '16 at 15:25