Xpath 表达式用于根据 id 循环不同文件中的记录

发布于 2024-11-05 07:30:48 字数 8933 浏览 0 评论 0原文

我有两个源文件

<corporations>
  <corporation>
    <CorporationID>1044293</CorporationID>
    <CorporationName>CorporationName1</CorporationName>
    <CorporationNumber>CorporationNumber1</CorporationNumber>
    <NVBusinessID>NVBusinessID1</NVBusinessID>
    <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
  </corporation>
  <corporation>
    <CorporationID>1044294</CorporationID>
    <CorporationName>CorporationName2</CorporationName>
    <CorporationNumber>CorporationNumber2</CorporationNumber>
    <NVBusinessID>NVBusinessID2</NVBusinessID>
    <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
  </corporation>
  <corporation>
    <CorporationID>1044295</CorporationID>
    <CorporationName>CorporationName3</CorporationName>
    <CorporationNumber>CorporationNumber3</CorporationNumber>
    <NVBusinessID>NVBusinessID3</NVBusinessID>
    <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
  </corporation>
</corporations>

<corporationActions>
  <corporationAction>
    <ActionID>5530960</ActionID>
    <CorporationID>1044293</CorporationID>
    <ActionDate>2008-05-03</ActionDate>
    <ActionType>ActionType1</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>3</DocumentNumber>
    <NumberofPages>1</NumberofPages>
  </corporationAction>
  <corporationAction>
    <ActionID>5530974</ActionID>
    <CorporationID>1044294</CorporationID>
    <ActionDate>2009-05-03</ActionDate>
    <ActionType>ActionType2</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>2</DocumentNumber>
    <NumberofPages>4</NumberofPages>
  </corporationAction>
  <corporationAction>
    <ActionID>5530975</ActionID>
    <CorporationID>1044294</CorporationID>
    <ActionDate>2009-05-03</ActionDate>
    <ActionType>ActionType3</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>2</DocumentNumber>
    <NumberofPages>2</NumberofPages>
  </corporationAction>
  <corporationAction>
    <ActionID>5530976</ActionID>
    <CorporationID>1044295</CorporationID>
    <ActionDate>2011-05-03</ActionDate>
    <ActionType>ActionType4</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>20110258778-91</DocumentNumber>
    <NumberofPages>4</NumberofPages>
  </corporationAction>
</corporationActions>

我需要从第一个文件中获取与 CorporationID 匹配的所有记录(作为响应),并从第二个文件中找到相应的 CorporationID 记录(它们可以是多个)并按以下格式打印

<sendCorporationActionDetailsVariable>
 <corporationActions>
  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID1</ns1:NVBusinessID>
   <ns1:CorporationName>CorporationName1</ns1:CorporationName>
   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044293</ns1:CorporationID>
    <ns1:ActionDate>04/05/2008</ns1:ActionDate>
    <ns1:ActionType>Articles of Organization1</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>

  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
   <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2011</ns1:ActionDate>
    <ns1:ActionType>ActionType2</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>

  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
   <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
  <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2010</ns1:ActionDate>
    <ns1:ActionType>ActionType3</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>

  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID3</ns1:NVBusinessID>
   <ns1:CorporationName>CorporationName3</ns1:CorporationName>
   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044295</ns1:CorporationID>
    <ns1:ActionDate>04/05/2011</ns1:ActionDate>
    <ns1:ActionType>ActionType4</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>
 </corporationActions>
</sendCorporationActionDetailsVariable>

这是响应的一部分文件:

<ns1:corporationAction>
 <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
 <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
 <ns1:corporationActionDetails>
  <ns1:CorporationID>1044294</ns1:CorporationID>
  <ns1:ActionDate>04/05/2011</ns1:ActionDate>
  <ns1:ActionType>ActionType2</ns1:ActionType>
 </ns1:corporationActionDetails>
</ns1:corporationAction>

<ns1:corporationAction>
 <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
 <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
<ns1:corporationActionDetails>
  <ns1:CorporationID>1044294</ns1:CorporationID>
  <ns1:ActionDate>04/05/2010</ns1:ActionDate>
  <ns1:ActionType>ActionType3</ns1:ActionType>
 </ns1:corporationActionDetails>
