I have an XML to which i need to apply a XSL to transform it to another XML with different schema version.
The two versions of schema is V1 and V2. In my schema version V1 the maximum instances allowed for ID was
2 but in version 2 of schema it was changed to 3. Now i am downgrading an XML which is in version 2 to an
XML in version 1, so I want to keep only 2 instances of ID type in my final XML.
Will appreciate if anyone can let me know how can we do that in XSLT.
In put XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<tns:send xmlns:tns="http://www.test.com/Service/v3">
<NS2:Message release="006" version="010" xmlns:NS2="http://www.ncpdp.org/schema/SCRIPT">
<NS2:Body>
<NS2:New>
<NS2:Pharmacy>
<NS2:Identification>
<NS2:ID>
<NS2:IDValue>01017</NS2:IDValue>
<NS2:IDQualifier>94</NS2:IDQualifier>
<NS2:IDRegion>SCA</NS2:IDRegion>
<NS2:IDState>CA</NS2:IDState>
</NS2:ID>
<NS2:ID>
<NS2:IDValue>01018</NS2:IDValue>
<NS2:IDQualifier>95</NS2:IDQualifier>
<NS2:IDRegion>SCA</NS2:IDRegion>
<NS2:IDState>CA</NS2:IDState>
</NS2:ID>
<NS2:ID>
<NS2:IDValue>01019</NS2:IDValue>
<NS2:IDQualifier>96</NS2:IDQualifier>
<NS2:IDRegion>SCA</NS2:IDRegion>
<NS2:IDState>CA</NS2:IDState>
</NS2:ID>
</NS2:Identification>
</NS2:Pharmacy>
</NS2:New>
</NS2:Body>
</NS2:Message>
</tns:send>
</soapenv:Body>
</soapenv:Envelope>
XSLT :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tns="http://www.test.com/Service/v3"
xmlns:NS2="http://www.ncpdp.org/schema/SCRIPT"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tns:*">
<xsl:element name="{name()}" namespace="http://www.test.com/Service/v1">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Desired Output:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<tns:send xmlns:tns="http://www.test.com/Service/v1">
<NS2:Message xmlns:NS2="http://www.ncpdp.org/schema/SCRIPT" release="006" version="010">
<NS2:Body>
<NS2:New>
<NS2:Pharmacy>
<NS2:Identification>
<NS2:ID>
<NS2:IDValue>01017</NS2:IDValue>
<NS2:IDQualifier>94</NS2:IDQualifier>
<NS2:IDRegion>SCA</NS2:IDRegion>
<NS2:IDState>CA</NS2:IDState>
</NS2:ID>
<NS2:ID>
<NS2:IDValue>01018</NS2:IDValue>
<NS2:IDQualifier>95</NS2:IDQualifier>
<NS2:IDRegion>SCA</NS2:IDRegion>
<NS2:IDState>CA</NS2:IDState>
</NS2:ID>
</NS2:Identification>
</NS2:Pharmacy>
</NS2:New>
</NS2:Identification>
</NS2:Pharmacy>
</NS2:New>
</NS2:Body>
</NS2:Message>
</tns:send>
</soapenv:Body>
</soapenv:Envelope>
Second variation which i expect after applying XSLT is to trim a string. The ID instance has IDRegion as 3 characters, i want to trim it to 2 characters “SC” after the XSLT is applied.
So output something like
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<tns:send xmlns:tns="http://www.test.com/Service/v1">
<NS2:Message xmlns:NS2="http://www.ncpdp.org/schema/SCRIPT" release="006" version="010">
<NS2:Body>
<NS2:New>
<NS2:Pharmacy>
<NS2:Identification>
<NS2:ID>
<NS2:IDValue>01017</NS2:IDValue>
<NS2:IDQualifier>94</NS2:IDQualifier>
<NS2:IDRegion>SC</NS2:IDRegion>
<NS2:IDState>CA</NS2:IDState>
</NS2:ID>
<NS2:ID>
<NS2:IDValue>01018</NS2:IDValue>
<NS2:IDQualifier>95</NS2:IDQualifier>
<NS2:IDRegion>SC</NS2:IDRegion>
<NS2:IDState>CA</NS2:IDState>
</NS2:ID>
</NS2:Identification>
</NS2:Pharmacy>
</NS2:New>
</NS2:Identification>
</NS2:Pharmacy>
</NS2:New>
</NS2:Body>
</NS2:Message>
</tns:send>
</soapenv:Body>
</soapenv:Envelope>
This transformation (both XSLT 1.0 and XSLT 2.0):
when applied to the provided XML document:
produces the wanted, correct result:
Explanation:
Proper use and overriding of the identity rule.
UPDATE:
In a comment the OP has added a new requirement: each
NS2:IDRegionhas to be truncated to two characters.Here is the updated transformation that implements the new requirement:
When this transformation is applied on the same XML document (above), the wanted, correct result is produced: