I am an admitted novice with XSLT and am looking for some direction. I have the follow (simplified) XML input data. I want to take the underlying data and apply it to AccountExternalSystemId or flatten it out.
<?xml version="1.0" ?>
<ns:CustomObject3WS_CustomObject3QueryPage_Output xmlns:ns="urn:crmondemand/ws/customobject3/10/2004">
<ns:LastPage>true</ns:LastPage>
<ListOfCustomObject3 xmlns="urn:/crmondemand/xml/customObject3">
<CustomObject3>
<AccountExternalSystemId>A000008351</AccountExternalSystemId>
<ListOfAccount>
<Account>
<AccountId>AAXA-H72YN</AccountId>
<ExternalSystemId>100000000002795</ExternalSystemId>
<Name>CATERPILLAR INC [100000000002795]</Name>
</Account>
<Account>
<AccountId>ADOA-3BAK0F</AccountId>
<ExternalSystemId>A000008351</ExternalSystemId>
<Name>CATERPILLAR</Name>
</Account>
</ListOfAccount>
</CustomObject3>
<CustomObject3>
<AccountExternalSystemId>100000000001059</AccountExternalSystemId>
<ListOfAccount>
<Account>
<AccountId>AAXA-H0B7N</AccountId>
<ExternalSystemId>100000000001059</ExternalSystemId>
<Name>SERV SA [100000000001059]</Name>
</Account>
</ListOfAccount>
</CustomObject3>
</ListOfCustomObject3>
I am applying the following XSL to the data:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/">
<xsl:output method="xm开发者_开发问答l" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="*:CustomObject3WS_CustomObject3QueryPage_Output"/>
</xsl:template>
<xsl:template match="*:CustomObject3WS_CustomObject3QueryPage_Output">
<xsl:copy>
<xsl:apply-templates select="*:LastPage"/>
<xsl:apply-templates select="*:ListOfCustomObject3"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*:ListOfCustomObject3">
<xsl:copy>
<xsl:apply-templates select="*:CustomObject3"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*:CustomObject3">
<xsl:variable select="*:AccountExternalSystemId" name="AccountExternalSystemId"/>
<xsl:copy>
<xsl:for-each select="*:ListOfAccount/*:Account">
<xsl:element name="AccountId" namespace="urn:/crmondemand/xml/customObject3"><xsl:value-of select="substring(*:AccountId,1,15)"/></xsl:element>
<xsl:element name="AccountName" namespace="urn:/crmondemand/xml/customObject3"><xsl:value-of select="substring(*:Name,1,255)"/></xsl:element>
<xsl:element name="AccountExternalSystemId" namespace="urn:/crmondemand/xml/customObject3"><xsl:value-of
select="substring($AccountExternalSystemId,1,64)"/></xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
and here is my result (you can see that CustomObject3 is not properly ended (as there should be 2) in the first example. Not sure if my approach is the best way to accomplish what I need to do:
<?xml version="1.0" encoding="UTF-8"?>
<ns:CustomObject3WS_CustomObject3QueryPage_Output>
<ns:LastPage>true</ns:LastPage>
<ListOfCustomObject3 xmlns="urn:/crmondemand/xml/customObject3">
<CustomObject3>
<AccountId>AAXA-H72YN</AccountId>
<AccountName>CATERPILLAR INC [100000000002795]</AccountName>
<AccountExternalSystemId>A000008351</AccountExternalSystemId>
<AccountId>ADOA-3BAK0F</AccountId>
<AccountName>CATERPILLAR</AccountName>
<AccountExternalSystemId>A000008351</AccountExternalSystemId>
</CustomObject3>
<CustomObject3>
<AccountId>AAXA-H0B7N</AccountId>
<AccountName>SERV SA [100000000001059]</AccountName>
<AccountExternalSystemId>100000000001059</AccountExternalSystemId>
</CustomObject3>
</ListOfCustomObject3>
The desired output would be:
<?xml version="1.0" encoding="UTF-8"?>
<ns:CustomObject3WS_CustomObject3QueryPage_Output>
<ns:LastPage>true</ns:LastPage>
<ListOfCustomObject3 xmlns="urn:/crmondemand/xml/customObject3">
<CustomObject3>
<AccountId>AAXA-H72YN</AccountId>
<AccountName>CATERPILLAR INC [100000000002795]</AccountName>
<AccountExternalSystemId>A000008351</AccountExternalSystemId>
</CustomObject3>
<CustomObject3>
<AccountId>ADOA-3BAK0F</AccountId>
<AccountName>CATERPILLAR</AccountName>
<AccountExternalSystemId>A000008351</AccountExternalSystemId>
</CustomObject3>
<CustomObject3>
<AccountId>AAXA-H0B7N</AccountId>
<AccountName>SERV SA [100000000001059]</AccountName>
<AccountExternalSystemId>100000000001059</AccountExternalSystemId>
</CustomObject3>
Remark note that the output in your question presents a prefix (ns:
) not bound to any namespace declaration.
Look at this direction:
- where
identity.xsl
is the well known Identity Transformation - default namespaces are handled in a simpler way
- produced output has the correct namespace declarations
[XSLT 2.0]
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="urn:/crmondemand/xml/customObject3"
xpath-default-namespace="urn:/crmondemand/xml/customObject3">
<xsl:output indent="yes"/>
<xsl:include href="identity.xsl"/>
<xsl:template match="CustomObject3">
<xsl:apply-templates select="ListOfAccount/Account"/>
</xsl:template>
<xsl:template match="Account">
<CustomObject3>
<xsl:apply-templates select="AccountId|Name"/>
<xsl:copy-of select="../../AccountExternalSystemId"/>
</CustomObject3>
</xsl:template>
<xsl:template match="Name">
<xsl:element name="Account{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
gives wanted output (with correct namespace declarations):
<ns:CustomObject3WS_CustomObject3QueryPage_Output xmlns:ns="urn:crmondemand/ws/customobject3/10/2004">
<ns:LastPage>true</ns:LastPage>
<ListOfCustomObject3 xmlns="urn:/crmondemand/xml/customObject3">
<CustomObject3>
<AccountId>AAXA-H72YN</AccountId>
<AccountName>CATERPILLAR INC [100000000002795]</AccountName>
<AccountExternalSystemId>A000008351</AccountExternalSystemId>
</CustomObject3>
<CustomObject3>
<AccountId>ADOA-3BAK0F</AccountId>
<AccountName>CATERPILLAR</AccountName>
<AccountExternalSystemId>A000008351</AccountExternalSystemId>
</CustomObject3>
<CustomObject3>
<AccountId>AAXA-H0B7N</AccountId>
<AccountName>SERV SA [100000000001059]</AccountName>
<AccountExternalSystemId>100000000001059</AccountExternalSystemId>
</CustomObject3>
</ListOfCustomObject3>
</ns:CustomObject3WS_CustomObject3QueryPage_Output>
精彩评论