Home > database >  Get value from element where the element above has a specific value from xml
Get value from element where the element above has a specific value from xml

Time:12-23

I need to extract the Values\String where ExtraMetaData\Property is C_PQIDSERVERJOBOPTIONS I've tried this with xmllint but did not succeed to get the correct value.

I want the value 'THIS STRING IS WANTED' where <ExtraMetaData><Property> = C_PQIDSERVERJOBOPTIONS

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="urn:SmartConnection" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <ns1:GetObjectsResponse>
      <Objects>
        <Object>
          <MetaData>
            <SourceMetaData>
              <Credit/>
              <Source/>
              <Author>AUTHOR</Author>
            </SourceMetaData>
            <ExtraMetaData>
              <ExtraMetaData>
                <Property>C_REPROJOB</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_PLACEMENT_FRAME_ID</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_ORIGINAL_IMG_ID</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_AUTOMATIONNOTE</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CLARO_PROFILE</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CDH_EXPORT_STATE</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_TOPIC</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_USAGE_RIGHTS</Property>
                <Values>
                  <String>Default</String>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_PQIDSERVERJOBOPTIONS</Property>
                <Values>
                  <String>THIS STRING IS WANTED</String>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CLARO_NORMALIZED</Property>
                <Values>
                  <String>0</String>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_PQIDSERVERSTATE</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_PQIDSERVERLINKS</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_PQIDSERVERFONTS</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CS_FILEFORMATVERSION</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CDH_NOTIFY_MESSAGE</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_MASTER_PAGES</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_FIXCONTENT</Property>
                <Values>
                  <String>0</String>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_ELVIS_ARCHIVESTATUSMODIFIED</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_ELVIS_ARCHIVESTATUS</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CS_SYNC_STATE</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CS_CONVERSION_RULE_ID</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CS_ARTICLE_TEMPLATE_ID</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CS_DE_COMPONENT_NAMES</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CS_COMPONENTSET</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
              <ExtraMetaData>
                <Property>C_CS_STYLEID</Property>
                <Values>
                  <String/>
                </Values>
              </ExtraMetaData>
            </ExtraMetaData>
          </MetaData>
        </Object>
      </Objects>
    </ns1:GetObjectsResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I tried with xmllint --xpath "//*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='GetObjectsResponse']/Objects/Object/MetaData/ExtraMetaData/ExtraMetaData/Values/String/text()" "MyXML.xml" but this gives me all the String values --> DefaultTHIS STRING IS WANTED00

CodePudding user response:

Check yq tool, it's same as jq but support xml,yaml,toml,etc

We can map elements (similar el in xmlstarlet)

yq -p xml --xml-raw-token xml_stack.xml

Default output in yaml, you can switch format output by flag yq -o {xml,json,yaml}


Next solution for:

I want the value 'THIS STRING IS WANTED' where = C_PQIDSERVERJOBOPTIONS

It return elements with Property = C_PQIDSERVERJOBOPTIONS

yq -p xml -o json --xml-raw-token '.Envelope.Body.GetObjectsResponse.Objects.Object[].ExtraMetaData.[][] | select(.Property == "C_PQIDSERVERJOBOPTIONS")' xml_stack.xml

Result:

{
  "Property": "C_PQIDSERVERJOBOPTIONS",
  "Values": {
    "String": "THIS STRING IS WANTED"
  }
}

CodePudding user response:

Try changing your xpath expression to

//ExtraMetaData[Property[text()="C_PQIDSERVERJOBOPTIONS"]]/Values/String/text()

and see if it works.

  • Related