简体   繁体   中英

collaborate XML tags using XSLT

I am new to XSL transformation, so may be its basic question, I have to transform following XML snippet into another XML format(Listing 2)

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

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

Also please suggest me tool to debug XSLT. Thanks in advance!

Your input XML, modified to have a single root element as required to be well-formed:

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

Transformed by this 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>

Yields the desired output 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>

Note that I took the liberty of providing the contents of the source element even though it's omitted in your sample output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM