我目前正在学习XSLT,但对我来说做任何有用的事情仍然太朦胧。你能不能给我一个提示(或更好的样式表)来转换像这样的所有元素
CREATE TABLE tab
( "empid" NUMBER(5,0),
"depid" NUMBER(7,0),
"somedt" DATE
);
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-JAN-11 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-FEB-11 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-MAR-11 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-APR-11 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-MAY-11 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-JUN-11 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-MAY-12 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,12868,to_date('05-JUN-12 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780018,17023,to_date('05-FEB-11 16:54:00','DD-MON-RR HH24:MI:SS'));
Insert into TAB (EMPID,DEPID,SOMEDT) values (780019,12868,to_date('05-FEB-11 16:54:00','DD-MON-RR HH24:MI:SS'));
进入这个?
<match target="font">
<test name="family" compare="eq">
<string>Foo</string>
<string>Bar</string>
<string>Baz</string>
</test>
<edit name="embeddedbitmap" mode="assign"><bool>false</bool></edit>
</match>
谢谢。
编辑:我正在学习并尝试使用XSLT 1.
答案 0 :(得分:0)
您没有指定您正在使用的XSLT版本,所以我假设是XSLT1。也, 你的示例输出缺少根元素,这意味着它不是格式良好的XML,所以我添加了一个。
这是一个选项:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" indent="yes"/>
<!-- Match the root node of this document -->
<xsl:template match="/">
<!-- Create the <root> element so that the document has a root element. -->
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<!-- Match the <match> element. -->
<xsl:template match="match">
<xsl:apply-templates select="test/string" mode="match"/>
</xsl:template>
<xsl:template match="string" mode="match">
<!--
Apply the closest ancestor <match> element of the current <string> element.
-->
<xsl:apply-templates select="ancestor::match[1]" mode="match">
<!--
Pass the current string element as a parameter to the matched template.
-->
<xsl:with-param name="string" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="match" mode="match">
<!--
If you're using XSLT2, you can use a tunnel parameter, in which case you
don't have to specify the parameter here.
-->
<xsl:param name="string"/>
<!-- Copy this <match> element into the output as is. -->
<xsl:copy>
<!-- Apply the attributes of this element with the default mode. -->
<xsl:apply-templates select="@*"/>
<!--
Apply the child nodes (element or text) with the "match" mode. Pass the
<string> element we passed as a parameter earlier on to the applied
templates.
-->
<xsl:apply-templates select="node()" mode="match">
<xsl:with-param name="string" select="$string"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="test" mode="match">
<!--
Remember, this parameter points to <string>Foo</string>,
<string>Bar</string>, or <string>Baz</string>.
-->
<xsl:param name="string"/>
<!-- Copy this element as is. -->
<xsl:copy>
<!-- Copy its attributes as is, too. -->
<xsl:apply-templates select="@*"/>
<!--
Don't copy any of the child elements as is: rather, create a copy of the
<string> element we passed as a parameter here.
-->
<xsl:copy-of select="$string"/>
</xsl:copy>
</xsl:template>
<!--
For every other attribute, element or text node that is applied with the
"match" mode, defer to the default mode. In this case, that means deferring
to the identity transform template (see below).
-->
<xsl:template match="@* | node()" mode="match">
<xsl:apply-templates select="."/>
</xsl:template>
<!--
Identity transform: every attribute or element matched by this template into
the output as is.
-->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出:
<root>
<match target="font">
<test name="family" compare="eq">
<string>Foo</string>
</test>
<edit name="embeddedbitmap" mode="assign">
<bool>false</bool>
</edit>
</match>
<match target="font">
<test name="family" compare="eq">
<string>Bar</string>
</test>
<edit name="embeddedbitmap" mode="assign">
<bool>false</bool>
</edit>
</match>
<match target="font">
<test name="family" compare="eq">
<string>Baz</string>
</test>
<edit name="embeddedbitmap" mode="assign">
<bool>false</bool>
</edit>
</match>
</root>