喂, 首先,对于xml和类似的我不是很熟悉,所以请不要用我的初学者问题来惩罚我:D
我有一个xml文件,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<mainstuff>
<category_major>
<project_name>Dream</project_name>
<project_attribute>Version 1.0</project_attribute>
<category_A></category_A>
<category_B></category_B>
<category_C></category_C>
</category_major>
</mainstuff>
然后我得到一个看起来像这样的XSLT文件:
<?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:copy>
</xsl:template>-->
<xsl:template match="/">
<xsl:element name="mainstuff">
<xsl:attribute name="version">1.0</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="category_major">
<xsl:element name="category_major">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="category_A">
<xsl:element name="category_A">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="category_B">
<xsl:element name="category_B">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="category_C">
<xsl:element name="category_C">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我想避免使用两个参数“project_name”和“project_attribute”。我想要一个这样的结果:
<?xml version="1.0" encoding="utf-8" ?>
<mainstuff>
<category_major>
<category_A></category_A>
<category_B></category_B>
<category_C></category_C>
</category_major>
</mainstuff>
但我得到的是转化后的结果:
<?xml version="1.0" encoding="utf-8"?>
<mainstuff version="1.0">
<category_major>
**Dream
Version 1.0**
<category_A />
<category_B />
<category_C />
</category_major>
</mainstuff>
文字仍在其中。我该如何解决呢?我究竟做错了什么 ?我如何才能实现获取参数但没有文本?在我的例子中输出如下:
<?xml version="1.0" encoding="utf-8" ?>
<mainstuff>
<category_major>
**<project_name></project_name>
<project_attribute></project_attribute>**
<category_A></category_A>
<category_B></category_B>
<category_C></category_C>
</category_major>
</mainstuff>
感谢您的帮助:D
答案 0 :(得分:2)
如果没有匹配元素的模板,则使用默认模板。默认模板的效果是有效地输出节点的字符串值 - 对于元素,它看起来像是所有后代文本节点的串联。
如果要覆盖此行为,则需要为要跳过的元素提供自己的无操作模板:
<xsl:template match="project_name | project_attribute" />
对于第二个请求,如果要输出元素但删除所有内容,可以使用xsl:copy
:
<xsl:template match="project_name | project_attribute">
<xsl:copy />
</xsl:template>
请注意xsl:copy
仅复制元素;它不会复制其属性,也不会复制其子属。
答案 1 :(得分:0)
尝试将其反转,匹配您要排除的内容,而不是您想要包含的内容:
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*" priority="0">
<xsl:element name="{local-name()}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="project_name" priority="1"></xsl:template>
<xsl:template match="project_attribute" priority="1"></xsl:template>
对不起,如果这有点神秘,但我希望它有所帮助