XSLT2:如何根据优先级匹配单个节点
XSLT2: How to match a single node based on priority
我需要创建一个新的 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,但我的技能水平还不够好,无法让它发挥作用。任何帮助将不胜感激。
干杯。
So if an image version "a" exist, then only that image is chosen. if
it doesn't then look for "16x9s" and finally "4x3s".
那是
select="(image[@version-type='a'], image[@version-type='16x9s'],
image[@version-type='4x3s'])[1]"
按照 Kay 博士的建议,创建一个序列并使用该序列中的第一个匹配项。
这是一个完整的例子。
XML 输入
<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>
XSLT 2.0
<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>
XML输出
<io>
<publication id="106125"/>
<related-images>
<PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1112835">
<PTR:width>413</PTR:width>
<PTR:height>550</PTR:height>
<PTR:src>/archive/01112/Thomas-Hicks-16_11128353x4s.jpg</PTR:src>
<PTR:title>Some text</PTR:title>
<PTR:description>null</PTR:description>
<PTR:author/>
</PTR:image>
<PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1111482">
<PTR:width>640</PTR:width>
<PTR:height>363</PTR:height>
<PTR:src>/archive/01111/1904_tugofwar_11114824x3s.jpg</PTR:src>
<PTR:title>Some text</PTR:title>
<PTR:description>Some text</PTR:description>
<PTR:author/>
</PTR:image>
<PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1111465">
<PTR:width>1499</PTR:width>
<PTR:height>1103</PTR:height>
<PTR:src>/archive/01111/1896_1111465a.jpg</PTR:src>
<PTR:title>Some text</PTR:title>
<PTR:description>Some text</PTR:description>
<PTR:author>Getty Images</PTR:author>
</PTR:image>
</related-images>
</io>
我需要创建一个新的 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,但我的技能水平还不够好,无法让它发挥作用。任何帮助将不胜感激。 干杯。
So if an image version "a" exist, then only that image is chosen. if it doesn't then look for "16x9s" and finally "4x3s".
那是
select="(image[@version-type='a'], image[@version-type='16x9s'],
image[@version-type='4x3s'])[1]"
按照 Kay 博士的建议,创建一个序列并使用该序列中的第一个匹配项。
这是一个完整的例子。
XML 输入
<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>
XSLT 2.0
<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>
XML输出
<io>
<publication id="106125"/>
<related-images>
<PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1112835">
<PTR:width>413</PTR:width>
<PTR:height>550</PTR:height>
<PTR:src>/archive/01112/Thomas-Hicks-16_11128353x4s.jpg</PTR:src>
<PTR:title>Some text</PTR:title>
<PTR:description>null</PTR:description>
<PTR:author/>
</PTR:image>
<PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1111482">
<PTR:width>640</PTR:width>
<PTR:height>363</PTR:height>
<PTR:src>/archive/01111/1904_tugofwar_11114824x3s.jpg</PTR:src>
<PTR:title>Some text</PTR:title>
<PTR:description>Some text</PTR:description>
<PTR:author/>
</PTR:image>
<PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1111465">
<PTR:width>1499</PTR:width>
<PTR:height>1103</PTR:height>
<PTR:src>/archive/01111/1896_1111465a.jpg</PTR:src>
<PTR:title>Some text</PTR:title>
<PTR:description>Some text</PTR:description>
<PTR:author>Getty Images</PTR:author>
</PTR:image>
</related-images>
</io>