sedを使用してXMLファイルから複数の文字列を検索する

sedを使用してXMLファイルから複数の文字列を検索する

大容量ファイルXMLをフィルタリングし、複数の基準を使用して文字列を見つける必要があります。 cnisfCFがtrueでnatg_passwordAlreadyResetedPostMigrationがtrueの場合は、電子メールをフィルタリングする必要があります。

誰でも助けることができますか?

<customer customer-no="09090909090">
        <credentials>
            <login>[email protected]</login>
            <enabled-flag>true</enabled-flag>
            <password-question/>
            <password-answer/>
        </credentials>
        <profile>
            <salutation/>
            <title/>
            <first-name>teste</first-name>
            <second-name/>
            <last-name>name 1</last-name>
            <suffix/>
            <company-name/>
            <job-title/>
            <email>[email protected]</email>
            <phone-home>542926407485</phone-home>
            <phone-business/>
            <phone-mobile/>
            <fax/>
            <birthday>1999-09-12Z</birthday>
            <gender>2</gender>
            <creation-date>2022-09-19T18:34:45.000Z</creation-date>
            <preferred-locale/>
            <custom-attributes>
                <custom-attribute attribute-id="natg_Newsletter">false</custom-attribute>
                <custom-attribute attribute-id="natg_cfIsCn">false</custom-attribute>
                <custom-attribute attribute-id="natg_cpf">5465465456456</custom-attribute>
                <custom-attribute attribute-id="natg_infContOptIn">false</custom-attribute>
                <custom-attribute attribute-id="natg_optInWP">false</custom-attribute>
                <custom-attribute attribute-id="natg_passwordAlreadyResetedPostMigration">true</custom-attribute>
                <custom-attribute attribute-id="natg_personNumber">116864397</custom-attribute>
                <custom-attribute attribute-id="natg_pushOptIn">false</custom-attribute>
                <custom-attribute attribute-id="natg_rut">456456456</custom-attribute>
            </custom-attributes>
        </profile>

ベストアンサー1

次のコマンドをテストする前に、欠落している</customer>クローズタグをデータに自由に追加しました。cnisfCFこれがあなたの場合であると仮定しますnatg_cfIsCn(属性とノード名は大文字と小文字を区別します)。


使用xmlastarlet:

xmlstarlet select --template \
    --match '//profile' \
    --match 'self::node()[custom-attributes/custom-attribute[@attribute-id="natg_cfIsCn"]="true"]' \
    --match 'self::node()[custom-attributes/custom-attribute[@attribute-id="natg_passwordAlreadyResetedPostMigration"]="true"]' \
    --value-of 'email' -nl file.xml

上記のコマンドは、email属性合計を含む入力文書のすべてのノードと値合計の子ノードからノード値を抽出します。profilecustom-attributes/custom-attributeattribute-idnatg_cfIsCnnatg_passwordAlreadyResetedPostMigrationfalsetrue

ここで難しいのは、パスに含まれるノード名が長すぎるため、読みやすい方法でコマンドを表示することです。まず//profile、パスを一致させてから、2つの別々の手順を実行して結果セットの範囲を狭めることで、この問題を解決しました。

単一の「値」XPathクエリのみを使用するselectステートメントは次のとおりです。

xmlstarlet select --template \
    --value-of '//profile[
        custom-attributes/custom-attribute[@attribute-id="natg_cfIsCn"]="true" and 
        custom-attributes/custom-attribute[@attribute-id="natg_passwordAlreadyResetedPostMigration"]="true"
    ]/email' -nl file.xml

これがもっときれいに見える場合は、代わりに使用してください。私は彼らが等しくなければならないと信じています。

照会に一致するデータがないため、上記のコマンドは指定された文書の出力を生成しません。

おすすめ記事