创建 xslt 以合并重复的术语属性,然后删除

发布于 2024-11-01 15:39:15 字数 5649 浏览 1 评论 0原文

抱歉,如果在其他地方回答了这个问题,我确实查看了相关问题,但看不出它们与此有何关系。

我有一个这样的文件:

<?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>

是否可以编写一个遍历并合并所有 Pt 的 xlst?在这种情况下,这将导致第二个术语名称使用相同的 Pt 术语。他们使用的 ID 并不重要。每个 Nd 都会引用一个 Pt,不是每个 Pt 都会有 ND 引用它,并且 Pts 可以有多个 Nd 引用它们

感谢您的任何指点,我对这些事情感到绝望!

编辑:输出将是:

<?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>

这样 2 个 Nd 项与具有相同名称但不同 ID 的 2 个 Pt 项的关系被组合为仅与一个 Pt 项有关系的 2 个 Nd 项。只要将关系合并到第一个PT中,就可以删除第二个PT,这样就不会出现重复,但关系仍然完好无损?

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</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>

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?

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

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

发布评论

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

评论(1

疏忽 2024-11-08 15:39:15

我编写了以下样式表:

<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>

当应用于

<?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>

输出时,

<?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>

一个交叉引用未正确解析,但我认为问题不在于 XSLT,而在于您的数据,其中您有 OMITERM9998< /code> ,然后是 OMITERMO9998 并带有前导 0。这有帮助吗?真实数据中的 ids 是否形状更好?或者您是否需要编码以某种方式去除前导零以匹配/交叉引用元素?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文