为什么 XSLT 不喜欢我的 XPath 查询? [英] Why doesn't XSLT like my XPath query?

查看:17
本文介绍了为什么 XSLT 不喜欢我的 XPath 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XPath 查询试图获取特定文件节点的父节点.当我在 Xselerator 中使用 XPath 评估器时,我的查询很好,但是当我将它放入我的 XSLT 代码时,它让我很合适.这是我的 XSLT 代码:

I have an XPath query that's trying to grab the parent of a specific file node. When I used the XPath evaluator in Xselerator it was fine with my query, but when I put it into my XSLT code, it gives me fits. Here's my XSLT code:

<xsl:template match="//*[local-name()='Wix']/*[local-name()='Fragment'][1]/*[local-name()='DirectoryRef']/*[local-name()='Directory'][./@*[local-name()='Name'][.='bin']]/*[local-name()='Component']/*[local-name()='File'][./@*[local-name()='Source'][.='!(wix.SourceDeployDir)\bin\Client.exe']]/..">
<xsl:copy>
  <xsl:apply-templates select="@* | node()" />
  <xsl:element name="RemoveFolder" namespace="{namespace-uri()}">
    <xsl:attribute name="Id">DeleteShortcutFolder</xsl:attribute>
    <xsl:attribute name="Directory">DesktopFolder</xsl:attribute>
    <xsl:attribute name="On">uninstall</xsl:attribute>
  </xsl:element>
</xsl:copy>

有什么想法吗?

这是相关的 XML(从较大的文件中清除):

Here's the relevant XML (cleaned up from the larger file):

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="INSTALLLOCATION">
<Directory Id="dirBD8892FBCC64DA5924D7F747259B8B87" Name="bin">
<Component Id="cmp92DC8F5323DA73C053179076052F92FF" Guid="{533500C1-ACB2-4A8D-866C-7CDB1DE75524}">
                    <File Id="fil7C1FC50442FC92D227AD1EDC1E6D259F" KeyPath="yes" Source="!(wix.SourceDeployDir)\bin\Client.exe">
                      <Shortcut Id="startmenuAdv" Directory="DesktopFolder" Advertise="yes" Name="!(wix.ProductName)" WorkingDirectory="INSTALLDIR" Icon="Icon.exe">
                        <Icon Id="Icon.exe" SourceFile="!(wix.SourceDeployDir)\Safeguard.SPI2.Client.exe" />
                      </Shortcut>
                      <netfx:NativeImage Id="ClientNativeImageId" Platform="64bit" Priority="0" AppBaseDirectory="INSTALLLOCATION" xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension" />
                    </File>
                </Component></Directory></DirectoryRef></Fragment></Wix>

我想要做的就是获取 Component 节点.Visual Studio 给了我以下错误:谓词之外的模式中只允许使用子"和属性"轴....in\Client.exe']]/​​-->..<--

All I want to be able to do is grab the Component node. Visual Studio is giving me the following error: Only 'child' and 'attribute' axes are allowed in a pattern outside predicates. ...in\Client.exe']]/ -->..<--

推荐答案

XSLT 匹配模式不允许所有类型的 XPath 表达式,而是模式是 XPath 表达式的子集.您似乎想访问父 ..,但不允许在 XSLT 模式中这样做,除非它在谓词内部.所以你需要重写你的模式,而不是 foo[predicate]/.. 使用 *[foo[predicate]].

Well an XSLT match pattern does not allow all type of XPath expressions, instead patterns are a subset of XPath expressions. You seem to want to access the parent .. but you are not allowed to do that in an XSLT pattern, unless it is inside of a predicate. So you will need to rewrite your pattern, instead of foo[predicate]/.. use *[foo[predicate]].

根据你最新的评论做

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template xmlns:wi="http://schemas.microsoft.com/wix/2006/wi"
  match="wi:Component[wi:File[@Source[. = '!(wix.SourceDeployDir)\bin\Client.exe']]]">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    <xsl:element name="RemoveFolder" namespace="{namespace-uri()}">
      <xsl:attribute name="Id">DeleteShortcutFolder</xsl:attribute>
      <xsl:attribute name="Directory">DesktopFolder</xsl:attribute>
      <xsl:attribute name="On">uninstall</xsl:attribute>
    </xsl:element>
  </xsl:copy>
</xsl:template>

可能就足够了(假设您想复制除添加元素的组件之外的所有内容.

might suffice (assuming you want to copy everything but the Component where you add an element.

这篇关于为什么 XSLT 不喜欢我的 XPath 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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