Xslt 在Xpath表达式中伪造变量

Xslt 在Xpath表达式中伪造变量,xslt,xslt-1.0,Xslt,Xslt 1.0,在纯XSLT1.0中,不可能将字符串变量用作XPath表达式 但是,如果可能的表达式都很简单,比如“/book/chapter/verse”或“/year/make/model/style”——只有子轴,只有元素节点,没有谓词——是否可以在键字符串所在的路径上构建键?差不多 <xsl:key name="elementByPath" match="*" use="path()" /> 其中,$var可以是类似“/book/chapter/verse”的字符串 但straight X

在纯XSLT1.0中,不可能将字符串变量用作XPath表达式

但是,如果可能的表达式都很简单,比如“/book/chapter/verse”或“/year/make/model/style”——只有子轴,只有元素节点,没有谓词——是否可以在键字符串所在的路径上构建键?差不多

<xsl:key name="elementByPath" match="*" use="path()" />
其中,$var可以是类似“/book/chapter/verse”的字符串

但straight XSLT 1.0没有path()函数:(

很容易找到一条路径

 <xsl:for-each select="ancestor-or-self::*">
      <xsl:text>/</xsl:text>
      <xsl:value-of select="name()"/>
 </xsl:for-each>

/
但这在“使用钥匙”时是不行的(


当可能的表达式(尽管很多)很简单时,是否有其他方法通过变量XPath表达式选择元素?

可以做一些事情:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pPath1" select="'/books/book/title'"/>
 <xsl:param name="pPath2" select="'/books/book/description'"/>

 <xsl:key name="kElemByPath" match="*"
  use="concat('/', name(ancestor-or-self::*[last()])
             ,'/', name(ancestor-or-self::*[last()-1])
             ,'/', name(ancestor-or-self::*[last()-2])
             )"/>

 <xsl:template match="/">
   <xsl:copy-of select="key('kElemByPath', $pPath1)"/>
==========&#xA;<xsl:text/>
   <xsl:copy-of select="key('kElemByPath', $pPath2)"/>
 </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>


<xsl:param name="pPath1" select="'books/book/title'"/>
<xsl:param name="pPath2" select="'books/book/description'"/>


<xsl:template match="/">
    <xsl:call-template name="elementsByPath">
        <xsl:with-param name="path" select="$pPath1" />
    </xsl:call-template>
    <xsl:text>==========&#xA;</xsl:text>
    <xsl:call-template name="elementsByPath">
        <xsl:with-param name="path" select="$pPath2" />
    </xsl:call-template>        
</xsl:template>


<xsl:template name="elementsByPath"> 
    <xsl:param name="path" />
    <xsl:choose>
        <xsl:when test="contains($path,'/')">
            <xsl:for-each select="*[name()=substring-before($path,'/')]"> 
                <xsl:call-template name="elementsByPath">
                    <xsl:with-param name="path" select="substring-after($path,'/')" />
                </xsl:call-template>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="*[name()=$path]" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="*">
    <xsl:copy-of select="." /><xsl:text>&#xA;</xsl:text>

</xsl:template>

</xsl:stylesheet>
<title>Extending Flash MX 2004</title>
<title>Java Software Solutions</title>
==========
<description>
        Using javascript alongwith actionscript 3.0 and mxml.
    </description>
<description>
        Complete book full of case studies on business solutions and design concepts while building mission critical
        business applications.
    </description>

==========&xA;
当此转换应用于以下XML文档时:

<books>
    <book isbn="1590593049">
        <title>Extending Flash MX 2004</title>
        <description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
    </book>
    <book isbn="0132149184">
        <title>Java Software Solutions</title>
        <description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>
    </book>
</books>
<title>Extending Flash MX 2004</title>
<title>Java Software Solutions</title>
==========
<description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
<description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>

扩展flashmx2004
将javascript与actionscript 3.0和mxml结合使用。
Java软件解决方案
在构建关键任务时,完整的商业解决方案和设计概念案例研究
商业应用。
生成所需的正确结果:

<books>
    <book isbn="1590593049">
        <title>Extending Flash MX 2004</title>
        <description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
    </book>
    <book isbn="0132149184">
        <title>Java Software Solutions</title>
        <description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>
    </book>
</books>
<title>Extending Flash MX 2004</title>
<title>Java Software Solutions</title>
==========
<description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
<description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>
扩展flashmx2004
Java软件解决方案
==========
将javascript与actionscript 3.0和mxml结合使用。
在构建关键任务时,完整的商业解决方案和设计概念案例研究
商业应用。

如果您知道“路径”中位置步骤的最大数量,那么您可以定义一个类似于此示例的键。位置步骤数量较少的表达式必须以必要数量的斜杠结尾。

或者只是递归解析字符串蛮力怎么样

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pPath1" select="'/books/book/title'"/>
 <xsl:param name="pPath2" select="'/books/book/description'"/>

 <xsl:key name="kElemByPath" match="*"
  use="concat('/', name(ancestor-or-self::*[last()])
             ,'/', name(ancestor-or-self::*[last()-1])
             ,'/', name(ancestor-or-self::*[last()-2])
             )"/>

 <xsl:template match="/">
   <xsl:copy-of select="key('kElemByPath', $pPath1)"/>
