代码之家  ›  专栏  ›  技术社区  ›  developer

使用XML数据动态生成XPath表达式

  •  4
  • developer  · 技术社区  · 15 年前

    我想在由XML文件中的数据确定的XSL中动态创建XPath表达式(i、 e.将XML数据“串联”以创建XPath表达式)。

    XML数据示例:

    <criteria>
        <criterion>AAA</criterion>
        <criterion>BBB</criterion>
        <criterion>CCC</criterion>
    </criteria>
    

    我希望XPath表达式的外观示例:

    //AAA | //BBB | //CCC
    

    这种动态生成需要在XSL文件中完成。

    谢谢!

    编辑: 提供更多的背景。。。。 我需要做的是生成一个XPath,用于创建 第二

    3 回复  |  直到 15 年前
        1
  •  3
  •   Mads Hansen    15 年前

    在对示例XML文件运行时,以下内容将创建示例XPATH字符串作为“generatedXPATH”变量的值:

        <xsl:variable name="generatedXPATH">
            <xsl:for-each select="/criteria/criterion">
                <xsl:text>//</xsl:text>
                <xsl:value-of select="." />
                <xsl:if test="position()!=last()">
                    <xsl:text> | </xsl:text>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
    

    可以在生成样式表的样式表中使用该变量来构造模板 @match 价值观如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xslt="http://www.w3.org/1999/XSL/TransformAlias"
        version="1.0">
        <xsl:namespace-alias stylesheet-prefix="xslt" result-prefix="xsl"/>
        <xsl:output indent="yes" />
        <xsl:template match="/">
            <xsl:variable name="generatedXPATH">
                <xsl:for-each select="/criteria/criterion">
                    <xsl:text>//</xsl:text>
                    <xsl:value-of select="." />
                    <xsl:if test="position()!=last()">
                        <xsl:text> | </xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </xsl:variable>
    
            <xslt:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                <xslt:template>
                    <xsl:attribute name="match">
                        <xsl:value-of select="$generatedXPATH" />
                    </xsl:attribute>
                    <xsl:comment>Do something special, like select the value of the matched elements</xsl:comment>
                    <xslt:value-of select="." />
                </xslt:template>
            </xslt:stylesheet>
        </xsl:template>
    
    </xsl:stylesheet>
    

    <?xml version="1.0" encoding="UTF-16"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="//AAA | //BBB | //CCC">
    <!--Do something special, like select the value of the matched elements-->
    <xsl:value-of select="." />
    </xsl:template>
    </xsl:stylesheet>
    
        2
  •  2
  •   Dimitre Novatchev    15 年前

    可以在XSLT中动态创建XPath表达式,但是不可能动态计算这些表达式(至少在 XSLT 2.1 ).

    举个例子:

    这种转变:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my"
     >
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <my:criteria>
      <criterion>a</criterion>
      <criterion>b</criterion>
      <criterion>c</criterion>
     </my:criteria>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*[name()=document('')/*/my:criteria/*]">
       <xsl:element name="{name()}-{name()}" namespace="{namespace-uri()}">
         <xsl:apply-templates select="node()|@*"/>
       </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于此XML文档时

    <t>
      <a>
        <x/>
        <y/>
        <b>
          <c/>
          <d/>
        </b>
      </a>
    </t>
    

    生产 :

    <t>
        <a-a>
            <x></x>
            <y></y>
            <b-b>
                <c-c></c-c>
                <d></d>
            </b-b>
        </a-a>
    </t>
    

    也就是说,我们只以特殊的方式处理名称为 a b c

        3
  •  0
  •   Jouke van der Maas    15 年前

    您不能用xsl或xpath动态地完成这项工作。您必须使用php或其他任何东西(取决于您在哪里使用它)。如果您有一组有限的节点,那么可以使用//AAA |//BBB |//CCC,它也可以工作,不过如果您想让它在许多不同的节点上工作,这并不是很有用。

    看到了吗 here 有关XPath的详细信息。

    编辑:

    为此使用/criteria/[criteria=AAA]。

    再次编辑:

    查询应为:“ /criteria/[criterion=AAA] | criteria/[criterion=BBB] | criteria/[criterion=CCC] ". 你也许可以把这句话改写得更有效率,尽管我忘了怎么说了。看到了吗 this tutorial