2009-08-20 60 views
0

喂, 首先,我不是很熟悉,当涉及到XML和similiar,所以请不要惩罚我与我的初学者的问题: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

请澄清 - 这导致你要吗?你询问了没有参数和它们的值的结果,最后你询问了没有它们的值的参数? – 2009-08-20 20:38:15

+0

我想要每个结果。一个接一个:D – Camal 2009-08-21 07:21:25

回答

2

如果没有模板相匹配的元素,一个默认的模板被使用。默认模板的效果是有效地输出节点的字符串值 - 对于一个元素,它看起来像是所有后代文本节点的串联。

如果要覆盖此行为,您需要为您希望跳过的元素提供自己无操作模板:

<xsl:template match="project_name | project_attribute" /> 

对于你的第二个要求,如果你想输出的元件,但去除所有内容,可以使用xsl:copy

<xsl:template match="project_name | project_attribute"> 
    <xsl:copy /> 
</xsl:template> 

注意xsl:copy只复制的元素;它不会复制它的属性,也不会复制它的子项。

+0

谢谢伟大的作品:D – Camal 2009-08-21 07:48:09

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> 

很抱歉,如果这是一个有点神秘,但我希望它能帮助

+0

身份转换(这是你似乎试图用于'match =“*”')的最好使用'xsl:copy'编写。而且,不需要明确的优先级 - 模板匹配的优先级总是低于与命名元素匹配的模板的优先级。 – 2009-08-20 20:45:47

+0

优先级是可读性的优先级,我总是在要覆盖的模板上包含priority =“0”,个人风格 和no,xsl:copy不会将模板应用于元素子元素,解决方案工作 – LorenVS 2009-08-20 20:47:54

+0

您可以在''内使用''(或任何其他构造函数)。 – 2009-08-21 17:08:14