我在更改DTD后转换了一些XML文件。
其中一项更改涉及将处理指令更改为其他元素的属性 - 但我无法从指令中选择特定信息。我还没有找到如何使用PI的方法 - 我发现的几乎所有内容都是关于如何创建它们 - 但我确实找到了这个:http://doccookbook.sourceforge.net/html/en/dbc.common.pi.html
例如 - 原始XML:
<?Fm Condition FM8_SYSTEM_HIDEELEMENT Dark%20Grey NO_OVERRIDE hide AsIs?>
<?Fm Condition FM8_TRACK_CHANGES_ADDED Forest%20Green SINGLE_UNDERLINE show AsIs?>
<?Fm Condition FM8_TRACK_CHANGES_DELETED Red STRIKETHROUGH show AsIs?>
<products>
<?Fm Condstart p-version?>
<prod>
<form>inj</form>
<strength>10, 20, 50</strength>
</prod>
<?Fm Condend p-version?>
<?Fm Condstart e-version?>
<prod>
<form>inj</form>
<strength>10</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<prod>
<form>inj</form>
<strength>20</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<prod>
<form>inj</form>
<strength>50</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<?Fm Condend e-version?>
</products>
我试过这个XSL模板
<xsl:template match="processing-instruction()">
<xsl:choose>
<xsl:when test="'Condstart'">
<xsl:text>ELEPHANTS</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:copy />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
但它将文档中的所有处理指令转换为&#34; ELEPHANTS&#34; - 不只是我试图选择的那个。
答案 0 :(得分:3)
你想要这样的东西:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"processing-instruction()[starts-with(.,'Condstart')]">
<xsl:text>ELEPHANTS</xsl:text>
</xsl:template>
</xsl:stylesheet>
当对提供的(格式正确的)XML文档应用此转换时:
<?Fm Condition FM8_SYSTEM_HIDEELEMENT Dark%20Grey NO_OVERRIDE hide AsIs?>
<?Fm Condition FM8_TRACK_CHANGES_ADDED Forest%20Green SINGLE_UNDERLINE show AsIs?>
<?Fm Condition FM8_TRACK_CHANGES_DELETED Red STRIKETHROUGH show AsIs?>
<products>
<?Fm Condstart p-version?>
<prod>
<form>inj</form>
<strength>10, 20, 50</strength>
</prod>
<?Fm Condend p-version?>
<?Fm Condstart e-version?>
<prod>
<form>inj</form>
<strength>10</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<prod>
<form>inj</form>
<strength>20</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<prod>
<form>inj</form>
<strength>50</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<?Fm Condend e-version?>
</products>
产生了想要的正确结果:
<?Fm Condition FM8_SYSTEM_HIDEELEMENT Dark%20Grey NO_OVERRIDE hide AsIs?>
<?Fm Condition FM8_TRACK_CHANGES_ADDED Forest%20Green SINGLE_UNDERLINE show AsIs?>
<?Fm Condition FM8_TRACK_CHANGES_DELETED Red STRIKETHROUGH show AsIs?>
<products>
ELEPHANTS
<prod>
<form>inj</form>
<strength>10, 20, 50</strength>
</prod>
<?Fm Condend p-version?>
ELEPHANTS
<prod>
<form>inj</form>
<strength>10</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<prod>
<form>inj</form>
<strength>20</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<prod>
<form>inj</form>
<strength>50</strength>
<packsize>0.4 mL (syringe), 4</packsize>
</prod>
<?Fm Condend e-version?>
</products>