Page 1 of 1

Insert/Update text in a tag using xsl

Posted: Fri Jan 31, 2020 9:01 pm
by stran
Hi,

I have an empty tag (not a combobox)

Code: Select all

<publishername></publishername>
I want to update the text inside of this tag depending on the value of a combobox publisher code

configuration xml file:

Code: Select all

<match elementName="publishername" editable="true">
	<xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
</match>
xsl file:

Code: Select all

<xsl:template match="/">
        <xsl:variable name="propertyElement"
            select="saxon:eval(saxon:expression($contextElementXPathExpression, ./*))"/>
        <xsl:choose>
        	<xsl:when test="$propertyElement/name() = 'publishername'">
                	<xsl:call-template name="getPublisherName"/>
            	</xsl:when>
        </xsl:choose>
</xsl:template>

<xsl:template name="getPublisherName">
         <xsl:variable name="linkedCode"
            select="saxon:eval(saxon:expression(concat($contextElementXPathExpression,'/../meta.caij/md.publisher.code'), ./*))"/>
          <xsl:if test="upper-case($linkedCode) = 'BELANGER'">
                Bélanger Sauvé
           </xsl:if>
</xsl:template>
So if $linkedCode is BELANGER, I want this :

Code: Select all

<publishername>Bélanger Sauvé</publishername>
How would I do that?

Thanks

Re: Insert/Update text in a tag using xsl

Posted: Wed Feb 05, 2020 9:26 am
by alex_jitianu
Hi,

It looks like you are pretty close to your goal. Here's an XSLT variant that I've tested on an XML instance like the one below. Let me know if there is anything missing. Please note that you can use xsl:message to add logging information that can help you understand why things don't go as planned.

Code: Select all

<test_cc>
    <meta.caij>
        <md.publisher.code>BELANGER</md.publisher.code>    
    </meta.caij>
    <publishername></publishername>
</test_cc>

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    xmlns:saxon="http://saxon.sf.net/"
    version="2.0">
    
    <xsl:param name="documentSystemID" as="xs:string"></xsl:param>
    <xsl:param name="contextElementXPathExpression" as="xs:string"></xsl:param>
    
    <xsl:template name="start">
        <xsl:apply-templates select="doc($documentSystemID)"/>
    </xsl:template>
    
    <xsl:template match="/">
        <xsl:variable name="propertyElement"
            select="saxon:eval(saxon:expression($contextElementXPathExpression, ./*))"/>
        
        <!--<xsl:message select="local-name($propertyElement)"></xsl:message>-->
        
        <xsl:apply-templates select="$propertyElement"/>
    </xsl:template>
    
    <xsl:template match="publishername">
        <!-- CC was invoked on element publishername -->
        
        <xsl:variable name="linkedCode" select="../meta.caij/md.publisher.code"/>
        <xsl:if test="upper-case($linkedCode) = 'BELANGER'">
            <items action="replace">
                <item value="Bélanger Sauvé"/>
            </items>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Re: Insert/Update text in a tag using xsl

Posted: Wed Feb 05, 2020 7:23 pm
by stran
It didn't work.

Here's the xslt:

Code: Select all

<xsl:template match="/">
        <xsl:variable name="propertyElement"
            select="saxon:eval(saxon:expression($contextElementXPathExpression, ./*))"/>
        <xsl:choose>
            <xsl:when test="$propertyElement/name() = 'md.type'">
                <xsl:call-template name="getTypeValues"/>
            </xsl:when>
            <xsl:when test="$propertyElement/name() = 'md.host.id'">
                <xsl:call-template name="getHostIdValues"/>
            </xsl:when>
            <xsl:when test="$propertyElement/name() = 'md.host.title'">
                <xsl:call-template name="getHostTitleValues"/>
            </xsl:when>
            <xsl:when test="$propertyElement/name() = 'md.publisher.code'">
                <xsl:call-template name="getPublisherCodeValues"/>
            </xsl:when>
            <xsl:when test="$propertyElement/name() = 'md.primary'">
                <xsl:call-template name="getPrimaryValues"/>
            </xsl:when>
            <xsl:when test="$propertyElement/name() = 'md.secondary'">
                <xsl:call-template name="getSecondaryValues"/>
            </xsl:when>
        </xsl:choose>
        <xsl:if test="$propertyElement/name() = 'publishername'">
            <xsl:message>Out</xsl:message>
        </xsl:if>
    </xsl:template>
The other elements are comboboxes. I have a configuration file for those elements but it doesn't work with work with publishername since it is just a display:inline.

Code: Select all

<match elementName="md.type" editable="onlyAllowedItems">
        <xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
    </match>
    
    <match elementName="md.host.id" editable="onlyAllowedItems">
        <xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
    </match>
    
    <match elementName="md.host.title" editable="onlyAllowedItems">
        <xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
    </match>
    
    <match elementName="md.publisher.code" editable="onlyAllowedItems">
        <xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
    </match>

    <match elementName="md.primary" editable="onlyAllowedItems">
        <xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
    </match>

    <match elementName="md.secondary" editable="onlyAllowedItems">
        <xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
    </match>

    <match elementName="publishername" editable="true">
        <xslt href="getMetaCaijValues.xsl" useCache="false" action="replace"/>
    </match>
In the xslt, it doesn't go even once in the if.

Re: Insert/Update text in a tag using xsl

Posted: Thu Feb 06, 2020 10:12 am
by alex_jitianu
Hi,

In the XSLT, do you also have a template named "start"? it is the entry point.
The other elements are comboboxes. I have a configuration file for those elements but it doesn't work with work with publishername since it is just a display:inline.
So for the other elements, which use combo boxes form controls, the proposed values are presented correctly? The CSS doesn't really matter... The configuration file works at a low level, in every place where proposals are needed, those values are contributed. For example, even if you go into the text page, if you place the caret inside the element and press CTRL + Space, the values are presented.

Can you send me the XSLT, cc_config.xml and an XML instance on support@oxygenxml.com ? This way I can put them together and understand what's happening. Afterwards, I will remove them from my system.


Best regards,
Alex