</ns1:corporationAction>

我需要上面的内容采用以下格式,

<ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
   <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>

   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2011</ns1:ActionDate>
    <ns1:ActionType>ActionType2</ns1:ActionType>
   </ns1:corporationActionDetails>

  <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2010</ns1:ActionDate>
    <ns1:ActionType>ActionType3</ns1:ActionType>
   </ns1:corporationActionDetails>
</ns1:corporationAction>

我已经编写了一个逻辑,该逻辑循环遍历第一个文件和第二个文件中的每个文件并比较corporationID并打印所需的信息。

<ns1:corporationActions>
  <xsl:for-each select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction">
  <xsl:variable name="corpActPos" select="position()"/>
<xsl:for-each select="/tns:corporations/tns:corporation">
<xsl:variable name="corpPos" select="position()"/>

  <xsl:if test="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:CorporationID = /tns:corporations/tns:corporation[$corpPos]/tns:CorporationID">
  <xsl:variable name="cntCorpId" select="count($InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction/ns0:CorporationID)"/>
    <ns1:corporationAction>
      <ns1:NVBusinessID>
        <xsl:value-of select="/tns:corporations/tns:corporation[$corpPos]/tns:NVBusinessID"/>
      </ns1:NVBusinessID>

<xsl:for-each select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:CorporationID">

  <ns1:corporationActionDetails>
        <ns1:CorporationID>
          <xsl:value-of select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:CorporationID"/>
        </ns1:CorporationID>
        <ns1:ActionDate>
          <xsl:value-of select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:ActionDate"/>
        </ns1:ActionDate>
        <ns1:ActionType>
          <xsl:value-of select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:ActionType"/>
        </ns1:ActionType>
  </ns1:corporationActionDetails>

</xsl:for-each>
    </ns1:corporationAction>
    </xsl:if>
  </xsl:for-each>
  </xsl:for-each>
</ns1:corporationActions>

相反,我需要一个 XPATH 表达式来处理这些问题。

I have two source files

<corporations>
  <corporation>
    <CorporationID>1044293</CorporationID>
    <CorporationName>CorporationName1</CorporationName>
    <CorporationNumber>CorporationNumber1</CorporationNumber>
    <NVBusinessID>NVBusinessID1</NVBusinessID>
    <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
  </corporation>
  <corporation>
    <CorporationID>1044294</CorporationID>
    <CorporationName>CorporationName2</CorporationName>
    <CorporationNumber>CorporationNumber2</CorporationNumber>
    <NVBusinessID>NVBusinessID2</NVBusinessID>
    <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
  </corporation>
  <corporation>
    <CorporationID>1044295</CorporationID>
    <CorporationName>CorporationName3</CorporationName>
    <CorporationNumber>CorporationNumber3</CorporationNumber>
    <NVBusinessID>NVBusinessID3</NVBusinessID>
    <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
  </corporation>
</corporations>

and

<corporationActions>
  <corporationAction>
    <ActionID>5530960</ActionID>
    <CorporationID>1044293</CorporationID>
    <ActionDate>2008-05-03</ActionDate>
    <ActionType>ActionType1</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>3</DocumentNumber>
    <NumberofPages>1</NumberofPages>
  </corporationAction>
  <corporationAction>
    <ActionID>5530974</ActionID>
    <CorporationID>1044294</CorporationID>
    <ActionDate>2009-05-03</ActionDate>
    <ActionType>ActionType2</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>2</DocumentNumber>
    <NumberofPages>4</NumberofPages>
  </corporationAction>
  <corporationAction>
    <ActionID>5530975</ActionID>
    <CorporationID>1044294</CorporationID>
    <ActionDate>2009-05-03</ActionDate>
    <ActionType>ActionType3</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>2</DocumentNumber>
    <NumberofPages>2</NumberofPages>
  </corporationAction>
  <corporationAction>
    <ActionID>5530976</ActionID>
    <CorporationID>1044295</CorporationID>
    <ActionDate>2011-05-03</ActionDate>
    <ActionType>ActionType4</ActionType>
    <ActionNotes></ActionNotes>
    <DocumentNumber>20110258778-91</DocumentNumber>
    <NumberofPages>4</NumberofPages>
  </corporationAction>
