2017-11-08 4 views
0

だから私は、私は取り付け祖先は、子ノードに属性

A重く編集されたバージョンが

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

NOTE下回っているCSVファイルに小さな部分を抽出しようとしていますこの非常に複雑なデータを持っている - 時々私は取得しようとしていますどのような全くSystematicNameまたは全く同義語またはどちら は、私が唯一のDisplayNameおよびIdを削除する方法を考え出すことができます現時点で

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

ありませんこの形式ではなく、すべての行で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> 
+0

「Phytoxanthins」を出力するXSLTコードがないため、XSLT共有は共有出力と一致しません。正しいXSLTを共有してください。 –

+0

正しいXSLTを持っていれば、私が望む出力を得る方法を尋ねることはありませんか? :)これを説明する更新された質問 –

答えて

0

ある

、あなたはCREできます入力に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" 
+0

それは動作しますが、libxmlベースのプロセッサではXPASATHが大きすぎるために失敗し、メモリを使い果たします。xalanの下で試してみてください... –

+0

OK xalanで動作しました...しかし、化学物質にはシノニムがないことがあります。どうすればそれらを検出してスキップすることができますか?現時点では私は ""を取得 –

+0

これは理想的には、SOのガイドラインに従って別の質問があったはずです。空の値をスキップするには、 ''ループに ''条件を追加して、 'printValues'テンプレートを空でない値に対してのみ呼び出したり、' ' '$ val2'が空でないときにのみ印刷する' printValues'テンプレートです。 –

関連する問題