首页 > 解决方案 > 使用基于关闭条件的 Powershell 更改 XML 中的标签

问题描述

示例 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<Searchable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" label="$RESX('Bank.1_8','CRDM_AgeInformation','Age Information')" xmlns="http://sysrepublic.com/Secure/4.0/DSL/SearchService">
  <Parameters />
  <Fields>
    <Field data-type="System.Int64" is-editable="true" id="ageinformation_transactionid" label="$RESX('Bank.1_8','TransactionID','Transaction ID')" is-mandatory-display-field="false" is-hidden-display-field="false" is-virtual-date-time="false" is-display-field-only="false" use-utc-datetime="true" apply-user-timezone-offset="false" show-date="true" show-time="true" is-favourite="false" common-field="true">
      <FieldDescription primary-key="false" nullable="false" readonly="false" hidden="false" format="text" enable-required-validation="true" />
      <Operators>
        <Operator name="equal" />
        <Operator name="notequal" />
        <Operator name="greaterthan" />
        <Operator name="greaterthanequal" />
        <Operator name="lessthan" />
        <Operator name="lessthanequal" />
        <Operator name="between" />
        <Operator name="notbetween" />
        <Operator name="in" />
        <Operator name="notin" />
      </Operators>
      <LeftExpression>
        <Field data-type="System.Int64" common-field="true">
          <CollectionDescription collection="pos.CRDM_AgeInformation">
            <Relationship parent="pos.CRDM_Header">
              <RelatedField from="TransactionID" to="TransactionID" common-to-field="true" />
              <RelatedField from="TradingDay" to="TradingDay" common-to-field="true" />
            </Relationship>
          </CollectionDescription>
          <Description>TransactionID</Description>
        </Field>
      </LeftExpression>
    </Field>
    <Field data-type="System.Int64" is-editable="true" id="ageinformation_checkpointid" label="$RESX('Bank.1_8','CheckPointID','CheckPoint ID')" is-mandatory-display-field="false" is-hidden-display-field="false" is-virtual-date-time="false" is-display-field-only="false" use-utc-datetime="true" apply-user-timezone-offset="false" show-date="true" show-time="true" is-favourite="false">
      <FieldDescription primary-key="false" nullable="false" readonly="false" hidden="false" format="text" enable-required-validation="true" />
      <Operators>
        <Operator name="equal" />
        <Operator name="notequal" />
        <Operator name="greaterthan" />
        <Operator name="greaterthanequal" />
        <Operator name="lessthan" />
        <Operator name="lessthanequal" />
        <Operator name="between" />
        <Operator name="notbetween" />
        <Operator name="in" />
        <Operator name="notin" />
      </Operators>
      <LeftExpression>
        <Field data-type="System.Int64">
          <CollectionDescription collection="pos.CRDM_AgeInformation">
            <Relationship parent="pos.CRDM_Header">
              <RelatedField from="TransactionID" to="TransactionID" common-to-field="true" />
              <RelatedField from="TradingDay" to="TradingDay" common-to-field="true" />
            </Relationship>
          </CollectionDescription>
          <Description>CheckPointID</Description>
        </Field>
      </LeftExpression>
    </Field>

</Fields>
</Searchable>

要解决的问题:

  1. 检查用于不同字段的 XML 标签是否存在于数据库表中。
  2. 如果它确实存在,什么也不做。
  3. 如果不存在,则更改节点标签。() 中的中间值应替换为节点 ID。之后,如果被替换的值有下划线,用点替换
  4. 将更新的值发送到数据库中的测试表设置

以下是我尝试过的代码:

$SQLServer = "WIN-17V7QT0IJVK"
$SQLDBName = "Test"
$uid ="WIN-17V7QT0IJVK\Administrator"
$pwd = "letmebackinplease"
$ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
$SqlConnection.open()

$xml = New-Object XML
$n = "C:\Users\Administrator\Desktop\test2.xml"
$xml.Load($n)
$nodes = $xml.Searchable.Fields.Field
# $regex =  '()\(.*?\)'
$regex =  '\((.*)\)'
foreach($node in $nodes) {
    $a = $node.label
    $a -match $regex
    $data = $Matches[1]
    $z = $data.split(',')
    Write-Output($z[0])
    Write-Output($z[1])
    Write-Output($z[2])

    $query = "
     SELECT
        count(ID) as cnt
     FROM [Secure4].[secure].[Resource]
     WHERE
        [ResourceType] = " + $z[0]
    $query += " AND [ResourceKey] = " + $z[1]
    $query += " AND [ResourceValue] = " + $z[2]

    Write-Output($query)
    $Resource = (Invoke-SQLCmd -query $query -Server $SQLServer)
    Write-Output($Resource)
    $id = $node.id
    Write-Output($id)
    #$description = $node.Description
    #Write-Output($description)

    if($Resource -eq '0'){
        #$id = $node.id
        #Write-Output($id)
        #$description = $node.description
        #$mid_value = $id.replace('
        $new_label = "$RESX('Ebr.Crdm.Store.2_0',"+$id+",'XXXXXX')"
        $node.label = $new_label



    }
    Write-Output($node.label)
}

它出现在调试我的代码时,我无法正确更新标签。有人可以建议最好的方法来改变它。在运行代码时,我没有看到 $node.label 打印出来。

问题

  1. 在我的输出中看不到 $node.description 打印(已解决)
  2. 在 IF 块中更新后看不到 $node.label 打印出来

标签: .netxmlpowershellloops

解决方案


以下是您的 IF 块的编写方式:

if($Resource -match '0'){
        $mid_value = $id -replace "_","."
        $new_label = "$RESX(Ebr.Crdm.Store.2_0,"+$mid_value+","+$description+")"
        $node.label = $new_label
        Write-Output("The updated label is"+$node.label)

        $b = $node.label
        $b -match $regex
        $newdata = $Matches[1]
        $y = $newdata.split(',')
        Write-Output("The first updated element of the node label is"+" "+$y[0])
        Write-Output("The second updated element of the node label is"+" "+$y[1])
        Write-Output("The third updated element of the node label is"+" "+$y[2])
        $insert_query = "INSERT INTO [Secure4].[secure].[Resource]
               ([ResourceType],[CultureCode],[ResourceKey],[ResourceValue])
               VALUES
                    ('"+$y[0]+"','en','"+$y[1]+"','"+$y[2]+"')"
        Write-Output($insert_query)
         $Resource2 = (Invoke-SQLCmd -query $insert_query -Server $SQLServer) 
         #write-output($Resource2)

    }

推荐阅读