Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

xml - Update an existing node concatenating the ID(deeper level) before that value in XSLT

i'm trying with XSLT to update an existing node (ItemDescription) with the concatenation of InternalID(in deeper level) and the existing description.

Origin XML

<urn:ExternalReqForApprovalImportRequest xmlns:urn="urn:Ariba:Buyer:vrealm_1" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" partition="" variant="">
    <urn:ExternalReqForApprovalInput_Item>
        <urn:item>
            <urn:Name>Line 1</urn:Name>
            <urn:Operation>NEW</urn:Operation>
            <urn:ExternalReqLineItems>
                <urn:item>
                    <urn:ExternalLineNumber>2</urn:ExternalLineNumber>
                    <urn:ItemDescription>TEST DESCRIPTION 1</urn:ItemDescription>
                    <urn:Quantity>1.00</urn:Quantity>
                    <urn:LineExtrinsics>
                        <Extrinsics>
                            <Extrinsic name="InternalID">7000083</Extrinsic>
                        </Extrinsics>
                    </urn:LineExtrinsics>
                </urn:item>
            </urn:ExternalReqLineItems>
        </urn:item>
        <urn:item>
            <urn:Name>Line 2</urn:Name>
            <urn:Operation>NEW</urn:Operation>
            <urn:ExternalReqLineItems>
                <urn:item>
                    <urn:ExternalLineNumber>2</urn:ExternalLineNumber>
                    <urn:ItemDescription>TEST DESCRIPTION 2</urn:ItemDescription>
                    <urn:Quantity>1.00</urn:Quantity>
                    <urn:LineExtrinsics>
                        <Extrinsics>
                            <Extrinsic name="InternalID">7000084</Extrinsic>
                        </Extrinsics>
                    </urn:LineExtrinsics>
                </urn:item>
            </urn:ExternalReqLineItems>
        </urn:item>     
    </urn:ExternalReqForApprovalInput_Item>
</urn:ExternalReqForApprovalImportRequest>

The result should be something like this:

<urn:ExternalReqForApprovalImportRequest xmlns:urn="urn:Ariba:Buyer:vrealm_1" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" partition="" variant="">
    <urn:ExternalReqForApprovalInput_Item>
        <urn:item>
            <urn:Name>Line 1</urn:Name>
            <urn:Operation>NEW</urn:Operation>
            <urn:ExternalReqLineItems>
                <urn:item>
                    <urn:ExternalLineNumber>2</urn:ExternalLineNumber>
                    <urn:ItemDescription>7000083|TEST DESCRIPTION 1</urn:ItemDescription>
                    <urn:Quantity>1.00</urn:Quantity>
                    <urn:LineExtrinsics>
                    </urn:LineExtrinsics>
                </urn:item>
            </urn:ExternalReqLineItems>
        </urn:item>
        <urn:item>
            <urn:Name>Line 2</urn:Name>
            <urn:Operation>NEW</urn:Operation>
            <urn:ExternalReqLineItems>
                <urn:item>
                    <urn:ExternalLineNumber>2</urn:ExternalLineNumber>
                    <urn:ItemDescription>7000084|TEST DESCRIPTION 2</urn:ItemDescription>
                    <urn:Quantity>1.00</urn:Quantity>
                    <urn:LineExtrinsics>
                    </urn:LineExtrinsics>
                </urn:item>
            </urn:ExternalReqLineItems>
        </urn:item>     
    </urn:ExternalReqForApprovalInput_Item>
</urn:ExternalReqForApprovalImportRequest>

Im using the XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:p1="urn:Ariba:Buyer:vrealm_1">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

But I'm missing how to go to ItemDescription and concatenate the values, what should be the way?

Thank you.

question from:https://stackoverflow.com/questions/65924846/update-an-existing-node-concatenating-the-iddeeper-level-before-that-value-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to add a template for each element you want to modify:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:urn="urn:Ariba:Buyer:vrealm_1">
<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="urn:ItemDescription">
    <xsl:copy>
        <xsl:value-of select="../urn:LineExtrinsics/Extrinsics/Extrinsic[@name='InternalID']"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="."/>
    </xsl:copy>
</xsl:template>

<xsl:template match="urn:LineExtrinsics">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...