</corporationActions>

I need to get all those records (as Response) matching CorporationID from 1st File and find the corresponding CorporationID records (they can be multiple) from 2nd file and print in the following format

<sendCorporationActionDetailsVariable>
 <corporationActions>
  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID1</ns1:NVBusinessID>
   <ns1:CorporationName>CorporationName1</ns1:CorporationName>
   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044293</ns1:CorporationID>
    <ns1:ActionDate>04/05/2008</ns1:ActionDate>
    <ns1:ActionType>Articles of Organization1</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>

  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
   <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2011</ns1:ActionDate>
    <ns1:ActionType>ActionType2</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>

  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
   <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
  <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2010</ns1:ActionDate>
    <ns1:ActionType>ActionType3</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>

  <ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID3</ns1:NVBusinessID>
   <ns1:CorporationName>CorporationName3</ns1:CorporationName>
   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044295</ns1:CorporationID>
    <ns1:ActionDate>04/05/2011</ns1:ActionDate>
    <ns1:ActionType>ActionType4</ns1:ActionType>
   </ns1:corporationActionDetails>
  </ns1:corporationAction>
 </corporationActions>
</sendCorporationActionDetailsVariable>

Here is a portion of the response file:

<ns1:corporationAction>
 <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
 <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
 <ns1:corporationActionDetails>
  <ns1:CorporationID>1044294</ns1:CorporationID>
  <ns1:ActionDate>04/05/2011</ns1:ActionDate>
  <ns1:ActionType>ActionType2</ns1:ActionType>
 </ns1:corporationActionDetails>
</ns1:corporationAction>

<ns1:corporationAction>
 <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
 <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>
<ns1:corporationActionDetails>
  <ns1:CorporationID>1044294</ns1:CorporationID>
  <ns1:ActionDate>04/05/2010</ns1:ActionDate>
  <ns1:ActionType>ActionType3</ns1:ActionType>
 </ns1:corporationActionDetails>
</ns1:corporationAction>

I need the above to be in the following format

<ns1:corporationAction>
   <ns1:NVBusinessID>NVBusinessID2</ns1:NVBusinessID>
   <ns1:CorporationName>NVBusinessID2</ns1:CorporationName>

   <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2011</ns1:ActionDate>
    <ns1:ActionType>ActionType2</ns1:ActionType>
   </ns1:corporationActionDetails>

  <ns1:corporationActionDetails>
    <ns1:CorporationID>1044294</ns1:CorporationID>
    <ns1:ActionDate>04/05/2010</ns1:ActionDate>
    <ns1:ActionType>ActionType3</ns1:ActionType>
   </ns1:corporationActionDetails>
</ns1:corporationAction>

I have written a logic which loop through for-each of the 1st file and 2nd file and compares corporationID and prints the required information.

<ns1:corporationActions>
  <xsl:for-each select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction">
  <xsl:variable name="corpActPos" select="position()"/>
<xsl:for-each select="/tns:corporations/tns:corporation">
<xsl:variable name="corpPos" select="position()"/>

  <xsl:if test="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:CorporationID = /tns:corporations/tns:corporation[$corpPos]/tns:CorporationID">
  <xsl:variable name="cntCorpId" select="count($InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction/ns0:CorporationID)"/>
    <ns1:corporationAction>
      <ns1:NVBusinessID>
        <xsl:value-of select="/tns:corporations/tns:corporation[$corpPos]/tns:NVBusinessID"/>
      </ns1:NVBusinessID>

<xsl:for-each select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:CorporationID">

  <ns1:corporationActionDetails>
        <ns1:CorporationID>
          <xsl:value-of select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:CorporationID"/>
        </ns1:CorporationID>
        <ns1:ActionDate>
          <xsl:value-of select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:ActionDate"/>
        </ns1:ActionDate>
        <ns1:ActionType>
          <xsl:value-of select="$InvokeCorpActFileReaderOutputVariable.body/ns0:corporationActions/ns0:corporationAction[$corpActPos]/ns0:ActionType"/>
        </ns1:ActionType>
  </ns1:corporationActionDetails>

