我有两个文件:Rules_Generic文件(generic.xml)和(Rules_Values.txt)。 我想找到一种方法,通过从generic.xml中提取Rules语法并填充Rules_Values.txt中的值来构建新的XML文件
我搜索了发现可以使用XSLT。以下是(Rules_Values.txt)
#VNF_Type VNF_Id min_num_Instance max_num_Instance ScalingOut% ScalingIn% min_num_CPUs max_num_CPUs Migration_Type
Firewall f1 1 4 60% 20% 4 8 Pre-Copy
Firewall f2 2 6 80% 10% 2 6 Post-Copy
Cache c1 2 8 90% 30% 2 6 Hybrid
以下是(generic.xml)
<?xml version="1.0" encoding="UTF-8"?>
<PolicySet PolicySetId="PS2" PolicySetName="ResourceAdaptation" Client_Id =" ">
<PolicySet SFC_Name="">
<Policy PolicyId=" ">
<Target>
<Subject>VNF_Type</Subject>
</Target>
<!-- Horizontal_Scaling -->
<Rule RuleId="R1" Effect="Affinity">
<Target>
<Subject>VNF_ID</Subject>
<Action>Horizontal_Scaling</Action>
<Resource>numOfInstances</Resource>
</Target>
<conditions>
<condition>min_numOfInstances: Any_Positive_Integer</condition>
<condition>max_numOfInstances: Any_Positive_Integer</condition>
</conditions>
</Rule>
<!-- Vertical_Scaling -->
<Rule RuleId="R2" Effect="Affinity">
<Target>
<Subject>VNF_ID</Subject>
<Action>Vertical_Scaling</Action>
<Resource>numOfCPUcores</Resource>
</Target>
<conditions>
<condition>min_numOfCPUcores: Any_Positive_Integer</condition>
<condition>max_numOfCPUcores: Any_Positive_Integer</condition>
</conditions>
</Rule>
<!-- Horizontal-Scaling threshold-based 1- scaling_Out 2- scaling_In -->
<Rule RuleId = "R3" Effect="Affinity">
<Target>
<Subject>VNF_ID</Subject>
<Action>Scaling_Out</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization greater_than threshold%</condition>
</conditions>
</Rule>
<Rule RuleId = "R4" Effect="Affinity">
<Target>
<Subject>VNF_ID</Subject>
<Action>Scaling_In</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization less_than threshold%</condition>
</conditions>
</Rule>
<!-- For Migration : specify 1. MigrationType 2. -->
<Rule RuleId = "R7" Effect="Affinity">
<Target>
<Subject>VNF_ID</Subject>
<Action>Migration</Action>
</Target>
<conditions>
<condition>MigrationType: [Pre-Copy , Post-Copy, Hybrid]</condition>
</conditions>
</Rule>
</Policy>
</PolicySet>
</PolicySet>
和所需的输出(out.xml)
<?xml version="1.0" encoding="UTF-8"?>
<PolicySet PolicySetId="PS2" PolicySetName="ResourceAdaptation" Client_Id =" ">
<PolicySet SFC_Name="">
<Policy PolicyId="p1">
<Target>
<Subject>Firewall</Subject>
</Target>
<Rule RuleId="R1" Effect="Affinity">
<Target>
<Subject>f1</Subject>
<Action>Horizontal_Scaling</Action>
<Resource>numOfInstances</Resource>
</Target>
<conditions>
<condition>min_numOfInstances: 1</condition>
<condition>max_numOfInstances: 4</condition>
</conditions>
</Rule>
<Rule RuleId="R2" Effect="Affinity">
<Target>
<Subject>f1</Subject>
<Action>Vertical_Scaling</Action>
<Resource>numOfCPUcores</Resource>
</Target>
<conditions>
<condition>min_numOfCPUcores: 4</condition>
<condition>max_numOfCPUcores: 8</condition>
</conditions>
</Rule>
<Rule RuleId = "R3" Effect="Affinity">
<Target>
<Subject>f1</Subject>
<Action>Scaling_Out</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization greater_than 60%</condition>
</conditions>
</Rule>
<Rule RuleId = "R4" Effect="Affinity">
<Target>
<Subject>f1</Subject>
<Action>Scaling_In</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization less_than 20%</condition>
</conditions>
</Rule>
<Rule RuleId = "R5" Effect="Affinity">
<Target>
<Subject>f1</Subject>
<Action>Migration</Action>
</Target>
<conditions>
<condition>MigrationType: Pre-Copy</condition>
</conditions>
</Rule>
<Rule RuleId="R6" Effect="Affinity">
<Target>
<Subject>f2</Subject>
<Action>Horizontal_Scaling</Action>
<Resource>numOfInstances</Resource>
</Target>
<conditions>
<condition>min_numOfInstances: 2</condition>
<condition>max_numOfInstances: 6</condition>
</conditions>
</Rule>
<Rule RuleId="R7" Effect="Affinity">
<Target>
<Subject>f2</Subject>
<Action>Vertical_Scaling</Action>
<Resource>numOfCPUcores</Resource>
</Target>
<conditions>
<condition>min_numOfCPUcores: 2</condition>
<condition>max_numOfCPUcores: 6</condition>
</conditions>
</Rule>
<Rule RuleId = "R8" Effect="Affinity">
<Target>
<Subject>f2</Subject>
<Action>Scaling_Out</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization greater_than 80%</condition>
</conditions>
</Rule>
<Rule RuleId = "R9" Effect="Affinity">
<Target>
<Subject>f2</Subject>
<Action>Scaling_In</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization less_than 10%</condition>
</conditions>
</Rule>
<Rule RuleId = "R10" Effect="Affinity">
<Target>
<Subject>f1</Subject>
<Action>Migration</Action>
</Target>
<conditions>
<condition>MigrationType: Post-Copy</condition>
</conditions>
</Rule>
</Policy>
<Policy PolicyId="p2">
<Target>
<Subject>Cache</Subject>
</Target>
<Rule RuleId="R11" Effect="Affinity">
<Target>
<Subject>c1</Subject>
<Action>Horizontal_Scaling</Action>
<Resource>numOfInstances</Resource>
</Target>
<conditions>
<condition>min_numOfInstances: 2</condition>
<condition>max_numOfInstances: 8</condition>
</conditions>
</Rule>
<Rule RuleId="R12" Effect="Affinity">
<Target>
<Subject>c1</Subject>
<Action>Vertical_Scaling</Action>
<Resource>numOfCPUcores</Resource>
</Target>
<conditions>
<condition>min_numOfCPUcores: 2</condition>
<condition>max_numOfCPUcores: 6</condition>
</conditions>
</Rule>
<Rule RuleId = "R13" Effect="Affinity">
<Target>
<Subject>c1</Subject>
<Action>Scaling_Out</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization greater_than 90%</condition>
</conditions>
</Rule>
<Rule RuleId = "R14" Effect="Affinity">
<Target>
<Subject>c1</Subject>
<Action>Scaling_In</Action>
<Resource>cpu_Utilization</Resource>
</Target>
<conditions>
<condition>utilization less_than 30%</condition>
</conditions>
</Rule>
<Rule RuleId = "R15" Effect="Affinity">
<Target>
<Subject>f1</Subject>
<Action>Migration</Action>
</Target>
<conditions>
<condition>MigrationType: Hybrid</condition>
</conditions>
</Rule>
</Policy>
</PolicySet>
</PolicySet>
我的代码如下(.java):
public static void main(String[] args) throws SaxonApiException {
Processor proc = new Processor(false);
XsltCompiler comp = proc.newXsltCompiler();
XsltExecutable exp = comp.compile(new StreamSource(new File(
"C:/Users/eclipse-workspace/PolicyFiles_Generator_V2/src/transformation.xsl")));
Serializer out = proc.newSerializer();
out.setOutputProperty(Serializer.Property.METHOD, "xml");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
out.setOutputFile(new File("output.xml"));
XsltTransformer trans = exp.load();
trans.setInitialTemplate(new QName("main"));
trans.setDestination(out);
trans.transform();
}
.xsl代码是:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vText" select="tokenize(unparsed-text('file:///C:/Users/eclipse-workspace/PolicyFiles_Generator_V2/src/Rules_Values.txt'),'
')"/>
<xsl:template match="/">
<shirts>
<xsl:apply-templates select="document('C:/Users/Hsuwi/eclipse-workspace/PolicyFiles_Generator_V2/src/GenericTemplate.xml')"/>
<xsl:apply-templates/>
</shirts>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="main" match="/">
<items>
<xsl:for-each select="$vText">
<item>
...........
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>
我不知道如何完成xsl代码,以便从第一个xml文件中提取规则,并使用.txt文件中的值填充每个规则
有什么建议吗?救命 ??或者是否有其他方法可以达到目标。 .txt文件非常灵活(这意味着我可以重新排序元素)
答案 0 :(得分:0)
您的问题中有太多和太多的示例数据来寻找准确的答案,但是正如评论中所说,我想知道是否和一种方法将定义的模板或函数定义为将要填充的值的数量作为命名参数,例如
<xsl:function name="mf:fill-template" as="element(Policy)">
<xsl:param name="p1"/>
<xsl:param name="p2"/>
<xsl:param name="p3"/>
<Policy xsl:expand-text="yes">
<Rule>
<foo>test {$p2}</foo>
</Rule>
<Rule att="{$p1}">
...
</Rule>
<Rule>
<header>...</header>
<value>... {$p3}</value>
</Rule>
</Policy>
</xsl:function>
,然后根据需要为文本中的每一行调用该函数,例如
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:function name="mf:fill-template" as="element(Policy)">
<xsl:param name="p1"/>
<xsl:param name="p2"/>
<xsl:param name="p3"/>
<Policy xsl:expand-text="yes">
<Rule>
<foo>test {$p2}</foo>
</Rule>
<Rule att="{$p1}">
...
</Rule>
<Rule>
<header>...</header>
<value>... {$p3}</value>
</Rule>
</Policy>
</xsl:function>
<xsl:param name="data" as="xs:string">P1 P2 P3
a b c
c d f
g h i</xsl:param>
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<PolicySet>
<xsl:for-each select="tail(tokenize($data, '\r?\n'))">
<xsl:sequence select="let $args := tokenize(., '\s+') return mf:fill-template($args[1], $args[2], $args[3])"/>
</xsl:for-each>
</PolicySet>
</xsl:template>
</xsl:stylesheet>
为了完整性和紧凑性,该示例使用字符串参数,但是您当然可以使用<xsl:param name="data" select="unparsed-text('file.txt')"/>
。
在线示例位于https://xsltfiddle.liberty-development.net/bFDb2CH。
不确定是否有帮助,但将建议塞入评论中将非常困难。
我意识到我没有处理您的输入XML,而是假设您能够对其进行修改并使用其模板/序列构造函数构造函数。可能有可能实现自动化,但是目前我看不到如何将各种列名匹配到XML示例中文本位置的不同和可变的地方。