0

我是 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 的工具。提前致谢!

4

1 回答 1

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元素的内容,即使它在您的示例输出中被省略了。

于 2013-10-04T13:48:26.963 回答