XSLT不输出所有记录

时间:2018-04-15 15:38:35

标签: xml xslt

下午好。

我正在尝试从XML文件中选择某些节点,并将它们输出到具有不同节点结构的新XML文件中。我可以让它对于只有一条记录的XML文件工作正常,但是当我尝试在具有2个不同记录的文件上运行xslt时,它会输出第一条记录的2个副本。

我要转换FROM的xml文件是:

<records>
<rec resultID="1">
  <header>
    <controlInfo>
      <bkinfo>
        <btl>Effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer.</btl>
      </bkinfo>
      <dissinfo/>
      <jinfo>
        <jtl>European Journal of Oncology Nursing</jtl>
        <issn>14623889</issn>
      </jinfo>
      <pubinfo>
        <dt year="2017" month="12" day="01">Dec2017</dt>
        <vid>31</vid>
      </pubinfo>
      <artinfo>
        <ui type="doi">10.1016/j.ejon.2017.08.005</ui>
        <ppf>46</ppf>
        <ppct>6</ppct>
        <formats/>
        <tig>
          <atl>Effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer.</atl>
        </tig>
        <aug>
          <au>Chimluang, Janya</au>
          <au>Thanasilp, Sureeporn</au>
          <au>Akkayagorn, Lanchasak</au>
          <au>Upasen, Ratchaneekorn</au>
          <au>Pudtong, Noppamat</au>
          <au>Tantitrakul, Wilailuck</au>
          <affil>Faculty of Nursing, Chulalongkorn University, Bangkok, Thailand</affil>
        </aug>
        <sug>
          <subj type="major">Terminally Ill Patients</subj>
          <subj type="major">Cancer Patients</subj>
          <subj type="major">Psychological Well-Being</subj>
          <subj type="major">Spiritual Care</subj>
          <subj type="major">Buddhism</subj>
          <subj type="major">Terminal Care</subj>
          <subj type="minor">Human</subj>
          <subj type="minor">Quasi-Experimental Studies</subj>
          <subj type="minor">Pretest-Posttest Design</subj>
          <subj type="minor">Control Group</subj>
          <subj type="minor">Adult</subj>
          <subj type="minor">Middle Age</subj>
          <subj type="minor">Aged</subj>
          <subj type="minor">Psychological Well-Being Evaluation</subj>
          <subj type="age">Adult: 19-44 years</subj>
          <subj type="age">Middle Aged: 45-64 years</subj>
          <subj type="age">Aged: 65+ years</subj>
        </sug>
        <ab>Purpose To evaluate the effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer. Methods This quasi-experimental research study had pre- and post-test control groups. The experimental group received conventional care and an intervention based on basic Buddhist principles for three consecutive days, including seven activities based on precept activities, concentration activities and wisdom activities. The control group received conventional care alone. Results Forty-eight patients participated in this study: 23 in the experimental group and 25 in the control group. Their mean age was 53 (standard deviation 10) years. The spiritual well-being of participants in the experimental group was significantly higher than that of participants in the control group at the second post-test ( P &lt; 0.05). Conclusions An intervention based on basic Buddhist principles improved the spiritual well-being of patients with terminal cancer. This result supports the beneficial effects of implementing this type of intervention for patients with terminal cancer.</ab>
        <pubtype>Academic Journal</pubtype>
        <doctype>research</doctype>
        <doctype>Article</doctype>
      </artinfo>
      <language>English</language>
    </controlInfo>
    <displayInfo>
      <pLink>
        <url>http://search.ebscohost.com/login.aspx?direct=true&amp;db=jlh&amp;AN=126392076&amp;site=ehost-live</url>
      </pLink>
    </displayInfo>
  </header>
