尝试使用XSLT扩展数据

时间:2011-08-24 18:19:29

标签: xslt

我是XSLT的新手,我正在寻找一些方向。我有以下(简化的)XML输入数据。我想获取基础数据并将其应用于AccountExternalSystemId或将其展平。

<?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>

我将以下XSL应用于数据:

<?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="xml" 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>

这是我的结果(你可以看到第一个例子中的CustomObject3没有正确结束(因为应该有2个)。不确定我的方法是否是完成我需要做的最好方法:

    <?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>

所需的输出是:

<?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>

1 个答案:

答案 0 :(得分:0)

备注请注意,问题中的输出会向任何名称空间声明提供前缀(ns:未绑定

看看这个方向:

  • 其中identity.xsl是众所周知的Identity Transformation
  • 默认名称空间以更简单的方式处理
  • 生成的输出具有正确的命名空间声明

[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>

给出想要的输出(具有正确的命名空间声明):

<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>