我试图遍历父节点的distinct子元素。框架预期的xml 以下是我的xml。
<NewDataSet>
<caseParticipants>
<Litigants>PORTFOLIO RECOVERY ASSOCIATES</Litigants>
<Attorneys>MANISCALCO, ANTHONY JOSEPH</Attorneys>
</caseParticipants>
<caseParticipants>
<Litigants>PLAINTIFF</Litigants>
<Attorneys>Plaintiff Attorney</Attorneys>
</caseParticipants>
<caseParticipants>
<Litigants />
<Attorneys />
<caseProceedings>
<Date>05/12/2015</Date>
<Details>PETITION/COMPLAINT NO DOC#</Details>
</caseProceedings>
<caseProceedings>
<Date>05/12/2015</Date>
<Details>ASM: SMALL CLAIMS $501-$2,500</Details>
</caseProceedings>
<caseCalendar>
<Date>06/30/2015 13:00:00</Date>
<Docket_x0020_Type>PRETRIAL CONF</Docket_x0020_Type>
<Judge>LANDT, ROBERT E</Judge>
<Location>XX : COURTR</Location>
</caseCalendar>
<caseFinancialSummary>
<Column1>Assessment Total:</Column1>
<Column2>$185.00</Column2>
</caseFinancialSummary>
<caseFinancialSummary>
<Column1>Assessment Paid:</Column1>
<Column2>$185.00</Column2>
</caseFinancialSummary>
<caseFinancialSummary>
<Column1>Assessment Due:</Column1>
<Column2>$0.00</Column2>
</caseFinancialSummary>
<caseFinancialSummary>
<Column1>Restitution Total:</Column1>
<Column2>$0.00</Column2>
</caseFinancialSummary>
<caseFinancialSummary>
<Column1>Restitution Paid:</Column1>
<Column2>$0.00</Column2>
</caseFinancialSummary>
<caseFinancialSummary>
<Column1>Restitution Due:</Column1>
<Column2>$0.00</Column2>
</caseFinancialSummary>
<caseFinancialSummary />
<caseFinancialdetails>
<Assessment_x0020_Due>$185.00</Assessment_x0020_Due>
<Assessment_x0020_Paid>$185.00</Assessment_x0020_Paid>
<Restitution_x0020_Due>$0.00</Restitution_x0020_Due>
<Restitution_x0020_Paid>$0.00</Restitution_x0020_Paid>
</caseFinancialdetails>
</NewDataSet>
我需要转换为xml,如下所示。
<searchResult>
<caseDetails>
<caseParticipants Label="Participants">
<row>
<colhead id="1" align="left" bold="no">Litigants</colhead>
<colhead id="2" align="left" bold="no">Attorneys</colhead>
</row>
<row>
<cell id="1">Portfolio Recovery Associates</cell>
<cell id="2">Maniscalco, Anthony Joseph</cell>
</row>
<row>
<cell id="1">Plaintiff</cell>
<cell id="2">Plaintiff Attorney</cell>
</row>
<row>
<cell id="1" />
<cell id="2" />
</row>
</caseParticipants>
<caseProceedings Label="Proceedings">
<row>
<colhead id="1" align="left" bold="no">Date</colhead>
<colhead id="2" align="left" bold="no">Details</colhead>
</row>
<row>
<cell id="1">05/12/2015</cell>
<cell id="2">Petition/Complaint no Doc#</cell>
<document available="No" />
</row>
<row>
<cell id="1">05/12/2015</cell>
<cell id="2">Asm: Small Claims $501-$2,500</cell>
<document available="No" />
</row>
</caseProceedings>
<caseCalendar Label="Calendar">
<row>
<colhead id="1" align="left" bold="no">Date</colhead>
<colhead id="2" align="left" bold="no">Docket Type</colhead>
<colhead id="3" align="left" bold="no">Judge</colhead>
<colhead id="4" align="left" bold="no">Location</colhead>
<colhead id="5" align="left" bold="no">Prosecutor</colhead>
<colhead id="6" align="left" bold="no">Defendant Attorney</colhead>
</row>
<row>
<cell id="1">06/30/2015 13:00:00</cell>
<cell id="2">Pretrial Conf</cell>
<cell id="3">Landt, Robert E</cell>
<cell id="4">Xx : Courtr</cell>
<cell id="5" />
<cell id="6" />
</row>
</caseCalendar>
<caseFinancialSummary Label="Financial Summary">
<row>
<colhead id="1" align="right" bold="no" />
<colhead id="2" align="left" bold="no" />
</row>
<row>
<cell id="1">Assessment Total:</cell>
<cell id="2">$185.00</cell>
</row>
<row>
<cell id="1">Assessment Paid:</cell>
<cell id="2">$185.00</cell>
</row>
<row>
<cell id="1">Assessment Due:</cell>
<cell id="2">$0.00</cell>
</row>
<row>
<cell id="1">Restitution Total:</cell>
<cell id="2">$0.00</cell>
</row>
<row>
<cell id="1">Restitution Paid:</cell>
<cell id="2">$0.00</cell>
</row>
<row>
<cell id="1">Restitution Due:</cell>
<cell id="2">$0.00</cell>
</row>
</caseFinancialSummary>
<caseFinancialDetails Label="Financial Details">
<row>
<colhead id="1" align="left" bold="no">Assessment Due</colhead>
<colhead id="2" align="left" bold="no">Assessment Paid</colhead>
<colhead id="3" align="left" bold="no">Restitution Due</colhead>
<colhead id="4" align="left" bold="no">Restitution Paid</colhead>
<colhead id="5" align="left" bold="no">Payment Date</colhead>
</row>
<row>
<cell id="1">$185.00</cell>
<cell id="2">$185.00</cell>
<cell id="3">$0.00</cell>
<cell id="4">$0.00</cell>
<cell id="5" />
</row>
</caseFinancialDetails>
</caseDetails>
我只为caseParticipant尝试过如下所示的一个部分。我需要为所有节点做这样的事情而不给出元素名称。因为我每次都得到不同类型的父母和孩子的元素。
XSLT代码:
<xsl:template match="caseParticipants">
<xsl:variable name="vrtfEvalResult">
<xsl:call-template name="eval">
<xsl:with-param name="pPath" select="'/NewDataSet/caseParticipants'"/>
</xsl:call-template>
</xsl:variable>
<row>
<cell id="1"><xsl:value-of select="Litigants" /></cell>
<cell id="2"><xsl:value-of select="Attorney" /></cell>
</row>
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="Path" name="eval">
<xsl:param name="pPath" select="."/>
<xsl:param name="pContext" select="/"/>
<xsl:choose>
<!-- If there is something to evaluate -->
<xsl:when test="string-length($pPath) >0">
<xsl:variable name="vPath" select=
"substring($pPath,2)"/>
<xsl:variable name="vNameTest">
<xsl:choose>
<xsl:when test="not(contains($vPath, '/'))">
<xsl:value-of select="$vPath"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=
"substring-before($vPath, '/')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:call-template name="eval">
<xsl:with-param name="pPath" select=
"substring-after($pPath, $vNameTest)"/>
<xsl:with-param name="pContext" select=
"$pContext/*[name()=$vNameTest]"/>
</xsl:call-template>
</xsl:when>
<!-- Otherwise we have evaluated completely the path -->
<xsl:otherwise>
<xsl:copy-of select="$pContext"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
提前致谢
答案 0 :(得分:0)
我不得不重新考虑这一点以使其正常工作,但我认为这就是你想要的:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:key name="distinct-second-level-elements" match="/*/*" use="local-name()" />
<xsl:template match="/NewDataSet">
<searchResults>
<caseDetails>
<xsl:for-each select="*[count(.|key('distinct-second-level-elements', local-name())[1]) = 1]">
<xsl:variable name="currNodeName" select="local-name()" />
<xsl:element name="{$currNodeName}">
<xsl:attribute name="Label">
<xsl:value-of select="substring-after($currNodeName, 'case')" />
</xsl:attribute>
<row>
<xsl:for-each select="./*">
<colhead id="{position()}" align="left" bold="no">
<xsl:value-of select="local-name(.)" />
</colhead>
</xsl:for-each>
</row>
<xsl:for-each select="//*[local-name() = $currNodeName]">
<row>
<xsl:for-each select="./*">
<cell id="{position()}">
<xsl:value-of select="." />
</cell>
</xsl:for-each>
</row>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</caseDetails>
</searchResults>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
for-each
es以获取具有相同名称和布局行/单元格的其他节点的子节点所有这一切都是使用像local-name()
和深度选择器(例如/*/*
)这样的东西完成的,所以只要你的结构保持一致,无论节点名称它都应该有效强>
一个注意事项 - id
attributes should be unique in a document,所以这在技术上不是有效的XML - 但我认为这只是下游消费者的要求,他们没有严格遵循标准。如果不是这种情况,您应该尽可能为该属性选择一个不同的名称。
输出:
<searchResults>
<caseDetails>
<caseParticipants Label="Participants">
<row>
<colhead id="1" align="left" bold="no">Litigants</colhead>
<colhead id="2" align="left" bold="no">Attorneys</colhead>
</row>
<row>
<cell id="1">PORTFOLIO RECOVERY ASSOCIATES</cell>
<cell id="2">MANISCALCO, ANTHONY JOSEPH</cell>
</row>
<row>
<cell id="1">PLAINTIFF</cell>
<cell id="2">Plaintiff Attorney</cell>
</row>
<row>
<cell id="1"></cell>
<cell id="2"></cell>
</row>
</caseParticipants>
<caseProceedings Label="Proceedings">
<row>
<colhead id="1" align="left" bold="no">Date</colhead>
<colhead id="2" align="left" bold="no">Details</colhead>
</row>
<row>
<cell id="1">05/12/2015</cell>
<cell id="2">PETITION/COMPLAINT NO DOC#</cell>
</row>
<row>
<cell id="1">05/12/2015</cell>
<cell id="2">ASM: SMALL CLAIMS $501-$2,500</cell>
</row>
</caseProceedings>
<caseCalendar Label="Calendar">
<row>
<colhead id="1" align="left" bold="no">Date</colhead>
<colhead id="2" align="left" bold="no">Docket_x0020_Type</colhead>
<colhead id="3" align="left" bold="no">Judge</colhead>
<colhead id="4" align="left" bold="no">Location</colhead>
</row>
<row>
<cell id="1">06/30/2015 13:00:00</cell>
<cell id="2">PRETRIAL CONF</cell>
<cell id="3">LANDT, ROBERT E</cell>
<cell id="4">XX : COURTR</cell>
</row>
</caseCalendar>
<caseFinancialSummary Label="FinancialSummary">
<row>
<colhead id="1" align="left" bold="no">Column1</colhead>
<colhead id="2" align="left" bold="no">Column2</colhead>
</row>
<row>
<cell id="1">Assessment Total:</cell>
<cell id="2">$185.00</cell>
</row>
<row>
<cell id="1">Assessment Paid:</cell>
<cell id="2">$185.00</cell>
</row>
<row>
<cell id="1">Assessment Due:</cell>
<cell id="2">$0.00</cell>
</row>
<row>
<cell id="1">Restitution Total:</cell>
<cell id="2">$0.00</cell>
</row>
<row>
<cell id="1">Restitution Paid:</cell>
<cell id="2">$0.00</cell>
</row>
<row>
<cell id="1">Restitution Due:</cell>
<cell id="2">$0.00</cell>
</row>
<row />
</caseFinancialSummary>
<caseFinancialdetails Label="Financialdetails">
<row>
<colhead id="1" align="left" bold="no">Assessment_x0020_Due</colhead>
<colhead id="2" align="left" bold="no">Assessment_x0020_Paid</colhead>
<colhead id="3" align="left" bold="no">Restitution_x0020_Due</colhead>
<colhead id="4" align="left" bold="no">Restitution_x0020_Paid</colhead>
</row>
<row>
<cell id="1">$185.00</cell>
<cell id="2">$185.00</cell>
<cell id="3">$0.00</cell>
<cell id="4">$0.00</cell>
</row>
</caseFinancialdetails>
</caseDetails>
</searchResults>