</rec>
<rec resultID="2">
  <header>
    <controlInfo>
      <bkinfo>
        <btl>Purpose in life in ALS patient-caregiver dyads: A multilevel longitudinal analysis.</btl>
      </bkinfo>
      <dissinfo/>
      <jinfo>
        <jtl>Health Psychology</jtl>
        <issn>02786133</issn>
      </jinfo>
      <pubinfo>
        <dt year="2017" month="11" day="01">Nov2017</dt>
        <vid>36</vid>
        <iid>11</iid>
        <place>Washington, District of Columbia</place>
      </pubinfo>
      <artinfo>
        <ui type="doi">10.1037/hea0000507</ui>
        <ppf>1092</ppf>
        <ppct>13</ppct>
        <formats/>
        <tig>
          <atl>Purpose in life in ALS patient-caregiver dyads: A multilevel longitudinal analysis.</atl>
        </tig>
        <aug>
          <au>Garcia, Natasha E.</au>
          <au>Morey, Jennifer N.</au>
          <au>Kasarskis, Edward J.</au>
          <au>Segerstrom, Suzanne C.</au>
        </aug>
        <sug>
          <subj type="major">Amyotrophic Lateral Sclerosis</subj>
          <subj type="major">Caregivers</subj>
          <subj type="major">Family Relations</subj>
          <subj type="major">Life Purpose</subj>
          <subj type="major">Psychological Well-Being</subj>
          <subj type="major">Disease Progression</subj>
          <subj type="major">Quality of Life</subj>
          <subj type="minor">Human</subj>
          <subj type="minor">Severity of Illness</subj>
          <subj type="minor">Prospective Studies</subj>
          <subj type="minor">Descriptive Research</subj>
          <subj type="minor">Descriptive Statistics</subj>
          <subj type="minor">Quality of Life Evaluation</subj>
          <subj type="minor">Time Factors</subj>
          <subj type="minor">Psychological Well-Being Evaluation</subj>
        </sug>
        <ab>Objective: Eudaemonic positive psychological health (PPH), such as purpose in life (PIL), may be maintained more than hedonic PPH, such as quality of life (QOL), for patients with amyotrophic lateral sclerosis (ALS) and their caregivers across the disease course. Furthermore, patients' and caregivers' PPH may impact one another. The present study examined (a) PIL and QOL variance structures; (b) PIL and QOL trajectories from diagnosis, approaching death, with disease severity; and (c) between-dyad and within-dyad relationships for PIL and QOL in patients with ALS and their caregivers. Method: PIL and QOL were assessed in patient-caregiver dyads (N = 110) up to 7 times over 18 months. Results: Multilevel models revealed the proportion of variance attributed to stable between-person differences was higher for PIL (patients = 74%; caregivers = 76%) than QOL (patients = 60%; caregivers = 55%). PIL and QOL declined in relation to disease severity and time. For PIL, proximity to diagnosis and death moderated within-person change; decline was generally faster following diagnosis and approaching death. Longitudinal within-dyad relationships revealed that patients' fluctuations in PIL were mirrored in their caregiver and vice versa. Conclusions: PIL was more stable than QOL and was therefore a potential psychological resource for patients and caregivers. Critical periods--after diagnosis and approaching death--accompanied more rapid PIL decline. QOL was also impacted by proximity to critical periods. PIL within-dyad relationships may reflect a shared disease experience. Psychological intervention focused on enhancing purpose, particularly during critical periods, is a promising direction for future study.</ab>
        <pubtype>Academic Journal</pubtype>
        <doctype>research</doctype>
        <doctype>Article</doctype>
      </artinfo>
      <language>English</language>
    </controlInfo>
    <displayInfo>
      <pLink>
        <url>http://search.ebscohost.com/login.aspx?direct=true&amp;db=jlh&amp;AN=125932785&amp;site=ehost-live</url>
      </pLink>
    </displayInfo>
  </header>
</rec>
</records>

我使用的xslt模板是:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <citations>
            <xsl:for-each select="/records/rec">
                <citation>
                    <Author><xsl:value-of select="/records//au"/></Author>
                    <Title><xsl:value-of select="/records//atl"/></Title>
                    <Journal><xsl:value-of select="/records//jtl"/>/Journal>
                    <Year><xsl:value-of select="/records//@year"/></Year>
                    <Volume><xsl:value-of select="/records//vid"/></Volume>
                    <StartPage><xsl:value-of select="/records//ppf"/>/StartPage>
                    <PageCount><xsl:value-of select="/records//ppct"/></PageCount>
                    <Abstract><xsl:value-of select="/records//ab"/></Abstract>
                    <DOI>http://dx.doi.org/<xsl:value-of select="/records//ui"/></DOI>
                    <URL><xsl:value-of select="/records//url"/></URL>
                    <Subjects><xsl:for-each select="/records//subj"><xsl:value-of select="text()"/>, </xsl:for-each></Subjects>
                </citation>
            </xsl:for-each>
        </citations>
    </xsl:template>
