根据XML标记位置添加属性

时间:2014-09-30 11:15:14

标签: xml xslt xslt-1.0

我有如下的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>

我需要使用名为preference的属性将上述XML转换为以下格式。

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

任何人都可以帮助我为第一次出现添加属性preference="1",并为使用XSLT的所有其他事件添加preference="0"

1 个答案:

答案 0 :(得分: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,并为特定cl:xref案例添加另一个模板其父级是第一个section

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