XSLT2:如何根据优先级匹配单个节点

时间:2016-08-12 07:37:06

标签: xslt-2.0

我需要创建一个新的xml结构,其中只存在一个图像版本,并且该版本应该是" a"," 16x9s"或" 4x3s" - 在那个优先事项。因此,如果图像版本" a"存在,然后只选择那个图像。如果它没有找到" 16x9s"最后" 4x3s"。 还需要一些其他节点(在下面想要的结果代码中的大写字母),而所有其他节点都应该被丢弃。

xml(摘录,可以更长或更短):

<?xml version="1.0" encoding="iso-8859-1"?>
<io>
    <publication id="106125"/>
    <related-images>
        <image id="1112835">
            <caption>null</caption>
            <placement>FRONTPAGETITLE</placement>
            <alignment>left</alignment>
            <photographer></photographer>
            <credit></credit>
            <name>Some text</name>
            <author></author>
            <copyright></copyright>
            <path>Image Root</path>
            <description></description>
            <alt-text></alt-text>
            <image-version version-id="2060510" version-type="16x9s">
                <url>/archive/01112/Thomas-Hicks-1_111283516x9s.jpg</url>
                <width>649</width>
                <height>365</height>
            </image-version>
            <image-version version-id="2060512" version-type="a">
                <url>/archive/01112/Thomas-Hicks-16_11128353x4s.jpg</url>
                <width>413</width>
                <height>550</height>
            </image-version>
            <image-version version-id="2060513" version-type="4x3s">
                <url>/archive/01112/Thomas-Hicks-16_11128354x3s.jpg</url>
                <width>550</width>
                <height>413</height>
            </image-version>
        </image>
        <image id="1111482">
            <caption>Some text</caption>
            <placement>FRONTPAGELEADTEXT</placement>
            <alignment>left</alignment>
            <photographer></photographer>
            <credit></credit>
            <name>Some text</name>
            <author></author>
            <copyright></copyright>
            <path>Image Root</path>
            <description></description>
            <alt-text></alt-text>
            <image-version version-id="2060099" version-type="4x3s">
                <url>/archive/01111/1904_tugofwar_11114824x3s.jpg</url>
                <width>640</width>
                <height>363</height>
            </image-version>
            <image-version version-id="2060075" version-type="f-tn">
                <url>/archive/01111/1904_tugofwar_1111482f-tn.jpg</url>
                <width>200</width>
                <height>113</height>
            </image-version>
            <image-version version-id="2060094" version-type="y">
                <url>/archive/01111/1904_tugofwar_1111482y.jpg</url>
                <width>484</width>
                <height>274</height>
            </image-version>
        </image>
        <image id="1111465">
            <caption>Some text</caption>
            <placement>FRONTPAGELEADTEXT</placement>
            <alignment>left</alignment>
            <photographer>Getty Images</photographer>
            <credit></credit>
            <name>Some text</name>
            <author></author>
            <copyright></copyright>
            <path>Image Root</path>
            <description></description>
            <alt-text></alt-text>
            <image-version version-id="2058398" version-type="a">
                <url>/archive/01111/1896_1111465a.jpg</url>
                <width>1499</width>
                <height>1103</height>
            </image-version>
        </image>
    </related-images>
</io>

想要的结果:

<PTR:image>
    <PTR:width>WIDTH OF IMAGE</PTR:width>
    <PTR:height>HEIGHT OF IMAGE</PTR:height>

    <PTR:src>IMAGE URL</PTR:src>

    <PTR:title>
        NAME
    </PTR:title>

    <PTR:description>
        CAPTION
    </PTR:description>

    <PTR:author>
        PHOTOGRAPHER
    </PTR:author>
</PTR:image>

我尝试了一些事情,比如xsl:when,但是我的技能水平还不够好,无法让它发挥作用。任何帮助将不胜感激。 欢呼声。

2 个答案:

答案 0 :(得分:1)

像Kay博士建议的那样,创建一个序列并使用该序列中的第一个序列。

这是一个完整的例子。

XML输入

# ...
with tf.variable_scope("first_lstm"):
  lstm_cell = rnn_cell.BasicLSTMCell(ukryta_warstwa, forget_bias=1.0)
  _X = tf.split(0, liczba_krokow, _X)

  outputs, states = rnn.rnn(lstm_cell, _X, initial_state=_istate)