</xsl:stylesheet>

我在xml上运行xslt时获得的结果是:

<?xml version="1.0"?>
<citations>
  <citation>
    <Author>Chimluang, Janya</Author>
    <Title>Effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer.</Title>
    <Journal>European Journal of Oncology Nursing</Journal>
    <Year>2017</Year>
    <Volume>31</Volume>
    <StartPage>46</StartPage>
    <PageCount>6</PageCount>
    <Abstract>Purpose To evaluate the effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer. Methods This quasi-experimental research study had pre- and post-test control groups. The experimental group received conventional care and an intervention based on basic Buddhist principles for three consecutive days, including seven activities based on precept activities, concentration activities and wisdom activities. The control group received conventional care alone. Results Forty-eight patients participated in this study: 23 in the experimental group and 25 in the control group. Their mean age was 53 (standard deviation 10) years. The spiritual well-being of participants in the experimental group was significantly higher than that of participants in the control group at the second post-test ( P &lt; 0.05). Conclusions An intervention based on basic Buddhist principles improved the spiritual well-being of patients with terminal cancer. This result supports the beneficial effects of implementing this type of intervention for patients with terminal cancer.</Abstract>
    <DOI>http://dx.doi.org/10.1016/j.ejon.2017.08.005</DOI>
    <URL>http://search.ebscohost.com/login.aspx?direct=true&amp;db=jlh&amp;AN=126392076&amp;site=ehost-live</URL>
    <Subjects>Terminally Ill Patients, Cancer Patients, Psychological Well-Being, Spiritual Care, Buddhism, Terminal Care, Human, Quasi-Experimental Studies, Pretest-Posttest Design, Control Group, Adult, Middle Age, Aged, Psychological Well-Being Evaluation, Adult: 19-44 years, Middle Aged: 45-64 years, Aged: 65+ years, Amyotrophic Lateral Sclerosis, Caregivers, Family Relations, Life Purpose, Psychological Well-Being, Disease Progression, Quality of Life, Human, Severity of Illness, Prospective Studies, Descriptive Research, Descriptive Statistics, Quality of Life Evaluation, Time Factors, Psychological Well-Being Evaluation, </Subjects>
  </citation>
  <citation>
    <Author>Chimluang, Janya</Author>
    <Title>Effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer.</Title>
    <Journal>European Journal of Oncology Nursing</Journal>
    <Year>2017</Year>
    <Volume>31</Volume>
    <StartPage>46</StartPage>
    <PageCount>6</PageCount>
    <Abstract>Purpose To evaluate the effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer. Methods This quasi-experimental research study had pre- and post-test control groups. The experimental group received conventional care and an intervention based on basic Buddhist principles for three consecutive days, including seven activities based on precept activities, concentration activities and wisdom activities. The control group received conventional care alone. Results Forty-eight patients participated in this study: 23 in the experimental group and 25 in the control group. Their mean age was 53 (standard deviation 10) years. The spiritual well-being of participants in the experimental group was significantly higher than that of participants in the control group at the second post-test ( P &lt; 0.05). Conclusions An intervention based on basic Buddhist principles improved the spiritual well-being of patients with terminal cancer. This result supports the beneficial effects of implementing this type of intervention for patients with terminal cancer.</Abstract>
    <DOI>http://dx.doi.org/10.1016/j.ejon.2017.08.005</DOI>
    <URL>http://search.ebscohost.com/login.aspx?direct=true&amp;db=jlh&amp;AN=126392076&amp;site=ehost-live</URL>
    <Subjects>Terminally Ill Patients, Cancer Patients, Psychological Well-Being, Spiritual Care, Buddhism, Terminal Care, Human, Quasi-Experimental Studies, Pretest-Posttest Design, Control Group, Adult, Middle Age, Aged, Psychological Well-Being Evaluation, Adult: 19-44 years, Middle Aged: 45-64 years, Aged: 65+ years, Amyotrophic Lateral Sclerosis, Caregivers, Family Relations, Life Purpose, Psychological Well-Being, Disease Progression, Quality of Life, Human, Severity of Illness, Prospective Studies, Descriptive Research, Descriptive Statistics, Quality of Life Evaluation, Time Factors, Psychological Well-Being Evaluation, </Subjects>
  </citation>
