输入:
<?xml version="1.0" encoding="utf-8" ?>
<products>
<product ID="123">
<Product Name>Sample Name 1</Product Name>
<Images>
<Image>url1</Image>
<Image>url2</Image>
<Image>url3</Image>
</Images>
</product>
<product ID="456">
<Product Name>Sample Name 2</ProductName>
<Images>
<Image>url4</Image>
<Image>url5</Image>
<Image>url6</Image>
</Images>
</product>
</products>
输出:
<?xml version="1.0" encoding="utf-8" ?>
<products>
<product ID="123">
<ProductName>Sample Name 1</ProductName>
<Image>url1</Image>
</product>
<product ID="456">
<ProductName>Sample Name 2</ProductName>
<Image>url4</Image>
</product>
</products>
正如你在上面看到的那样,是两次一次改变:
元素标签“Product Name”更改为“ProductName”。
每个产品都有多个“Image”元素嵌套在“Images”元素下,其中只保留了第一个元素,而其他元素则被丢弃并在层次结构中显示。
一个xslt可以这样做吗?
在同一个文件上多次执行xslt转换时,是否也可能没有错误? 上次请求,我找不到合适的标题/标签。请建议一些其他人更容易找到的东西。我会更新它(如果允许)。
答案 0 :(得分:0)
这不起作用,因为您的XML 格式不正确。 元素名称中不允许使用空格。如果XML格式良好,很容易满足您的请求2。
假设你以某种方式得到格式良好的XML。比拥有身份转换 e.g.
的厕所然后为图像添加模板,仅对第一个图像进行对比:
<xsl:template match="Images">
<xsl:copy>
<xsl:apply-templates select=" @* | Image[1]"/>
</xsl:copy>
</xsl:template>
答案 1 :(得分:0)
是的,这是可能的。实际上,遵循非常简单的模板可以完成这项工作。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes" />
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="Images">
<xsl:copy-of select="Image[1]" />
</xsl:template>
</xsl:stylesheet>