Xml XSL如何选择以特定字符集开头的每个选项卡?
我正在尝试创建一个xsl文件,该文件将通过xml文件,并且只处理以特定字符集开头的标记。我应该提到,我对xsl文件相当陌生,所以这可能很容易实现,但我不知道如何实现 我的xml文件与此类似:Xml XSL如何选择以特定字符集开头的每个选项卡?,xml,xslt,Xml,Xslt,我正在尝试创建一个xsl文件,该文件将通过xml文件,并且只处理以特定字符集开头的标记。我应该提到,我对xsl文件相当陌生,所以这可能很容易实现,但我不知道如何实现 我的xml文件与此类似: <generic_etd> <associated_tags> <master> <dc.contributor>contributor</dc.contributor> </master>
<generic_etd>
<associated_tags>
<master>
<dc.contributor>contributor</dc.contributor>
</master>
<related>
<dc.contributor.role>contributor role</dc.contributor.role>
</related>
</associated_tags>
<associated_tags>
<master>
<dc.contributor>sponsor</dc.contributor>
</master>
<related>
<dc.contributor.role>sponsor role</dc.contributor.role>
</related>
</associated_tags>
<dc.creator>gradstudent2</dc.creator>
<dc.date>2014-02-11</dc.date>
<dc.description>description</dc.description>
<thesis.degree.discipline>Business Administration</thesis.degree.discipline>
<generic_etd>
我希望我的xsl文件只处理以“dc”开头的标记
这是我到目前为止提出的xsl文件:
<xsl:template match="*">
<xsl:choose>
<!-- Process the tags we are interested in -->
<xsl:when test="contains(name(),'dc.')">
<xsl:variable name="newtag" select="concat('dc:',substring-after(name(),'.'))"/>
<xsl:choose>
<xsl:when test="contains($newtag, '.')">
<xsl:element name="{substring-before($newtag,'.')}">
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$newtag}">
<xsl:apply-templates/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:template>
但是,它生成的文件在输出中缺少dc.contributor和dc.contributor.role。实际上,我想在本例中排除dc.contributor.role,但我需要在正在处理的另一个文件中使用它
我的问题是我哪里做错了
谢谢。这里是一个示例样式表。它直接匹配以dc开头的元素
嗯。。。。是否尝试将dc.*元素移动到命名空间中?如果是这样的话,您也需要在替换的xsl:element上声明这一点。如果不是,则不能在元素名称中使用冒号;保留用作名称空间前缀。是的,我正在给它命名。我只展示了代码中我遇到问题的部分,这是一个比我提出的更干净的实现。谢谢您的示例。您可能需要检查名称是否以dc开头。而不是它是否包含它,只是为了让它更健壮一点。。。“但除此之外,这确实应该有效。”凯什兰,如果你读第二句话,我将以开头。也许我在复制OP代码时忘了更改它:这是我抱怨的一句话,这是直接从我上面看到的复制品。你可能想修正它。是的,我并不是为了让你先明白我的观点才修正它的。现在我要改变它。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://www.example.com">
<xsl:output method='xml' indent='yes'/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:namespace name="dc">http://www.example.com</xsl:namespace>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(),'dc.')]">
<xsl:variable name="newtag" select="concat('dc:',substring-after(name(),'.'))"/>
<xsl:element name="{$newtag}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>