3

次のようなコードのブロックがあり、ツリーの下のノードの名前を取得します。

section/page/subPage

しかし、私はそれを次のように理解できるようにしたいと思います(それを構成するだけです):

section[@id='someId']/page/subPage[@user='UserA']/@title

これらのStackOverflow投稿の1つから次のコードを見つけました:


<xsl:attribute name="path">
  <xsl:for-each select="ancestor-or-self::*">
    <xsl:if test="name() != 'root'">
      <xsl:value-of select="name()">
        <xsl:if test="not(position()=last())">
          <xsl:text>/</xsl:text>
        </xsl:if>
      </xsl:value-of>
    </xsl:if>
  </xsl:for-each>
</xsl:attribute>

これは私にまっすぐな道を与えますが、@ id(または関連する属性)を含むように、より多くのロジックを実行して、現時点では考えられないものを追加したいと思います。

これを行うための最良の方法は何ですか?

EXSLT関数をチェックインしましたが、これは機能する可能性がありますが、皆さんはすでにこの問題をより良い方法で解決しているかもしれません。

何か案は?

私はrubyのnokogiriを使用して、xml/xsltを解析しています。

どうもありがとう、ランス

4

2 に答える 2

4

このソリューションはあなたが望むことをします:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" />

  <xsl:template match="/">
    <root>
      <xsl:attribute name="path">
        <xsl:apply-templates select="(//@title)[1]" mode="make-path" />
      </xsl:attribute>
    </root>
  </xsl:template>

  <xsl:template match="*|@*" mode="make-path">
    <xsl:apply-templates select="parent::*" mode="make-path" />
    <xsl:text>/</xsl:text>
    <xsl:apply-templates select="." mode="make-name" />
    <xsl:choose>
      <xsl:when test="self::section">
        <xsl:apply-templates select="@id" mode="make-predicate" />
      </xsl:when>
      <xsl:when test="self::subPage">
        <xsl:apply-templates select="@user" mode="make-predicate" />
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*|@*" mode="make-predicate">
    <xsl:text>[</xsl:text>
    <xsl:apply-templates select="." mode="make-name" />
    <xsl:text> = '</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>']</xsl:text>
  </xsl:template>

  <xsl:template match="*" mode="make-name">
    <xsl:value-of select="name()" />
  </xsl:template>

  <xsl:template match="@*" mode="make-name">
    <xsl:text>@</xsl:text>
    <xsl:value-of select="name()" />
  </xsl:template>

</xsl:stylesheet>

適用した場合

<section id="someId">
  <page>
    <subPage user="UserA" title="test" />
    <subPage user="UserB" title="blah" />
  </page>
  <page>
    <subPage user="UserC" title="fooh" />
  </page>
</section>

あなたが得る:

<root path="/section[@id = 'someId']/page/subPage[@user = 'UserA']/@title" />

これ<xsl:choose>は構成可能なスポットです(必要な数だけ追加し<xsl:when>てください)。

<!-- test for element name -->
<xsl:when test="self::section">
  <!-- make predicates out of selected attributes -->
  <xsl:apply-templates select="@id" mode="make-predicate" />
</xsl:when>

また可能:

<xsl:when test="self::section">
  <xsl:apply-templates select="@name|@category|subElement" mode="make-predicate" />
</xsl:when>

これは

<root path="/section[@name = 'someName'][@category = 'somecat'][subElement = 'xyz']/..." />

私が目にする唯一の問題は、一重引用符を含む述語値に関するものです。それらはXPathを壊します。

于 2009-09-30T12:59:16.513 に答える
1

祖先ノードごとに、単純なxsl:for-eachを使用してすべての属性をループできます。

<xsl:for-each select="@*">

次に、属性を使用してXPath文字列を作成できます

<xsl:for-each select="ancestor-or-self::*">
   <xsl:if test="name() != 'root'">
      <xsl:value-of select="name()"/>
      <xsl:if test="@*">
         <xsl:text>[</xsl:text>
         <xsl:for-each select="@*">
            <xsl:text>@</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:text>='</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>'</xsl:text>
            <xsl:if test="not(position()=last())">
               <xsl:text> and </xsl:text>
            </xsl:if>
         </xsl:for-each>
         <xsl:text>]</xsl:text>
      </xsl:if>
      <xsl:if test="not(position()=last())">
         <xsl:text>/</xsl:text>
      </xsl:if>
   </xsl:if>
</xsl:for-each>
于 2009-09-30T12:46:30.567 に答える