添加基于XML标记位置的属性



我有如下的XML结构:

<root>
<section id="c001">
<cl:xref target-type="generated-number" link-target="ch12_fig_1"/>
<cl:xref target-type="generated-number" link-target="ch12_fig_2"/>
<cl:xref target-type="generated-number" link-target="ch12_fig_3"/>
</section>
<section id="c002">
<cl:xref target-type="generated-number" link-target="ch12_fig_2"/>
</section>
<section id="c003">
<cl:xref target-type="generated-number" link-target="ch12_fig_1"/>
</section>
</root>

我需要将上面的XML转换为下面的格式,属性为preference

<root>
<section id="c001">
<cl:xref target-type="generated-number" link-target="ch12_fig_1" preference="1"/>
<cl:xref target-type="generated-number" link-target="ch12_fig_2" preference="1"/>
<cl:xref target-type="generated-number" link-target="ch12_fig_3" preference="1"/>
</section>
<section id="c002">
<cl:xref target-type="generated-number" link-target="ch12_fig_2" preference="0"/>
</section>
<section id="c003">
<cl:xref target-type="generated-number" link-target="ch12_fig_1" preference="0"/>
</section>
</root>

有人能帮助我如何使用XSLT为第一次出现添加属性preference="1",为所有其他出现添加属性preference="0"吗。

您的XML格式不正确,因为它使用了未声明的命名空间前缀。首先,修复以下问题:

<root xmlns:cl="http://xml.cengage-learning.com/cendoc-core">
  <section id="c001">
    <cl:xref target-type="generated-number" link-target="ch12_fig_1"/>
    <cl:xref target-type="generated-number" link-target="ch12_fig_2"/>
    <cl:xref target-type="generated-number" link-target="ch12_fig_3"/>
  </section>
  <section id="c002">
    <cl:xref target-type="generated-number" link-target="ch12_fig_2"/>
  </section>
  <section id="c003">
    <cl:xref target-type="generated-number" link-target="ch12_fig_1"/>
  </section>
</root>

注意:我已经对cl:前缀的名称空间定义进行了假设;如果我猜错了,您可能需要调整为实际的命名空间

其次,通过为进行通常复制的常见cl:xref情况添加一个模板,再添加@preference = 1,并为其父级为第一个section的特定cl:xref情况添加另一个模板来调整身份转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:cl="http://xml.cengage-learning.com/cendoc-core">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="cl:xref">
    <xsl:copy>
      <xsl:attribute name="preference">0</xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="cl:xref[../count(preceding::section) = 0]">
    <xsl:copy>
      <xsl:attribute name="preference">1</xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

对已更正的输入XML文档运行此转换将生成所需的输出XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:cl="http://xml.cengage-learning.com/cendoc-core">
  <section id="c001">
    <cl:xref preference="1" target-type="generated-number" link-target="ch12_fig_1"/>
    <cl:xref preference="1" target-type="generated-number" link-target="ch12_fig_2"/>
    <cl:xref preference="1" target-type="generated-number" link-target="ch12_fig_3"/>
  </section>
  <section id="c002">
    <cl:xref preference="0" target-type="generated-number" link-target="ch12_fig_2"/>
  </section>
  <section id="c003">
    <cl:xref preference="0" target-type="generated-number" link-target="ch12_fig_1"/>
  </section>
</root>

相关内容

  • 没有找到相关文章

最新更新