cc0
cc0

Reputation: 1950

XSLT 1.0 - Iteratively remove character at start of text

I have an xml like this (and am restricted to using XSLT 1.0 to process it)

<Template>
    <ID>someTemplate</ID>
    <Object>
        <ID>someID</ID>
        <Name>someName</Name>
        <Association type="someAssociation">
            <Object>
                <ID>/someUniqueID</ID>
                <Context>
                    <ID>someContextID</ID>
                </Context>
                <ClassID>someClassID</ClassID>
            </Object>
        </Association>
        <Association type="someAssociation">
            <Object>
                <ID>/someUniqueID</ID>
                <Context>
                    <ID>someContextID</ID>
                </Context>
                <ClassID>someClassID</ClassID>
            </Object>
        </Association>
        <Association type="someAssociation">
            <Object>
                <ID>/someUniqueID</ID>
                <Context>
                    <ID>someContextID</ID>
                </Context>
                <ClassID>someClassID</ClassID>
            </Object>
        </Association>
    </Object>
</Template>

And I want to remove the character '/' at the start of each value in the ID nodes under the Association/Object nodes. The expected output should look like this;

    <Template>
    <ID>someTemplate</ID>
    <Object>
        <ID>someID</ID>
        <Name>someName</Name>
        <Association type="someAssociation">
            <Object>
                <ID>someUniqueID</ID>
                <Context>
                    <ID>someContextID</ID>
                </Context>
                <ClassID>someClassID</ClassID>
            </Object>
        </Association>
        <Association type="someAssociation">
            <Object>
                <ID>someUniqueID</ID>
                <Context>
                    <ID>someContextID</ID>
                </Context>
                <ClassID>someClassID</ClassID>
            </Object>
        </Association>
        <Association type="someAssociation">
            <Object>
                <ID>someUniqueID</ID>
                <Context>
                    <ID>someContextID</ID>
                </Context>
                <ClassID>someClassID</ClassID>
            </Object>
        </Association>
    </Object>
</Template>

I have been looking at the translate() function, but am struggling to find any way of implementing it.

Upvotes: 0

Views: 248

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Write a template matching those ID elements starting with a slash and then simply output the value after the slash with substring:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="ID[starts-with(., '/')]">
  <xsl:copy>
    <xsl:value-of select="substring(., 2)"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

You might want or need to adjust the match pattern to Association/Object/ID if you only want to treat these particular ID elements.

Upvotes: 1

Related Questions