Toshi Mario
Toshi Mario

Reputation: 3

Sending value with different tag XSLT

is there any chance can sending value to output other node response, with different path from there input request. i have input and response tag like this

Input Request:

<Root>
    <Items>
        <Item1>Rambutan12</Item1>
    </Items>
</Root>

and i try with this code for add new node with additional info at Response

i try like this

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="text"/>
    
    <xsl:template match="/">
        <Root>
            <ItemsResponse>
                <xsl:call-templates select="items">
                    <xsl:with-param name="item1s" select="//Root/Items/Item1"/>
                </xsl:call-templates>
            </ItemsResponse>
        </Root>
    </xst:template>
    
    <xsl:template name="items">
        <xsl:param name="item1s"/>
        <xsl:variable name="information">
            <xsl:choose>
                <xsl:when test="fn:matches($item1s, '^[a-zA-Z]*$') ">
                    <Item1><xsl:value-of select="$item1s"/></Item1>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy>
                        <Item1><xsl:value-of select="$item1s"/></Item1>
                        <xsl:apply-templates select="Item1Information"/>
                    </xsl:copy>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
    </xsl:template>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Item1Information">
        <xsl:copy>
            <Item1Information>Wrong Failed Format Input</Item1Information>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Expected Result was like this

<Root>
    <ItemsResponse>
        <Item1>Rambutan12</Item1>
        <Item1Information>Input Failed Format</ItemInformation>
    </ItemsResponse>
</Root>

Any tips like for this case, thanks

Upvotes: 0

Views: 41

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117175

It is very difficult to understand what your question is.

On the off-chance that I am guessing correctly, and that you want to add an error warning when Item1 contains any characters other than the 26 letters of the English alphabet, then you could do simply:

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:template match="/Root">
    <Root>
        <ItemsResponse>
            <xsl:copy-of select="Items/Item1"/>
            <xsl:if test="translate(Items/Item1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', '')">
                <Item1Information>Input Failed Format</Item1Information>
            </xsl:if>
        </ItemsResponse>
    </Root>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Related Questions