使用XSLT重新构造节点
我有以下XML:使用XSLT重新构造节点,xslt,Xslt,我有以下XML: <myreport> <media> <assets> <asset> <type>image</type> <H>224 mm</H> <W>154 mm</W> </asset> <asset> <type>v
<myreport>
<media>
<assets>
<asset>
<type>image</type>
<H>224 mm</H>
<W>154 mm</W>
</asset>
<asset>
<type>video</type>
<H>480 px</H>
<W>600 px</W>
</asset>
</assets>
</myreport>
形象
224毫米
154毫米
视频
480像素
600像素
我需要重组如下:
<myreport>
<media>
<assets>
<image>
<H>224 mm</H>
<W>154 mm</W>
</image>
<video>
<H>480 px</H>
<W>600 px</W>
<video>
</assets>
</media>
</myreport>
224毫米
154毫米
480像素
600像素
我如何将类型与高度(H)宽度(W)匹配,以实现欲望转换。我使用xsl:value of select=“node”进行正常的重组 这可以通过修改后的。要匹配
资产
元素的模板,不要复制资产
元素,而是使用其类型元素的值作为要创建的元素的名称,然后将模板应用于其其余子元素。一个(空)模板,它将抑制类型
元素和任何仅包含空格的text()
节点
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--instead of an asset element, create an element named after it's type-->
<xsl:template match="asset[type]">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<!--suppress the type element and whitespace-only text nodes -->
<xsl:template match="asset/type | text()[not(normalize-space())]"/>
</xsl:stylesheet>
从标识转换开始,该转换复制节点在输入XML中的显示:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
<xsl:output method="xml" indent="yes"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
</xsl:stylesheet>
注意,name={type}
将根据子type
元素的值命名输出的元素
抑制类型
元素:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
<xsl:output method="xml" indent="yes"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
</xsl:stylesheet>
澄清输出格式:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
<xsl:output method="xml" indent="yes"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
</xsl:stylesheet>
总共:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
<xsl:output method="xml" indent="yes"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="asset">
<xsl:element name="{type}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="type"/>
</xsl:stylesheet>
你能发布你的XSLT吗?你能在最后一行向我解释一下“text()[not(normalize-space())]”的作用吗?它是一个谓词过滤器,用于匹配纯空白字符的text()节点。函数normalize-space()将折叠连续的空格,如果只有空白,则返回nothing/empty,这在谓词测试中是“falsy”。使用not()可确保其为真,从而匹配仅空白的文本。如果删除该匹配表达式,您将在输出中看到一些奇怪的缩进。您还可以通过将应用模板更改为select=“*”并仅处理子元素来排除它们。