[英]Using XSLT, how do I match an attribute with regex using a variable in a .NET project?
I am working on a .NET project which converts an xml file into a different format using XSLT. 我正在一个.NET项目中,该项目使用XSLT将xml文件转换为其他格式。
I have original xml like this (this is just the relevant section): 我有这样的原始xml(这只是相关部分):
<video>
<part ids="pt1">
<title>Title1</title>
</part>
<part ids="pt2 pt3">
<title>Title2</title>
</part>
<part ids="pt4">
<title>Title3</title>
</part>
</video>
In my xslt file I have a variable $partid
which is set fine as something like pt2
. 在我的xslt文件中,我有一个变量$partid
,它可以像pt2
这样设置。 I then wanted to use this to find the <title>
within the <part>
which contained the variable $partid
in the attribute ids
. 然后,我想用它在<part>
内找到<title>
,该<title>
在属性ids
包含变量$partid
。
I was doing this originally with the following: 我最初是通过以下方式进行此操作的:
<xsl:value-of select="/video/part[contains(@ids, $partid)]/title"/>
However, I have now realised this runs into problems if the $partid
is something like pt1
but there exists a <part>
element with ids
as pt12
as well as a <part>
element with ids
as pt1
. 不过,我现在已经意识到了这一点,如果过程中遇到问题$partid
是一样的东西pt1
,但存在一个<part>
与元素ids
为pt12
以及一个<part>
与元素ids
为pt1
。 As this will match both. 因为这将两者都匹配。
I thought I could just swap it to use matches()
instead of contains()
and use a regex like this: 我以为我可以将其交换为使用matches()
而不是contains()
并使用如下正则表达式:
matches(@ids, '(^'+$partId+' | '+$partId+' | '+$partId+'$ |^'+$partId+'$)')
However, it seems I'm restricted to XSLT 1.0 which does not allow matches()
. 但是,似乎我仅限于XSLT 1.0,它不允许使用matches()
。 Any ideas on how I could proceed with this? 关于如何进行此操作的任何想法?
Many thanks. 非常感谢。
There is no regex support in XSLT 1.0. XSLT 1.0中没有正则表达式支持。 To eliminate the ambiguity between pt1
and pt 12
, you can do: 要消除pt1
和pt 12
之间的歧义,可以执行以下操作:
<xsl:value-of select="/video/part[contains(concat(' ', @ids, ' '), concat(' ', $partid, ' '))]/title"/>
A better solution would be upstream of you: there's really little point in using XML that way. 更好的解决方案将摆在您的上游:以这种方式使用XML确实没有什么意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.