我想在xsl中重命名一个处理指令。我的输入看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<material xml:lang="en-us">
<title>
<?PI_start author="joepublic" comment="Comment #1" ?>Discovering
<?PI_end?>XML
</title>
<related-stuff>
<?PI_start author="johndoe" comment="Comment #3" ?>
<a href="otherdoc.xml" />
<?PI_end?>
</related-stuff>
</material>
我想将处理指令从'PI'重命名为'otherPI',并将属性"author"重命名为"user"。
结果如下所示:
<?xml version="1.0" encoding="utf-8"?>
<material xml:lang="en-us">
<title>
<?otherPI_start user="joepublic" comment="Comment #1"?>Discovering
<?otherPI_end?>XML
</title>
<related-stuff>
<?otherPI_start user="johndoe" comment="Comment #3"?>
<a href="otherdoc.xml" />
<?otherPI_end?>
</related-stuff>
</material>
你能帮我在xsl中找到匹配的语句吗?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('PI_start')">
<xsl:copy>
<!-- What should I put here??? -->
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('PI_end')">
<xsl:copy>
<!-- What should I put there??? -->
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
要重命名一条处理指令,您可以这样做:
<xsl:template match="processing-instruction('PI_start')">
<xsl:processing-instruction name="otherPI_start">
<xsl:value-of select="." />
</xsl:processing-instruction>
</xsl:template>
如果您还想修改内容,例如将author="joepublic"
更改为user="joepublic"
,则必须使用字符串操作来完成此操作-例如:
<xsl:template match="processing-instruction('PI_start')">
<xsl:processing-instruction name="otherPI_start">
<xsl:value-of select="substring-before(., 'author=')" />
<xsl:text>user=</xsl:text>
<xsl:value-of select="substring-after(., 'author=')" />
</xsl:processing-instruction>
</xsl:template>