</xsl:for-each>
    </ns1:corporationAction>
    </xsl:if>
  </xsl:for-each>
  </xsl:for-each>
</ns1:corporationActions>

Instead of this I need one XPATH expression which takes care of these.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-11-12 07:30:48

想要的输出不能仅作为计算单个 XPath 表达式的结果而产生。 XPath 是 XML 文档的查询语言,因此,计算 XPath 表达式不会更改任何 XML 文档的结构,也不会修改任何 XML 文档中的任何节点。

您希望更改节点(例如新节点位于新名称空间中——前缀 ns1: 绑定到该名称空间)——仅计算 XPath 表达式是无法完成的。

要执行此类转换,您不仅需要 XPath,还需要另一种托管 XPath 引擎的语言,例如 XSLT、XQuery 或任何具有 DOM API 的编程语言。

这样的转换可以使用 XSLT 以非常紧凑的方式编写:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns1="my:ns1" exclude-result-prefixes="ns1" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="ActionByCorpId" match="corporationAction"
  use="CorporationID"/>

 <xsl:variable name="vDoc2" select="document('delete16.xml')"/>

 <xsl:template match="/">

  <sendCorporationActionDetailsVariable>
   <corporationActions>
    <xsl:apply-templates select="/*/*/CorporationID"/>
   </corporationActions>
  </sendCorporationActionDetailsVariable>
 </xsl:template>

 <xsl:template match="CorporationID">
   <xsl:variable name="vId" select="."/>

   <xsl:for-each select="$vDoc2/*">
     <xsl:apply-templates mode="addNamespace"
      select="key('ActionByCorpId',$vId)"/>
   </xsl:for-each>
 </xsl:template>

 <xsl:template match="node()|@*" mode="addNamespace">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*" mode="addNamespace"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="*" mode="addNamespace">
  <xsl:element name="ns1:{name()}" namespace="my:ns1">
   <xsl:apply-templates select="node()|@*" mode="addNamespace"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<corporations>
    <corporation>
        <CorporationID>1044293</CorporationID>
        <CorporationName>CorporationName1</CorporationName>
        <CorporationNumber>CorporationNumber1</CorporationNumber>
        <NVBusinessID>NVBusinessID1</NVBusinessID>
        <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
    </corporation>
    <corporation>
        <CorporationID>1044294</CorporationID>
        <CorporationName>CorporationName2</CorporationName>
        <CorporationNumber>CorporationNumber2</CorporationNumber>
        <NVBusinessID>NVBusinessID2</NVBusinessID>
        <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
    </corporation>
    <corporation>
        <CorporationID>1044295</CorporationID>
        <CorporationName>CorporationName3</CorporationName>
        <CorporationNumber>CorporationNumber3</CorporationNumber>
        <NVBusinessID>NVBusinessID3</NVBusinessID>
        <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
    </corporation>
</corporations>

生成所需的正确结果

<sendCorporationActionDetailsVariable>
   <corporationActions>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530960</ns1:ActionID>
         <ns1:CorporationID>1044293</ns1:CorporationID>
         <ns1:ActionDate>2008-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType1</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>3</ns1:DocumentNumber>
         <ns1:NumberofPages>1</ns1:NumberofPages>
      </ns1:corporationAction>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530974</ns1:ActionID>
         <ns1:CorporationID>1044294</ns1:CorporationID>
         <ns1:ActionDate>2009-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType2</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>2</ns1:DocumentNumber>
         <ns1:NumberofPages>4</ns1:NumberofPages>
      </ns1:corporationAction>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530975</ns1:ActionID>
         <ns1:CorporationID>1044294</ns1:CorporationID>
         <ns1:ActionDate>2009-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType3</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>2</ns1:DocumentNumber>
         <ns1:NumberofPages>2</ns1:NumberofPages>
      </ns1:corporationAction>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530976</ns1:ActionID>
         <ns1:CorporationID>1044295</ns1:CorporationID>
         <ns1:ActionDate>2011-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType4</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>20110258778-91</ns1:DocumentNumber>
         <ns1:NumberofPages>4</ns1:NumberofPages>
      </ns1:corporationAction>
   </corporationActions>
</sendCorporationActionDetailsVariable>

