2016-09-08 7 views
0

私の目標は、XMLファイル(メモリ内のDOMオブジェクト)を調べて、特定の属性を含むすべての要素を削除することです。したがって、私はxpathを返したいと思います。これは、このような要素をすべて削除するためのものです。XPath - 属性の値が存在する場合は、値を要求する

代表的なXMLレイアウト:

<root> 
    <pages> 
     <page required_distribution="customers, internal, vendors"> 
      <id>ID of page</id> 
      <name>Name of page with limited scope</name> 
      <more>more stuff</more> 
     </page> 
     <page> 
      <id>ID of next page</id> 
      <name>Name of next page which has unlimited scope</name> 
      <more>More stuff, other elements, etc.</more> 
     </page> 
    </pages> 
    <buttons> 
     <button> 
      <id>button ID</id> 
      <text>button text</text> 
     </button> 
     <button required_distribution="customers, vendors"> 
      <id>button ID with limited distribution</id> 
      <text>button text</text> 
     </button> 
    </buttons> 
    <innerhtmlblocks> 
     <!-- Represents elements that are inner html and pulled in directly 
      without additional XSLT parsing, except to remove the control attribute --> 
     <innerhtmlblock id="blockid"> 
      This is a content page, wherein there is innerhtml such as 
      <img src="./image.png" /> images and other elements can be 
      included in free form. Theoretically, though, I want to be 
      able to show certain 
      <div required_distribution="internal"> 
       content only to certain versions. 
      </div> 
      <div required_distribution="vendor, customers"> 
       content that varies by version. 
      </div> 
     </innerhtmlblock> 
    </innerhtmlblocks> 
</root> 

XSLTは、XMLからHTMLへの変換を提供します。私はXSLTが起こる前に要素をフィルタリングしたいので、自分の要件を満たしていないノードをすべて選択して削除することで、結果的にXMLを取得し、 "内部"の架空の分布を得ます。この場合

<root> 
    <pages> 
     <page required_distribution="customers, internal, vendors"> 
      <id>ID of page</name> 
      <name>Name of page with limited scope</name> 
      <more>more stuff</more> 
     </page> 
     <page> 
      <id>ID of next page</id> 
      <name>Name of next page which has unlimited scope</name> 
      <more>More stuff, other elements, etc.</more> 
     <page> 
    </pages> 
    <buttons> 
     <button> 
      <id>button ID</id> 
      <text>button text</text> 
     </button> 
    </buttons> 
    <innerhtmlblocks> 
     <!-- Represents elements that are inner html and pulled in directly 
      without additional XSLT parsing, except to remove the control attribute --> 
     <innerhtmlblock id="blockid"> 
      This is a content page, wherein there is innerhtml such as 
      <img src="./image.png" /> images and other elements can be 
      included in free form. Theoretically, though, I want to be 
      able to show certain 
      <div required_distribution="internal"> 
       content only to certain versions. 
      </div> 
    </innerhtmlblocks> 
</root> 

、@required_distributionを持っているすべての要素をチェックする必要があり、かつ$ requiredval(「内部」)場合は、そのノードを削除する必要があり、表示されません。

私は(スタック交換のcourtesty)に来て一番近い考え方は次のとおりです。

//*[@required_distribution and not(contains(@required_distribution,$requiredval))] 

私も

//*[@required_distribution]/[contains(@required_distribution,$requiredval)] 

//*[@required_distribution]/@required_distribution[contains(string(),$requiredval] 

が、noにしてみました利用可能。私はnode()、self ::などのバリエーションも試しましたが、それらは同じように不得手でした(おそらくそれらを役に立たないようにするには不十分です)。

私はそれを行うならば、私は作品を知っているだけであるのXPath、使用して制御属性(複数可)を削除します:私の質問は、私はすべて選択しない方法で、要するに

//*[@required_distribution] 

を指定された属性が存在するが、指定された文字列を含まない要素

答えて

0

は、カンマを含めます。だから、containsは '内部'を見つけられません。

+0

私はコンマを必要とする機能的理由がありません。それは当時の私の頭の中でちょうど意味があり、contains()は関数型関数であると思いました。ありがとう!文字列「内部」のアポストロフィが欠けていたので、それも捨ててしまった。私はそれを修正して動作させる。 – Nick

+1

引用符の中に 'internal'の後にコンマを入れる必要はありません。それどころか、カンマが必要な場合、 'internal'がリストされた最後の(または唯一の)値であるケースを見逃すでしょう。 –

+0

私は同意します - それは '内部'が最後にあった値を失うため意味がありません。しかし、@ Federicoが言及した理由のために、タグにカンマを付けることはそれを妨害していました。私はちょうどコンマを引っ張って問題を解決しました。 – Nick

0

次は動作するはずです:内部

//*[@required_distribution and not(contains(@required_distribution, 'internal,'))] 

属性の値は、スペースではなくコンマで区切られた後

//*[@required_distribution and not(contains(@required_distribution, 'internal'))]

+0

ありがとうございます!私は、文字列「内部」を囲むアポストロフィを見逃していました。 – Nick

関連する問題