create xslt to merge duplicate term properties and then delete
apologies if this is answered elsewhere, I did look at the related questions but couldn't see how they related to this.
I have a file like this:
<?xml version="1.0" encoding="utf-8"?>
<Zthes>
<term>
<termId>1</termId>
<termUpdate>Add</termUpdate>
<termName>Science, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</rel开发者_如何学GoationType>
<termId>OMITERM9998</termId>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>Sciences, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</relationType>
<termId>OMITERMO9999</termId>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>OMITERMO9998</termId>
<termUpdate>Add</termUpdate>
<termName>Biological Sciences</termName>
<termType>Pt</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
</term>
<term>
<termId>OMITERMO9999</termId>
<termUpdate>Add</termUpdate>
<termName>Biological Sciences</termName>
<termType>Pt</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
</term>
</Zthes>
Is it possible to write a xlst which goes through and merges all Pts? That would result, in this instance, there would be 2nd term names using the same Pt term. The id they use is not important. Every Nd will reference a Pt, not every Pt will have NDs referencing it, and Pts can have multiple Nds referencing them
Thanks for any pointers I am hopeless at these things!
Edit: the output would be:
<?xml version="1.0" encoding="utf-8"?>
<Zthes>
<term>
<termId>1</termId>
<termUpdate>Add</termUpdate>
<termName>Science, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</relationType>
<termId>OMITERM9998</termId>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>Sciences, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</relationType>
<termId>OMITERMO9998</termId>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>OMITERMO9998</termId>
<termUpdate>Add</termUpdate>
<termName>Biological Sciences</termName>
<termType>Pt</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
</term>
</Zthes>
so that the relationship which 2 Nd terms had with 2 Pt terms with the same name, but different IDs, were combined into 2 Nd terms having a relationship with just one Pt term. As long as the relationships were merged into the first PT, the second PT could be deleted so that there would be no duplicates but the relationships would still be intact?
I wrote the following stylesheet:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="k1" match="term[termType = 'Pt']" use="termName"/>
<xsl:key name="k2" match="term[termType = 'Pt']" use="termId"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="term[termType = 'Pt'][not(generate-id() = generate-id(key('k1', termName)[1]))]"/>
<xsl:template match="term[termType = 'Nd']/relation/termId">
<xsl:copy>
<xsl:value-of select="key('k1', key('k2', .)/termName)[1]/termId"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to
<?xml version="1.0" encoding="utf-8"?>
<Zthes>
<term>
<termId>1</termId>
<termUpdate>Add</termUpdate>
<termName>Science, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</relationType>
<termId>OMITERM9998</termId>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>Sciences, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</relationType>
<termId>OMITERMO9999</termId>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>OMITERMO9998</termId>
<termUpdate>Add</termUpdate>
<termName>Biological Sciences</termName>
<termType>Pt</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
</term>
<term>
<termId>OMITERMO9999</termId>
<termUpdate>Add</termUpdate>
<termName>Biological Sciences</termName>
<termType>Pt</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
</term>
</Zthes>
the output is
<?xml version="1.0" encoding="utf-8"?><Zthes>
<term>
<termId>1</termId>
<termUpdate>Add</termUpdate>
<termName>Science, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</relationType>
<termId/>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>Sciences, Biological</termName>
<termType>Nd</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
<relation>
<relationType>USE</relationType>
<termId>OMITERMO9998</termId>
<termName>Biological Sciences</termName>
</relation>
</term>
<term>
<termId>OMITERMO9998</termId>
<termUpdate>Add</termUpdate>
<termName>Biological Sciences</termName>
<termType>Pt</termType>
<termStatus>Active</termStatus>
<termApproval>Approved</termApproval>
<termCreatedDate>20110414T07:23:26</termCreatedDate>
<termCreatedBy>admin</termCreatedBy>
<termModifiedDate>20110414T07:35:54</termModifiedDate>
<termModifiedBy>admin</termModifiedBy>
<termAttribute label="A-Z Entry"/>
</term>
</Zthes>
so one cross reference is not resolved correctly but I think the problem is not with the XSLT but with your data where you have <termId>OMITERM9998</termId>
but then <termId>OMITERMO9998</termId>
with a leading 0
. Does that help? Are the ids in the real data in better shape? Or do you need to code to strip leading zeros somehow to match/cross reference elements?
精彩评论