The wanted output cannot be produced only as the result of evaluating a single XPath expression. XPath is a query language for XML documents and as such, evaluating an XPath expression never changes the structure of any XML document and never modifies any node in any XML document.

You want nodes changed (such as the new nodes being in a new namespace -- to which the prefix ns1: is bound) -- this cannot be done just evaluating an XPath expression.

To perform such transformations, you need not only XPath but also another language that is hosting an XPath engine -- such as XSLT, XQuery or any programming language with DOM API.

Such a transformation can be written in a very compact way using XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns1="my:ns1" exclude-result-prefixes="ns1" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="ActionByCorpId" match="corporationAction"
  use="CorporationID"/>

 <xsl:variable name="vDoc2" select="document('delete16.xml')"/>

 <xsl:template match="/">

  <sendCorporationActionDetailsVariable>
   <corporationActions>
    <xsl:apply-templates select="/*/*/CorporationID"/>
   </corporationActions>
  </sendCorporationActionDetailsVariable>
 </xsl:template>

 <xsl:template match="CorporationID">
   <xsl:variable name="vId" select="."/>

   <xsl:for-each select="$vDoc2/*">
     <xsl:apply-templates mode="addNamespace"
      select="key('ActionByCorpId',$vId)"/>
   </xsl:for-each>
 </xsl:template>

 <xsl:template match="node()|@*" mode="addNamespace">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*" mode="addNamespace"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="*" mode="addNamespace">
  <xsl:element name="ns1:{name()}" namespace="my:ns1">
   <xsl:apply-templates select="node()|@*" mode="addNamespace"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<corporations>
    <corporation>
        <CorporationID>1044293</CorporationID>
        <CorporationName>CorporationName1</CorporationName>
        <CorporationNumber>CorporationNumber1</CorporationNumber>
        <NVBusinessID>NVBusinessID1</NVBusinessID>
        <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
    </corporation>
    <corporation>
        <CorporationID>1044294</CorporationID>
        <CorporationName>CorporationName2</CorporationName>
        <CorporationNumber>CorporationNumber2</CorporationNumber>
        <NVBusinessID>NVBusinessID2</NVBusinessID>
        <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
    </corporation>
    <corporation>
        <CorporationID>1044295</CorporationID>
        <CorporationName>CorporationName3</CorporationName>
        <CorporationNumber>CorporationNumber3</CorporationNumber>
        <NVBusinessID>NVBusinessID3</NVBusinessID>
        <BusinessLicenseExp>2011-04-05T12:12:12</BusinessLicenseExp>
    </corporation>
</corporations>

the wanted, correct result is produced:

<sendCorporationActionDetailsVariable>
   <corporationActions>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530960</ns1:ActionID>
         <ns1:CorporationID>1044293</ns1:CorporationID>
         <ns1:ActionDate>2008-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType1</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>3</ns1:DocumentNumber>
         <ns1:NumberofPages>1</ns1:NumberofPages>
      </ns1:corporationAction>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530974</ns1:ActionID>
         <ns1:CorporationID>1044294</ns1:CorporationID>
         <ns1:ActionDate>2009-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType2</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>2</ns1:DocumentNumber>
         <ns1:NumberofPages>4</ns1:NumberofPages>
      </ns1:corporationAction>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530975</ns1:ActionID>
         <ns1:CorporationID>1044294</ns1:CorporationID>
         <ns1:ActionDate>2009-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType3</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>2</ns1:DocumentNumber>
         <ns1:NumberofPages>2</ns1:NumberofPages>
      </ns1:corporationAction>
      <ns1:corporationAction xmlns:ns1="my:ns1">
         <ns1:ActionID>5530976</ns1:ActionID>
         <ns1:CorporationID>1044295</ns1:CorporationID>
         <ns1:ActionDate>2011-05-03</ns1:ActionDate>
         <ns1:ActionType>ActionType4</ns1:ActionType>
         <ns1:ActionNotes/>
         <ns1:DocumentNumber>20110258778-91</ns1:DocumentNumber>
         <ns1:NumberofPages>4</ns1:NumberofPages>
      </ns1:corporationAction>
   </corporationActions>
</sendCorporationActionDetailsVariable>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文