XSLT——将文本节点的值解释为 XPath 查询(并在转换中使用它) [英] XSLT -- interpret a text node's value as XPath query (and use it in transformation)

查看:19
本文介绍了XSLT——将文本节点的值解释为 XPath 查询(并在转换中使用它)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图想出一个不错的解决方案,但我什至找不到合适的解决方案.

I've been trying to come up with a decent solution to this, but I couldn't even find an indecent one.

我有一个看起来像这样的 XML 文件:

I have a XML file that looks a bit like this:

<Root>
    <Something>
        <SomethingElse>Value of SomethingElse</SomethingElse>
    </Something>

...

    <Map>
        <Node>
            <Path>/Root/Something/SomethingElse</Path>
        </Node>
    <Map>

...

</Root>

我想要做的是编写一个 XSL 转换,该转换将采用 <Path> 元素并使用其 text() 值作为 XPath 查询来应用转换为 元素,结果类似于:

What I want to do is write an XSL transformation that would take the <Path> element and use its text() value as an XPath query to apply a transformation to the <SomethingElse> element, resulting in something like e.g.:

<text>Value of SomethingElse</text>

我脑海中浮现的第一件事是:

The first thing that popped into my mind was something along the lines of:

<xsl:template match="Path">
    <text><xsl:value-of select="{text()}"/></text>
</xsl:template>

但这当然不起作用,因为 select 不是值属性.

but this of course doesn't work as select is not a value attribute.

我不知道该怎么做.Google 搜索仅返回有关如何使用 XPath 获取文本值的结果,而不是相反.

I have no idea how to go about this. Google search only returned results on how to use XPath to obtain text values, not the other way around.

我还想对返回的 <SomethingElse> 元素应用进一步的转换,但是一旦我弄清楚如何做到这一点,这应该是小菜一碟.

I would also like to apply further transformations to the returned <SomethingElse> element, but that should be a piece of cake once I figure out how to do this.

感谢大家走到这一步,
斯兰皮斯科

Thanks a bunch for getting this far,
Slampisko

推荐答案

对于纯 XSLT 1.0 甚至 2.0,您需要使用两个样式表,第一个获取输入并使用您想要的 XPath 表达式创建第二个样式表作为其结果使用,然后针对原始输入运行第二个样式表.

With pure XSLT 1.0 and even 2.0 you need to use two stylesheets, the first takes the input and creates a second stylesheet as its result with the XPath expression you want to use, then you run the second stylesheet against the original input.

或者您需要检查您的 XSLT 处理器是否支持扩展函数来将字符串计算为路径表达式,就像 Saxon 具有的 http://www.saxonica.com/documentation/extensions/functions/evaluate.xml 或类似 AltovaXML 的 altova:evaluate 函数 http://manual.altova.com/AltovaXML/altovaxmlcommunity/index.html?xextaltova_general.htm.

Or you need to check whether your XSLT processor supports an extension function to evaluate a string as a path expression, like Saxon has with http://www.saxonica.com/documentation/extensions/functions/evaluate.xml or like AltovaXML has with its altova:evaluate function http://manual.altova.com/AltovaXML/altovaxmlcommunity/index.html?xextaltova_general.htm.

这篇关于XSLT——将文本节点的值解释为 XPath 查询(并在转换中使用它)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