将祖先属性附加到子节点

时间:2017-11-08 10:03:42

标签: xslt

所以我有这个相当复杂的数据,我试图将一小部分提取到csv文件

编辑严重的版本在

之下
<Chemical id="000036884" displayFormula="UNKNOWN" displayName="Carotene">
 <NameList>
  <SystematicName>
   Carotenes and Carotenoids
   <SourceList>
    <Source>MESH</Source>
   </SourceList>
  </SystematicName>
  <Synonyms>
   Phytoxanthins
   <SourceList>
    <Source>NLM</Source>
   </SourceList>
  </Synonyms>
  <DescriptorName>Carotene</DescriptorName>
 </NameList>
</Chemical>
 <Chemical id="000050011" displayFormula="C-H5-N3.Cl-H" displayName="Guanidine, monohydrochloride">
    <NameList>
      <Synonyms>
        AI3-19014
        <SourceList>
          <Source>NLM</Source>
        </SourceList>
      </Synonyms>
</Chemical>

注意 - 有时没有SystematicName或没有同义词或两者都没有 我想要解决的是

"000036884","Carotene"
"000036884","Carotenes and Carotenoids"
"000036884","Phytoxanthins"

目前我只能弄清楚如何以这种格式删除id和displayName,但无法解决如何使用每一行提取id ..

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:variable name="FS">
    <!-- Field seperator -->
    <xsl:text>;</xsl:text>
  </xsl:variable>
  <xsl:variable name="LT">
    <!-- Line terminator -->
    <xsl:text>&#10;</xsl:text>
  </xsl:variable>
  <xsl:strip-space elements="*" />
  <xsl:template match="/">
    <xsl:for-each select="//Chemical[@displayName != '' and @displayName != 'INDEX NAME NOT YET ASSIGNED']">
        <xsl:text>&#34;</xsl:text>
      <xsl:value-of select="@displayName" />
      <xsl:text>&#34;</xsl:text>
      <xsl:text>&#44;</xsl:text>
      <xsl:text>&#34;</xsl:text>https://chem.nlm.nih.gov/chemidplus/sid/startswith/<xsl:value-of select="@id" />
      <xsl:text>&#34;</xsl:text>
      <xsl:text>&#44;&#34;nlm&#34;</xsl:text>
      <xsl:value-of select="$LT" />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

这个XSLT只能给我

"000036884","Carotene","nlm"

我正在寻找帮助为所需的输出(第一个样本输出)创建XSLT

工作解决方案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:variable name="FS">
    <!-- Field seperator -->
    <xsl:text>;</xsl:text>
  </xsl:variable>
  <xsl:variable name="LT">
    <!-- Line terminator -->
    <xsl:text>&#10;</xsl:text>
  </xsl:variable>
  <xsl:strip-space elements="*" />

<xsl:template match="/">
    <xsl:for-each select="//Chemical[@displayName != '' and @displayName != 'INDEX NAME NOT YET ASSIGNED']">
        <xsl:call-template name="printValues">
            <xsl:with-param name="val1" select="@id" />
            <xsl:with-param name="val2" select="@displayName" />
        </xsl:call-template>
        <xsl:if test="normalize-space(NameList/SystematicName/text()) != ''">
        <xsl:call-template name="printValues">
            <xsl:with-param name="val1" select="@id" />
            <xsl:with-param name="val2" select="normalize-space(NameList/SystematicName/text())" />
        </xsl:call-template>
        </xsl:if>
        <xsl:if test="normalize-space(NameList/Synonyms/text()) != ''">
        <xsl:call-template name="printValues">
            <xsl:with-param name="val1" select="@id" />
            <xsl:with-param name="val2" select="normalize-space(NameList/Synonyms/text())" />
        </xsl:call-template>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

  <xsl:template name="printValues">
    <xsl:param name="val1" />
    <xsl:param name="val2" />
    <xsl:text>&#34;</xsl:text>
    <xsl:value-of select="$val2" />
    <xsl:text>&#34;</xsl:text>
    <xsl:text>&#44;</xsl:text>
    <xsl:text>&#34;</xsl:text>https://chem.nlm.nih.gov/chemidplus/sid/startswith/<xsl:value-of select="$val1" /><xsl:text>&#34;</xsl:text>
    <xsl:text>&#44;&#34;nlm&#34;</xsl:text>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

要获得所需的输出,您可以创建一个在输入中使用2个参数的模板,并以所需的结构/格式打印数据。

<xsl:template name="printValues">
    <xsl:param name="val1" />
    <xsl:param name="val2" />

    <xsl:text>&#34;</xsl:text>
    <xsl:value-of select="$val1" />
    <xsl:text>&#34;</xsl:text>

    <xsl:text>&#44;</xsl:text>
    <xsl:text>&#34;</xsl:text>
    <xsl:value-of select="$val2" />
    <xsl:text>&#34;</xsl:text>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

可以使用需要打印的值从<xsl:for-each>循环调用此模板。

<xsl:template match="/">
    <xsl:for-each select="//Chemical[@displayName != '' and @displayName != 'INDEX NAME NOT YET ASSIGNED']">
        <xsl:call-template name="printValues">
            <xsl:with-param name="val1" select="@id" />
            <xsl:with-param name="val2" select="@displayName" />
        </xsl:call-template>
        <xsl:call-template name="printValues">
            <xsl:with-param name="val1" select="@id" />
            <xsl:with-param name="val2" select="normalize-space(NameList/SystematicName/text())" />
        </xsl:call-template>
        <xsl:call-template name="printValues">
            <xsl:with-param name="val1" select="@id" />
            <xsl:with-param name="val2" select="normalize-space(NameList/Synonyms/text())" />
        </xsl:call-template>
    </xsl:for-each>
</xsl:template>

这将提供所需的输出。

"000036884","Carotene"
"000036884","Carotenes and Carotenoids"
"000036884","Phytoxanthins"
相关问题