xml - 如何在列出的xml内容中使用xsl:for-each选择第二个节点?

标签 xml xslt xpath

我在单独的xml文件中列出了图形名称以及宽度和高度信息,

我需要匹配源xml文件中的图形名称,并且需要将宽度和高度信息作为属性插入源xml文件中。

这是源xml文件。

<graphic name="sample.jpg" align="center"/>


预期的输出xml文件

<graphic name="sample.jpg" width="100" depth="200" align="center"/>


图测量xml

<figure>
<name>sample.jpg</name>
<width>100</width>
<height>200</height>
</figure>


我已将源文件名属性值存储在“ $ names”中。我已将图形测量文件名属性值存储在“ $ figname”中。

我的xsl脚本

<xsl:for-each select="@name">
<xsl:if test="$figname=$name">
<xsl:attribute name="width"><xsl:value-of select="document('figure.xml')/figure/width"/></xsl:attribute>
<xsl:attribute name="depth"><xsl:value-of select="document('figure.xml')/figure/height"/></xsl:attribute>
</xsl:if>
</xsl:for-each>


它仅在第一次运行,而不是全部运行。我有近100幅图像。对于所有图像,我需要宽度和高度值。我的脚本只起作用第一个值。如何选择所有值?

请给我建议..

最好的祝福,
安东尼

-------------------------------------------------- -----------------------------------------

抱歉,我已经使用了您的脚本,但是我无法获得我需要的确切输出。

我在这里给出了所有明确的细节,供您考虑。

输入XML:

<figure>
<title>Earliest Discoveries</title>
<graphic name="luc26959_0101.eps" align="center"/>
<caption>These lithographs of teeth of Iguanodon are from Mantell original 1825 article.</caption>
</figure>


输入图XML:

<?xml version="1.0"?>
<figuregroup>
  <figure><name>luc26959_0101.eps</name><width>500</width><height>347</height></figure>
  <figure><name>luc26959_0102.eps</name><width>500</width><height>352</height></figure>
  <figure><name>luc26959_0103.eps</name><width>500</width><height>348</height></figure>
  <figure><name>luc26959_0104.eps</name><width>445</width><height>263</height></figure>
  <figure><name>luc26959_0105.eps</name><width>217</width><height>250</height></figure>
</figuregroup>


转换XSL代码:

<xsl:template match="graphic">
<xsl:variable name="names">
<xsl:value-of select="substring-before(@name, '.')"/>
</xsl:variable>
<xsl:for-each select="//graphic">
    <imageobject>
        <imagedata>
        <xsl:attribute name="fileref">graphics/<xsl:value-of select="$names"/>.jpg</xsl:attribute>
        <xsl:attribute name="width"><xsl:value-of select="document('../input/fig.xml')/figuregroup/figure[name=$names]/width"/></xsl:attribute>
        <xsl:attribute name="depth"><xsl:value-of select="document('../input/fig.xml')/figuregroup/figure[name=$names]/height"/></xsl:attribute>
        <xsl:apply-templates/>
        </imagedata>
     </imageobject>
</xsl:for-each>
</xsl:template>


当前输出XML:

<figure>
<title>Earliest Discoveries</title>
<mediaobject>
    <imageobject><imagedata fileref="graphics/luc26959_0101.jpg" width="" depth=""/></imageobject>
    <imageobject><imagedata fileref="graphics/luc26959_0101.jpg" width="" depth=""/></imageobject>
    <imageobject><imagedata fileref="graphics/luc26959_0101.jpg" width="" depth=""/></imageobject>
    <imageobject><imagedata fileref="graphics/luc26959_0101.jpg" width="" depth=""/></imageobject>
    <imageobject><imagedata fileref="graphics/luc26959_0101.jpg" width="" depth=""/></imageobject>
    <caption><para>These lithographs of teeth of <emphasis>Iguanodon</emphasis> are from Mantell's original 1825 article.</para><para/></caption>
</mediaobject>
</figure>


所需的输出:

<figure>
<title>Earliest Discoveries</title>
<mediaobject>
    <imageobject><imagedata fileref="graphics/luc26959_0101.jpg" width="500" depth="347"/></imageobject>
    <caption><para>These lithographs of teeth of <emphasis>Iguanodon</emphasis> are from Mantell's original 1825 article.</para><para/></caption>
