首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用XSLT扩展数据

尝试使用XSLT扩展数据
EN

Stack Overflow用户
提问于 2011-08-25 02:19:29
回答 1查看 71关注 0票数 1

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

代码语言:javascript
复制
<?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应用于数据:

代码语言:javascript
复制
<?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)。不确定我的方法是否是完成我需要做的事情的最佳方式:

代码语言:javascript
复制
    <?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>

期望的输出将是:

代码语言:javascript
复制
<?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>

EN

回答 1

Stack Overflow用户

发布于 2011-08-25 03:14:54

备注请注意,问题中的输出显示了一个前缀(ns:) 未绑定到任何名称空间声明。

看看这个方向:

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

XSLT 2.0

代码语言:javascript
复制
<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>

提供所需的输出(带有正确的名称空间声明):

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7180316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档