XSLT:识别 <xsl:copy> 是否做了某事
XSLT: recognize if <xsl:copy> did something
我想将规范的 xml 文件转换为另一个规范。所以理论上所有的 和@attributes 都是已知的。但有一点可能性是该文件中存在不符合规范的手写信息。
我做了以下事情:
XML-文件
<tasklist>
<task1 enabled="true">
<param>123</param>
</task1>
<task2 enabled="true">
<param>123</param>
</task2>
<randomhandwritten>some important information</randomhandwritten>
</tasklist>
XSL 文件
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:message>Match <xsl:copy-of select="."/> </xsl:message>
</xsl:copy>
</xsl:template>
<xsl:template match="tasklist/task1">
<xsl:variable name="content" select="param"/>
<screen enabled="true" param="{$content}"/>
</xsl:template>
<xsl:template match="tasklist/task2">
<xsl:variable name="content" select="param"/>
<printer enabled="true" param="{$content}"/>
</xsl:template>
</xsl:stylesheet>
通常不会有任何手写元素,所以所有的工作都是模板完成的,复制不应该做任何事情。但是如果有人 DID 在其中写入元素,有没有办法警告我的 XSL 文件的用户复制方法实际上做了什么?
否则,我会在没有复制方法的情况下应用相同的脚本,如果它们执行相同的输出,则比较它们,但这会……有点脏。
非常感谢!
雷内克
编辑:
感谢 xsl:message 的帮助!!
使用显示的 XSL 文件,我得到
的输出
Match some important information
还有
Match
123
123
some important information
我不明白为什么所有元素内容都显示在一场比赛中?谁能告诉我如何省略最后一场比赛,或者我应该接受它吗?
要让 XSLT 输出结果文档以外的内容,您可以在 XSLT 1.0 http://www.w3.org/TR/xslt#message and additionally <xsl:message select="..."/>
in XSLT 2.0 http://www.w3.org/TR/xslt20/#message.
中使用 <xsl:message>...</xsl:message>
因此 <xsl:template match="node()"><xsl:message>Match on <xsl:copy-of select="."/></xsl:message>...
或 <xsl:template match="node()"><xsl:message select="concat('Match on ', name())"/>...
将输出一条消息,显示匹配的节点及其名称。
我想将规范的 xml 文件转换为另一个规范。所以理论上所有的
XML-文件
<tasklist>
<task1 enabled="true">
<param>123</param>
</task1>
<task2 enabled="true">
<param>123</param>
</task2>
<randomhandwritten>some important information</randomhandwritten>
</tasklist>
XSL 文件
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:message>Match <xsl:copy-of select="."/> </xsl:message>
</xsl:copy>
</xsl:template>
<xsl:template match="tasklist/task1">
<xsl:variable name="content" select="param"/>
<screen enabled="true" param="{$content}"/>
</xsl:template>
<xsl:template match="tasklist/task2">
<xsl:variable name="content" select="param"/>
<printer enabled="true" param="{$content}"/>
</xsl:template>
</xsl:stylesheet>
通常不会有任何手写元素,所以所有的工作都是模板完成的,复制不应该做任何事情。但是如果有人 DID 在其中写入元素,有没有办法警告我的 XSL 文件的用户复制方法实际上做了什么? 否则,我会在没有复制方法的情况下应用相同的脚本,如果它们执行相同的输出,则比较它们,但这会……有点脏。
非常感谢! 雷内克
编辑: 感谢 xsl:message 的帮助!! 使用显示的 XSL 文件,我得到
的输出Match some important information
还有
Match
123
123
some important information
我不明白为什么所有元素内容都显示在一场比赛中?谁能告诉我如何省略最后一场比赛,或者我应该接受它吗?
要让 XSLT 输出结果文档以外的内容,您可以在 XSLT 1.0 http://www.w3.org/TR/xslt#message and additionally <xsl:message select="..."/>
in XSLT 2.0 http://www.w3.org/TR/xslt20/#message.
<xsl:message>...</xsl:message>
因此 <xsl:template match="node()"><xsl:message>Match on <xsl:copy-of select="."/></xsl:message>...
或 <xsl:template match="node()"><xsl:message select="concat('Match on ', name())"/>...
将输出一条消息,显示匹配的节点及其名称。