我有以下xml文件:
<root_tag>
<tag1>
<tag1_1>A</tag1_1>
<tag1_2>B</tag1_2>
<tag1_3>C</tag1_3>
</tag1>
<tag2>
<tag2_1>D</tag2_1>
<tag2_2>
<elem xmlns="http://a.b.com/sample">
<elem_1>E</elem_1>
<elem_2>F</elem_2>
</elem>
</tag2_2>
</tag2></root_tag>
和xslt模板:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<NODE>
<node1>
<xsl:value-of select="/root_tag/tag1/tag1_2"/>
</node1>
<node2>
<xsl:value-of select="/root_tag/tag2/tag2_1"/>
</node2>
<node3>
<xsl:value-of select="/root_tag/tag2/tag2_2/child::*[local-name(.) = 'elem']/child::*[local-name(.) = 'elem_1']"/>
</node3>
<xsl:variable name="A" select="namespace-uri(/root_tag/tag2/tag2_2/child::*[local-name(.) = 'elem'])"/>
<node4>
<xsl:value-of select="$A"/>
</node4>
<node5>
<!-- some code here -->
</node5>
</NODE>
</xsl:template>
输出结果为:
<NODE>
<node1>B</node1>
<node2>D</node2>
<node3>E</node3>
<node4>http://a.b.com/sample</node4>
<node5></node5>
</NODE>
有没有办法在node5中输入 / root_tag / tag2 / tag2_2 / elem / elem_1 的值,而不使用类似node3中的xpath?存储elem命名空间的变量A可以以某种方式用于此吗?
我还设法通过在 xsl:stylesheet 标记中添加 xmlns:a_1 =“http:// abcom / sample”并使用xpath来提取信息 / root_tag / tag2 / tag2_2 / a_1:elem / a_1:elem_1 ,但这仅涵盖我事先知道命名空间的情况。
答案 0 :(得分:1)
不幸的是 - 没有。更短的XPath:
/root_tag/tag2/tag2_2/*[local-name(.) = 'elem']/*[local-name(.) = 'elem_1']
答案 1 :(得分:1)
以下是使用xxx:node-set()
扩展名在XSLT 1.0中执行此操作的一种方法:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vrtfDoc2">
<xsl:apply-templates/>
</xsl:variable>
<xsl:template match="/">
<node5>
<xsl:value-of select=
"ext:node-set($vrtfDoc2)/root_tag/tag2/tag2_2/elem/elem_1"/>
</node5>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
在提供的XML文档上应用此转换时:
<root_tag>
<tag1>
<tag1_1>A</tag1_1>
<tag1_2>B</tag1_2>
<tag1_3>C</tag1_3>
</tag1>
<tag2>
<tag2_1>D</tag2_1>
<tag2_2>
<elem xmlns="http://a.b.com/sample">
<elem_1>E</elem_1>
<elem_2>F</elem_2>
</elem>
</tag2_2>
</tag2>
</root_tag>
产生了想要的正确结果(node5
):
<node5>E</node5>
<强>解释强>:
这是两遍转换。
在pass1中,文档被转换为包含原始文档元素的RTF(结果树片段),但所有元素都在“无命名空间”中。
EXSLT ext:node-set()
扩展函数用于将上面1.中获得的RTF转换为常规树。
提供的XPath表达式不包含对local-name()
函数的任何谓词和引用,将根据上面3中生成的文档进行评估。