我们有一个如下所示的policy.xml文件,需要根据shell脚本的条件,在(
inbound or backend or outbound
)下的xml文件的每个会话下插入如下字符串,示例策略.xml
<base />
要添加的字符串
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
我们尝试使用 xmlstarlet 来实现此目的,但无法实现我们想要的
例如导入[policies/inbound/base]下的字符串
<rate-limit-by-key calls="xx" renewal-period="xx" counter-key="@(Regex.Match(context.x.y.GetValueOrDefault("xxxxxxx",""), @"^[.x-y]*")?.Value)" />`
我们的脚本
xmlstarlet edit --subnode "//policies/inbound/" xxxxxxxxxxxxxxxx policy.xml > custompolicy.xml
为“入站”成功的条件生成的自定义策略文件的预期输出
#!/bin/bash
################Variables##################
source parse_yaml.sh
eval $(parse_yaml input2.yaml policy)
ipfilter="<rate-limit-by-key calls="xxx" renewal-period="xx" counter-key="@(Regex.Match(context.x.y.GetValueOrDefault("xxxxxxx",""), @"^[.x-y]*")?.Value)" />"
echo ".............Eval Result..............................."
for f in $policy_ ; do eval echo \$f \$${f}_ ; done
echo "............Eval Result................................"
echo " ********policy is ************ "
for f in $policy_ ; do
if [[ $(eval echo \$${f}_name) == "ipfilter" ]]; then
echo " given policy name is ipfilter "
if [[ $(eval echo \$${f}_scope) == "api" ]]; then
echo "decided the scope of Ipfilter policy as api "
fi
for g in $(eval echo \$${f}_apis_); do
echo " this policy will be applied to apis, $g"
for h in $(eval echo \$${g}_session_); do
echo " this policy will be applied to api, $g under sesion $h "
if [[ $(eval echo \$${g}_session_) == "inbound" ]]; then
echo "add the add Ipfilter string to the inbound session of xml"
xmlstarlet edit --subnode "//policies/ibound/" xxxxxxxxxxxxxxxx policy.xml > custompolicy.xml
fi
done
XSLT 使用所谓的“身份转换”模式。
您只需要知道如何在您的环境中启动 XSLT 转换。
输入XML<policies>
<inbound>
<base />
<rate-limit-by-key calls="xxx" renewal-period="xx" counter-key="@(Regex.Match(context.x.y.GetValueOrDefault("xxxxxxx",""), @"^[.x-y]*")?.Value)" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
<policies>
<inbound>
<base/>
</inbound>
<backend>
<base/>
</backend>
<outbound>
<base/>
</outbound>
<on-error>
<base/>
</on-error>
</policies>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"
encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Identity transform-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="inbound | backend | outbound">
<xsl:copy>
<xsl:copy-of select="*"/>
<rate-limit-by-key calls="xx" renewal-period="xx" counter-key='@(Regex.Match(context.x.y.GetValueOrDefault("xxxxxxx",""), @"^[.x-y]*")?.Value)' />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>