使用 xslt 2.0 执行转换如何获取用定界符分隔的引用值?
Performing transformation with xslt 2.0 how do I get quoted values separated with a delimiter?
我有一个xml要转换:
<root>
<item name="first" />
<item name="second" />
<item name="third" />
</root>
我想要得到的是一个字符串,其中每个值都被引用并且值被分隔(顺序无关紧要):
"first" , "second" , "third"
我的问题是如何正确使用 xslt 2.0 实现此目的?
这是我的解决方案,但没有按预期工作。
所以我的共同问题是 - 为什么不是?
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns="urn:my.name.space"
version="2.0">
<xsl:output method="text" media-type="text/json"/>
<xsl:template match="root">
<xsl:value-of select="item/@name" separator=" , "/>
<xsl:text> :: </xsl:text>
<xsl:value-of select="item/ns:quotedString(@name)" separator=" , "/>
</xsl:template>
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:variable name="quote">"</xsl:variable>
<xsl:value-of select="concat($quote, $input, $quote)"/>
</xsl:function>
</xsl:stylesheet>
这给了我:
first , second , third :: "second""third""first"
请注意,如果我调用引用函数,分隔符会丢失。
我使用 Saxon-B 9.1.0.8 应用转换。
您的函数 return 是一个文本节点并且 adjacent text nodes in the sequence are merged into a single text node。让它 return 一个序列。
所以在您的函数中将 xsl:value
更改为 xsl:sequence
...
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:variable name="quote">"</xsl:variable>
<xsl:sequence select="concat($quote, $input, $quote)"/>
</xsl:function>
我的偏好是只将序列发送到函数...
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns="urn:my.name.space"
version="2.0">
<xsl:output method="text" media-type="text/json"/>
<xsl:template match="root">
<xsl:value-of select="item/@name" separator=" , "/>
<xsl:text> :: </xsl:text>
<xsl:value-of select="ns:quotedString(item/@name)"/>
</xsl:template>
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:value-of select="concat('"',
string-join($input,'", "'),
'"')"/>
</xsl:function>
</xsl:stylesheet>
免责声明:我没有 Saxon-B 9.1.0.8。测试。我使用 Saxon-HE 9.5.1.7。测试。
连接一堆字符串有多种解决方案。
...的 XPath 在...
<xsl:template match="root">
<xsl:value-of select="string-join(for $i in item return concat('"', $i/@name, '"'), ' , ')"/>
</xsl:template>
<string-join>
连接每个 item/@name
的引号字符串。 <string-join>
的参数是字符串。
函数调用"ns:quotedString"
<xsl:template match="root">
<xsl:value-of select="item/ns:quotedString(@name)" separator=" , "/>
</xsl:template>
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:variable name="quote">"</xsl:variable>
<xsl:sequence select="concat($quote, $input, $quote)"/>
</xsl:function>
这将回答您关于为什么它不起作用 的问题。请参阅 <xsl:sequence>
作为函数中的最后一条语句。 @separator
仅在 <xsl:value select="...">
语句 return 是一个序列时有效,在您的情况下 return 值是单个文本节点。
我有一个xml要转换:
<root>
<item name="first" />
<item name="second" />
<item name="third" />
</root>
我想要得到的是一个字符串,其中每个值都被引用并且值被分隔(顺序无关紧要):
"first" , "second" , "third"
我的问题是如何正确使用 xslt 2.0 实现此目的?
这是我的解决方案,但没有按预期工作。 所以我的共同问题是 - 为什么不是?
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns="urn:my.name.space"
version="2.0">
<xsl:output method="text" media-type="text/json"/>
<xsl:template match="root">
<xsl:value-of select="item/@name" separator=" , "/>
<xsl:text> :: </xsl:text>
<xsl:value-of select="item/ns:quotedString(@name)" separator=" , "/>
</xsl:template>
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:variable name="quote">"</xsl:variable>
<xsl:value-of select="concat($quote, $input, $quote)"/>
</xsl:function>
</xsl:stylesheet>
这给了我:
first , second , third :: "second""third""first"
请注意,如果我调用引用函数,分隔符会丢失。
我使用 Saxon-B 9.1.0.8 应用转换。
您的函数 return 是一个文本节点并且 adjacent text nodes in the sequence are merged into a single text node。让它 return 一个序列。
所以在您的函数中将 xsl:value
更改为 xsl:sequence
...
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:variable name="quote">"</xsl:variable>
<xsl:sequence select="concat($quote, $input, $quote)"/>
</xsl:function>
我的偏好是只将序列发送到函数...
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns="urn:my.name.space"
version="2.0">
<xsl:output method="text" media-type="text/json"/>
<xsl:template match="root">
<xsl:value-of select="item/@name" separator=" , "/>
<xsl:text> :: </xsl:text>
<xsl:value-of select="ns:quotedString(item/@name)"/>
</xsl:template>
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:value-of select="concat('"',
string-join($input,'", "'),
'"')"/>
</xsl:function>
</xsl:stylesheet>
免责声明:我没有 Saxon-B 9.1.0.8。测试。我使用 Saxon-HE 9.5.1.7。测试。
连接一堆字符串有多种解决方案。
...的 XPath 在...
<xsl:template match="root">
<xsl:value-of select="string-join(for $i in item return concat('"', $i/@name, '"'), ' , ')"/>
</xsl:template>
<string-join>
连接每个 item/@name
的引号字符串。 <string-join>
的参数是字符串。
函数调用"ns:quotedString"
<xsl:template match="root">
<xsl:value-of select="item/ns:quotedString(@name)" separator=" , "/>
</xsl:template>
<xsl:function name="ns:quotedString">
<xsl:param name="input"/>
<xsl:variable name="quote">"</xsl:variable>
<xsl:sequence select="concat($quote, $input, $quote)"/>
</xsl:function>
这将回答您关于为什么它不起作用 的问题。请参阅 <xsl:sequence>
作为函数中的最后一条语句。 @separator
仅在 <xsl:value select="...">
语句 return 是一个序列时有效,在您的情况下 return 值是单个文本节点。