</mediaobject>
</figure>


希望我能清楚地说明我的要求,我也尝试了不使用“ for-each”的情况,当我不使用“ for-each”的情况下,我不会重复得到任何输出元素,只有一次我会得到它宽度和深度属性为空。

在使用您的代码后,我不知道如何以正确的方式填充该属性。

请帮我..

感谢和问候,
安东尼

最佳答案

您的方法有很多问题。

首先,只能给变量赋一次值,而不能再赋值。我怀疑您未显示给我们的代码是在假设变量被分配多次的情况下分配变量的。

其次,<for-each select="@name">将仅选择一件事:name属性(如果存在)在当前上下文中。您可能更想要<for-each select="//graphic">之类的东西。

然后就是使用figure.xml函数访问document的方式。您的示例将仅获得第一个高度和宽度,而不管图像的名称如何。您需要使用以下内容过滤结果:

<xsl:attribute name="width">
  <xsl:value-of select="document('figure.xml')//figure[name=$name]/width"/>
</xsl:attribute>


假设您已经正确设置了$name,这是我在第一点中谈到的。

您没有给出所有麻烦的XSLT代码段,因此我无法给您进行全面的更正。不过,从您给我的东西来看,我认为您将需要放弃已有的东西并重新评估您的方法。您正在做的事情并不复杂,但是您显然是XSLT以及功能编程的新手。这是一个入门的示例:

<xsl:template match="/">
  <xsl:for-each select="//graphic">
    <xsl:variable name="name" select="@name"/>
    <xsl:copy>
      <!-- stuff to copy the attributes/children you want, left to the OP -->
      <xsl:attribute name="width">
        <xsl:value-of 
          select="document('figure.xml')//figure[name=$name]/width"/>
      </xsl:attribute>
      <xsl:attribute name="height">
        <xsl:value-of 
          select="document('figure.xml')//figure[name=$name]/height"/>
      </xsl:attribute>
    </xsl:copy>
  </xsl:for-each>
</xsl:template>




编辑

我可以肯定地说的一件事是,您确实需要重温XSLT basics。您遇到的问题相对简单,并且大多数似乎是基于这样一种信念,即XSLT比实际要聪明。就是说,这是一个我相信可以满足您需求的脚本:

<xsl:template match="graphic">
  <!-- Get the name of the file in the right dir, with a .jpg extension -->
  <xsl:variable
    name="filename" 
    select="concat('graphics/',substring-before(@name, '.'),'.jpg')"
  />

  <!-- Set a variable to preserve the name of the file we're looking up -->
  <xsl:variable name="lookupname" select="@name"/>

  <!-- Look up this particular figure by name for easy access -->
  <xsl:variable
    name="lookup"
    select="document('../input/fig.xml')/figuregroup/figure[name=$lookupname]"
  />

  <imageobject>
    <imagedata>
    <xsl:attribute name="fileref">
      <xsl:value-of select="$filename"/>
    </xsl:attribute>

    <!-- Set the width and depth according to the lookup variable's children -->
    <xsl:attribute name="width">
      <xsl:value-of select="$lookup/width"/>
    </xsl:attribute>
    <xsl:attribute name="depth">
      <xsl:value-of select="$lookup/height"/>
    </xsl:attribute>

    <!-- Apply any more templates as necessary -->
    <xsl:apply-templates/>
    </imagedata>
  </imageobject>
</xsl:template>

关于xml - 如何在列出的xml内容中使用xsl:for-each选择第二个节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1334176/

相关文章:

java - 如何解析在 xml 标签中包含 html 标签的 xml

xslt - 使用 XSLT 将字符串转换为数字

python - 随机点击无限量的所有选项卡

Java XML 修改

c - XML 文件的拆分

java - 如何根据设备有效地更改布局

xml - XSL 根据元素值删除所有前面的 sibling

xslt - 如何检测 XSLT 中的换行符

xml - 选择值小于某个数字的元素

c++ - 具有多个 OR 的 Xpath 查询不适用于 Windows 事件