with tf.variable_scope("second_lstm"):
  lstm_cell_2 = rnn_cell.BasicLSTMCell(ukryta_warstwa, forget_bias=1.0)
  outputs2, states2 = rnn.rnn(lstm_cell_2, outputs, initial_state=_istate_2)
# ...

XSLT 2.0

<io>
    <publication id="106125"/>
    <related-images>
        <image id="1112835">
            <caption>null</caption>
            <placement>FRONTPAGETITLE</placement>
            <alignment>left</alignment>
            <photographer></photographer>
            <credit></credit>
            <name>Some text</name>
            <author></author>
            <copyright></copyright>
            <path>Image Root</path>
            <description></description>
            <alt-text></alt-text>
            <image-version version-id="2060510" version-type="16x9s">
                <url>/archive/01112/Thomas-Hicks-1_111283516x9s.jpg</url>
                <width>649</width>
                <height>365</height>
            </image-version>
            <image-version version-id="2060512" version-type="a">
                <url>/archive/01112/Thomas-Hicks-16_11128353x4s.jpg</url>
                <width>413</width>
                <height>550</height>
            </image-version>
            <image-version version-id="2060513" version-type="4x3s">
                <url>/archive/01112/Thomas-Hicks-16_11128354x3s.jpg</url>
                <width>550</width>
                <height>413</height>
            </image-version>
        </image>
        <image id="1111482">
            <caption>Some text</caption>
            <placement>FRONTPAGELEADTEXT</placement>
            <alignment>left</alignment>
            <photographer></photographer>
            <credit></credit>
            <name>Some text</name>
            <author></author>
            <copyright></copyright>
            <path>Image Root</path>
            <description></description>
            <alt-text></alt-text>
            <image-version version-id="2060099" version-type="4x3s">
                <url>/archive/01111/1904_tugofwar_11114824x3s.jpg</url>
                <width>640</width>
                <height>363</height>
            </image-version>
            <image-version version-id="2060075" version-type="f-tn">
                <url>/archive/01111/1904_tugofwar_1111482f-tn.jpg</url>
                <width>200</width>
                <height>113</height>
            </image-version>
            <image-version version-id="2060094" version-type="y">
                <url>/archive/01111/1904_tugofwar_1111482y.jpg</url>
                <width>484</width>
                <height>274</height>
            </image-version>
        </image>
        <image id="1111465">
            <caption>Some text</caption>
            <placement>FRONTPAGELEADTEXT</placement>
            <alignment>left</alignment>
            <photographer>Getty Images</photographer>
            <credit></credit>
            <name>Some text</name>
            <author></author>
            <copyright></copyright>
            <path>Image Root</path>
            <description></description>
            <alt-text></alt-text>
            <image-version version-id="2058398" version-type="a">
                <url>/archive/01111/1896_1111465a.jpg</url>
                <width>1499</width>
                <height>1103</height>
            </image-version>
        </image>
    </related-images>
</io>

XML输出

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:PTR="whatever the PTR namespace uri is">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="image">
    <xsl:variable name="targetImage" select="
      (image-version[@version-type='a'],
       image-version[@version-type='16x9s'],
       image-version[@version-type='4x3s'])[1]"/>
    <PTR:image>
      <xsl:apply-templates select="@*"/>
      <PTR:width><xsl:value-of select="$targetImage/width"/></PTR:width>
      <PTR:height><xsl:value-of select="$targetImage/height"/></PTR:height>
      <PTR:src><xsl:value-of select="$targetImage/url"/></PTR:src>
      <PTR:title><xsl:value-of select="name"/></PTR:title>
      <PTR:description><xsl:value-of select="caption"/></PTR:description>
      <PTR:author><xsl:value-of select="photographer"/></PTR:author>
    </PTR:image>
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

  

因此,如果图像版本&#34; a&#34;存在,然后只选择那个图像。如果   它没有找到&#34; 16x9s&#34;最后&#34; 4x3s&#34;。

那是

select="(image[@version-type='a'], image[@version-type='16x9s'],
           image[@version-type='4x3s'])[1]"
相关问题