【问题标题】:i want the first two figure has to be in different tag and last two in different using XSLT我希望前两个数字必须在不同的标签中,最后两个在不同的标签中使用 XSLT
【发布时间】:2017-01-04 10:19:37
【问题描述】:

我有 4 个连续的数字,前两个数字必须带有文本标记,最后两个数字必须使用 XSLT 带有标题标记,

我的输入 XML 文件是:

<images>
<image>
<url>insulin.jpg</url>
<caption>Caption Fig – Text Here</caption>
</image>
<image>
<url>medicine.jpg</url>
<caption>Caption Fig – Text Here</caption>
</image>
<image>
<url>power.jpg</url>
<caption>Caption Fig – Text Here</caption>
</image>
<image>
<url>injection.jpg</url>
<caption>Caption Fig – Text Here</caption>
</image>
</images>

XSLT 我用作:

<xsl:stylesheet version="3.0">

<xsl:template match="images">
images: [
<xsl:apply-templates/>
]
</xsl:template>

<xsl:template match="images/image">
<xsl:if test="position() != 1">,</xsl:if>
{
<xsl:apply-templates/>
}
</xsl:template>

<xsl:template match="image/url">
    <xsl:text>"url": "img/</xsl:text>
    <xsl:value-of select="tokenize(., '/')[last()]"/>",
</xsl:template>

<xsl:template match="image/caption">
    "caption": "<xsl:apply-templates/>",
</xsl:template>

</xsl:stylesheet>

我得到的输出是:

images: [

    {
    "url": "img/insulin.jpg",

    "caption": "Caption Fig – Text Here",

    }
,
    {
    "url": "img/medicine.jpg",

    "caption": "Caption Fig – Text Here",

    }
,
    {
    "url": "img/power.jpg",

    "caption": "Caption Fig – Text Here",

    }
,
    {
    "url": "img/injection.jpg",

    "caption": "Caption Fig – Text Here",

    }

    ]

但我希望前两张图片需要带有文本标签和标题,最后两张图片保持不变

预期的 Json 输出:

images: [

    {
    "url": "img/insulin.jpg",

    "caption": {

    "text": "Caption Fig – Text Here",

    }

    }
,
    {
    "url": "img/medicine.jpg",

    "caption": {

    "text": "Caption Fig – Text Here",

    }

    }
,
    {
    "url": "img/power.jpg",

    "caption": "Caption Fig – Text Here",

    }
,
    {
    "url": "img/injection.jpg",

    "caption": "Caption Fig – Text Here",

    }

    ]

请帮助我。提前致谢。

【问题讨论】:

  • 如果您有超过 4 张图片会怎样?决定什么应该有“文本”和什么应该有“标题”的实际逻辑是什么?
  • @TimC 这类文章只有 4 张图片,不多不少。前两个图像表示带有颜色信息的标题,最后两个图像表示带有普通文本的标题。所以我只想区分前两个与文本的标题和最后两个仅与标题

标签: json xml xslt


【解决方案1】:

一种方法是添加更高优先级的模板以匹配出现在前两个 image 选定元素下的 caption 元素

<xsl:template match="image[position() &lt;= 2]/caption" priority="2">
    "text": "<xsl:apply-templates/>",
</xsl:template>

但要使其正常工作,您应该更改模板中匹配 images 的 &lt;xsl:apply-templates/&gt; 以显式选择 image 元素

<xsl:template match="images">
images: [
<xsl:apply-templates select="image" />
]
</xsl:template>

这是因为否则它可能会拾取空白节点,并影响新模板中的位置检查(因为 position() 指的是所选节点集中的位置,不一定是文档中的位置)。

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="images">
images: [
<xsl:apply-templates select="image" />
]
</xsl:template>

<xsl:template match="images/image">
<xsl:if test="position() != 1">,</xsl:if>
{
<xsl:apply-templates/>
}
</xsl:template>

<xsl:template match="image/url">
    <xsl:text>"url": "img/</xsl:text>
    <xsl:value-of select="tokenize(., '/')[last()]"/>",
</xsl:template>

<xsl:template match="image[position() &lt;= 2]/caption" priority="2">
    "text": "<xsl:apply-templates/>",
</xsl:template>

<xsl:template match="image/caption">
    "caption": "<xsl:apply-templates/>",
</xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多