==========&#xA;<xsl:text/>
   <xsl:copy-of select="key('kElemByPath', $pPath2)"/>
 </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>


<xsl:param name="pPath1" select="'books/book/title'"/>
<xsl:param name="pPath2" select="'books/book/description'"/>


<xsl:template match="/">
    <xsl:call-template name="elementsByPath">
        <xsl:with-param name="path" select="$pPath1" />
    </xsl:call-template>
    <xsl:text>==========&#xA;</xsl:text>
    <xsl:call-template name="elementsByPath">
        <xsl:with-param name="path" select="$pPath2" />
    </xsl:call-template>        
</xsl:template>


<xsl:template name="elementsByPath"> 
    <xsl:param name="path" />
    <xsl:choose>
        <xsl:when test="contains($path,'/')">
            <xsl:for-each select="*[name()=substring-before($path,'/')]"> 
                <xsl:call-template name="elementsByPath">
                    <xsl:with-param name="path" select="substring-after($path,'/')" />
                </xsl:call-template>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="*[name()=$path]" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="*">
    <xsl:copy-of select="." /><xsl:text>&#xA;</xsl:text>

</xsl:template>

</xsl:stylesheet>
<title>Extending Flash MX 2004</title>
<title>Java Software Solutions</title>
==========
<description>
        Using javascript alongwith actionscript 3.0 and mxml.
    </description>
<description>
        Complete book full of case studies on business solutions and design concepts while building mission critical
        business applications.
    </description>

==========&xA;

;
将该转换应用于Dimitre的示例XML时,将获得正确的结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pPath1" select="'/books/book/title'"/>
 <xsl:param name="pPath2" select="'/books/book/description'"/>

 <xsl:key name="kElemByPath" match="*"
  use="concat('/', name(ancestor-or-self::*[last()])
             ,'/', name(ancestor-or-self::*[last()-1])
             ,'/', name(ancestor-or-self::*[last()-2])
             )"/>

 <xsl:template match="/">
   <xsl:copy-of select="key('kElemByPath', $pPath1)"/>
==========&#xA;<xsl:text/>
   <xsl:copy-of select="key('kElemByPath', $pPath2)"/>
 </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>


<xsl:param name="pPath1" select="'books/book/title'"/>
<xsl:param name="pPath2" select="'books/book/description'"/>


<xsl:template match="/">
    <xsl:call-template name="elementsByPath">
        <xsl:with-param name="path" select="$pPath1" />
    </xsl:call-template>
    <xsl:text>==========&#xA;</xsl:text>
    <xsl:call-template name="elementsByPath">
        <xsl:with-param name="path" select="$pPath2" />
    </xsl:call-template>        
</xsl:template>


<xsl:template name="elementsByPath"> 
    <xsl:param name="path" />
    <xsl:choose>
        <xsl:when test="contains($path,'/')">
            <xsl:for-each select="*[name()=substring-before($path,'/')]"> 
                <xsl:call-template name="elementsByPath">
                    <xsl:with-param name="path" select="substring-after($path,'/')" />
                </xsl:call-template>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="*[name()=$path]" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="*">
    <xsl:copy-of select="." /><xsl:text>&#xA;</xsl:text>

</xsl:template>

</xsl:stylesheet>
<title>Extending Flash MX 2004</title>
<title>Java Software Solutions</title>
==========
<description>
        Using javascript alongwith actionscript 3.0 and mxml.
    </description>
<description>
        Complete book full of case studies on business solutions and design concepts while building mission critical
        business applications.
    </description>
扩展flashmx2004
Java软件解决方案
==========
将javascript与actionscript 3.0和mxml结合使用。
在构建关键任务时,完整的商业解决方案和设计概念案例研究
商业应用。

我想这会管用…

当然,这种处理方式并不新鲜——我已经回答了至少两次类似的问题。这里的新鲜之处是使用键的想法/尝试。当然,使用
key()
函数要快得多。