Xpath 表达式用于根据 id 循环不同文件中的记录
我有两个源文件
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想要的输出不能仅作为计算单个 XPath 表达式的结果而产生。 XPath 是 XML 文档的查询语言,因此,计算 XPath 表达式不会更改任何 XML 文档的结构,也不会修改任何 XML 文档中的任何节点。
您希望更改节点(例如新节点位于新名称空间中——前缀
ns1:
绑定到该名称空间)——仅计算 XPath 表达式是无法完成的。要执行此类转换,您不仅需要 XPath,还需要另一种托管 XPath 引擎的语言,例如 XSLT、XQuery 或任何具有 DOM API 的编程语言。
这样的转换可以使用 XSLT 以非常紧凑的方式编写:
当此转换应用于提供的 XML 文档时:
生成所需的正确结果:
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:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced: