使用XSLT协作XML标记

时间:2013-10-04 12:31:59

标签: xml xslt

我是XSL转换的新手,所以可能是它的基本问题,我必须将以下XML代码段转换为另一种XML格式(清单2)

清单1:

<arr name="experimental-properties-kind">
        <str>Hydrophobicity</str>
        <str>Isoelectric Point</str>
        <str>Molecular Weight</str>
        <str>Molecular Formula</str>
    </arr>
    <arr name="experimental-properties-value">
        <str>-0.985</str>
        <str>3.91</str>
        <str>2180.2853</str>
        <str>C287H440N80O110S6</str>
    </arr>
    <arr name="experimental-properties-source">
        <str>Otto, A.  Seckler, R. Eur. J. Biochem. 202:67-73 (1991)</str>
        <str/>
        <str/>
        <str/>
    </arr>

清单2:

<experimental-properties>
  <property>
    <kind>Hydrophobicity</kind>
    <value>-0.985</value>
    <source></source>
  </property>
  <property>
    <kind>Isoelectric Point</kind>
    <value>3.91</value>
    <source></source>
  </property>
  <property>
    <kind>Molecular Weight</kind>
    <value>2180.2853</value>
    <source></source>
  </property>
  <property>
    <kind>Molecular Formula</kind>
    <value>C98H138N24O33</value>
    <source></source>
  </property>
</experimental-properties>

另外请建议我调试XSLT的工具。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您的输入XML,已修改为根据需要具有单个根元素,格式正确:

<arrs-triplet>
  <arr name="experimental-properties-kind">
    <str>Hydrophobicity</str>
    <str>Isoelectric Point</str>
    <str>Molecular Weight</str>
    <str>Molecular Formula</str>
  </arr>
  <arr name="experimental-properties-value">
    <str>-0.985</str>
    <str>3.91</str>
    <str>2180.2853</str>
    <str>C287H440N80O110S6</str>
  </arr>
  <arr name="experimental-properties-source">
    <str>Otto, A.  Seckler, R. Eur. J. Biochem. 202:67-73 (1991)</str>
    <str/>
    <str/>
    <str/>
  </arr>
</arrs-triplet>

由此XSLT转换:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/arrs-triplet">
    <experimental-properties>
      <xsl:for-each select="arr[@name = 'experimental-properties-kind']/str">
        <xsl:variable name="kind-position" select="position()"/>
        <property>
          <kind><xsl:value-of select="."/></kind>
          <value><xsl:value-of select="../../arr[@name='experimental-properties-value']/str[position() = $kind-position]"/></value>
          <source><xsl:value-of select="../../arr[@name='experimental-properties-source']/str[position() = $kind-position]"/></source>
        </property>
      </xsl:for-each>
    </experimental-properties>
  </xsl:template>
</xsl:stylesheet>

产生所需的输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<experimental-properties>
   <property>
      <kind>Hydrophobicity</kind>
      <value>-0.985</value>
      <source>Otto, A.  Seckler, R. Eur. J. Biochem. 202:67-73 (1991)</source>
   </property>
   <property>
      <kind>Isoelectric Point</kind>
      <value>3.91</value>
      <source/>
   </property>
   <property>
      <kind>Molecular Weight</kind>
      <value>2180.2853</value>
      <source/>
   </property>
   <property>
      <kind>Molecular Formula</kind>
      <value>C287H440N80O110S6</value>
      <source/>
   </property>
</experimental-properties>

请注意,我冒昧地提供了source元素的内容,即使它在示例输出中被省略了。