</citations>

正如您所看到的,我正在获得记录1的副本。我尝试将“for-reach select”更改为几个不同的值而不做任何更改,包括rec @resultID。

我希望能找到关于xslt代码出错的一些指导,以便我可以生成唯一的输出。

感谢您的帮助

安德鲁

2 个答案:

答案 0 :(得分:1)

代码中的错误是您使用/records//au之类的绝对路径,而不是au循环中的xsl:for-each之类的相对路径。
简而言之:您的xsl:for-each循环是正确且有效的,但您并没有真正使用它。 xsl:for-each循环将每次迭代中的上下文节点设置为新值,此处为所需的下一个rec节点。然后,您可以使用auatljtl ...

相对引用此当前节点

在下面,更改了模板,我使用了xsl:variable来缩短相对路径。我只使用了第一个au元素,您可以根据需要进行更改。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <citations>
            <xsl:for-each select="/records/rec">
                <xsl:variable name="hdr" select="header/controlInfo" />
                <citation>
                    <Author><xsl:value-of select="$hdr/artinfo/aug/au[1]"/></Author>
                    <Title><xsl:value-of select="$hdr/artinfo/tig/atl"/></Title>
                    <Journal><xsl:value-of select="$hdr/jinfo/jtl"/></Journal>
                    <Year><xsl:value-of select="$hdr/pubinfo/dt/@year"/></Year>
                    <Volume><xsl:value-of select="$hdr/pubinfo/vid"/></Volume>
                    <StartPage><xsl:value-of select="$hdr/artinfo/ppf"/></StartPage>
                    <PageCount><xsl:value-of select="$hdr/artinfo/ppct"/></PageCount>
                    <Abstract><xsl:value-of select="$hdr/artinfo/ab"/></Abstract>
                    <DOI>http://dx.doi.org/<xsl:value-of select="$hdr/artinfo/ui"/></DOI>
                    <URL><xsl:value-of select="header/displayInfo/pLink/url"/></URL>
                    <Subjects><xsl:for-each select="$hdr/artinfo/sug/subj"><xsl:value-of select="text()"/>, </xsl:for-each></Subjects>
                </citation>
            </xsl:for-each>
        </citations>
    </xsl:template>
</xsl:stylesheet>

例如,也可以使用.//au,但如果更改了某些内容,则指定像header/controlInfo/artinfo/aug/au[1]这样的整个路径会更清晰,更不容易出错。这只是一个选择问题。

答案 1 :(得分:0)

另一种使其工作的方法是在select-statement语句的位置之前添加current():

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <citations>
            <xsl:for-each select="records/rec">
                <citation>
                    <Author><xsl:value-of select="current()//au"/></Author>
                    <Year><xsl:value-of select="current()//@year"/></Year>
                    <Title><xsl:value-of select="current()//atl"/></Title>
                    <Abstract><xsl:value-of select="current()//ab"/></Abstract>
                    <Journal><xsl:value-of select="current()//jtl"/></Journal>
                    <Volume><xsl:value-of select="current()//vid"/></Volume>
                    <StartPage><xsl:value-of select="current()//ppf"/></StartPage>
                    <PageCount><xsl:value-of select="current()//ppct"/></PageCount>
                    <DOI>http://dx.doi.org/<xsl:value-of select="current()//ui"/></DOI>
                    <URL><xsl:value-of select="current()//url"/></URL>
                    <Subjects><xsl:for-each select="current()//subj"><xsl:value-of select="text()"/>, </xsl:for-each></Subjects>
                </citation>
            </xsl:for-each>
        </citations>
    </xsl:template>
</xsl:stylesheet>