Xml XSLT插入同级(如果不插入';不存在-由于某些原因无法重新命名
我试图仅在元素不存在时才向元素添加同级 以下是我的XML:Xml XSLT插入同级(如果不插入';不存在-由于某些原因无法重新命名,xml,xslt,xpath,Xml,Xslt,Xpath,我试图仅在元素不存在时才向元素添加同级 以下是我的XML: <?xml version='1.0' encoding='UTF-8'?> <domain xmlns="http://xmlns.oracle.com/weblogic/domain" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/security/
<?xml version='1.0' encoding='UTF-8'?>
<domain xmlns="http://xmlns.oracle.com/weblogic/domain" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/security/xacml http://xmlns.oracle.com/weblogic/security/xacml/1.0/xacml.xsd http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator/1.0/passwordvalidator.xsd http://xmlns.oracle.com/weblogic/domain http://xmlns.oracle.com/weblogic/1.0/domain.xsd http://xmlns.oracle.com/weblogic/security http://xmlns.oracle.com/weblogic/1.0/security.xsd http://xmlns.oracle.com/weblogic/security/wls http://xmlns.oracle.com/weblogic/security/wls/1.0/wls.xsd">
<security-configuration>
<realm>
<sec:authentication-provider xsi:type="wls:default-authenticatorType">
<wls:use-retrieved-user-name-as-principal>true</wls:use-retrieved-user-name-as-principal>
</sec:authentication-provider>
<sec:authentication-provider xsi:type="wls:default-identity-asserterType">
<sec:active-type>AuthenticatedUser</sec:active-type>
</sec:authentication-provider>
</realm>
</security-configuration>
</domain>
真的
认证用户
所需输出应为:
<domain xmlns="http://xmlns.oracle.com/weblogic/domain" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/security/xacml http://xmlns.oracle.com/weblogic/security/xacml/1.0/xacml.xsd http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator/1.0/passwordvalidator.xsd http://xmlns.oracle.com/weblogic/domain http://xmlns.oracle.com/weblogic/1.0/domain.xsd http://xmlns.oracle.com/weblogic/security http://xmlns.oracle.com/weblogic/1.0/security.xsd http://xmlns.oracle.com/weblogic/security/wls http://xmlns.oracle.com/weblogic/security/wls/1.0/wls.xsd">
<security-configuration>
<realm>
<sec:authentication-provider xsi:type="wls:default-authenticatorType">
<wls:use-retrieved-user-name-as-principal>true</wls:use-retrieved-user-name-as-principal>
</sec:authentication-provider>
<sec:authentication-provider xsi:type="wls:default-identity-asserterType">
<sec:active-type>AuthenticatedUser</sec:active-type>
</sec:authentication-provider>
<!-- Just adding this element -->
<sec:authentication-provider xmlns:ext="http://xmlns.oracle.com/weblogic/security/extension" xsi:type="ext:session-authenticatorType">
<sec:name>SessionAuthenticator</sec:name>
<sec:control-flag>SUFFICIENT</sec:control-flag>
</sec:authentication-provider>
</realm>
</security-configuration>
</domain>
真的
认证用户
会话认证器
充足的
这个转换需要重新命名,所以我只需要添加不存在的新元素
以下是我尝试使用的XLST:
<xsl:stylesheet version="1.0" xmlns:bi="http://xmlns.oracle.com/weblogic/domain"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://xmlns.oracle.com/weblogic/security"
xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls">
<xsl:output method="xml" version="1.0" encoding='UTF-8' indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<!-- Identity transform -->
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- match the existing child of realm -->
<xsl:template match="bi:realm/sec:authentication-provider[@xsi:type='wls:default-identity-asserterType']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- If the element doesn't exist already add it as a sibling -->
<xsl:if test="not(bi:realm/sec:authentication-provider[@xsi:type='ext:session-authenticatorType'])">
<sec:authentication-provider xmlns:ext="http://xmlns.oracle.com/weblogic/security/extension" xsi:type="ext:session-authenticatorType">
<sec:name>SessionAuthenticator</sec:name>
<sec:control-flag>SUFFICIENT</sec:control-flag>
</sec:authentication-provider>
</xsl:if>
</xsl:template>
会话认证器
充足的
运行此转换时,将添加所需的元素。然而问题是,这不是重新命名的。每次运行此命令时,都会忽略对新元素存在性的检查。有什么想法吗?您的模板与
sec:authentication provider
元素匹配:
match="bi:realm/sec:authentication-provider[...]"
因此,在模板中,上下文节点是匹配的sec:authentication provider
元素。因此,当您执行测试时,您需要从该上下文开始。您当前的测试
test="not(bi:realm/sec:authentication-provider[@xsi:type='ext:session-authenticatorType'])"
只有当上下文节点是bi:realm
的父节点,即bi:security configuration
时,才会工作。由于上下文节点是一个sec:authentication provider
元素,并且您希望测试兄弟节点,因此您的测试应如下所示:
test="not(../sec:authentication-provider[@xsi:type='ext:session-authenticatorType'])"
实际上,如果我没有弄错的话,您测试的兄弟姐妹总是在匹配元素之后。因此,您可以使用以下兄弟姐妹::而不是
。/
您是对的。您是否知道,如果删除名称空间id xmlns:bi,为什么无法匹配match=“bi:realm/sec:authentication provider[…]”?似乎match=“realm/sec:authentication provider[…]”不起作用。但是,除了从继承的默认名称空间之外,域没有使用特定名称空间定义。@keftes,没错:
从
继承默认名称空间,即URI为http://xmlns.oracle.com/weblogic/domain
。因此,如果希望匹配模式匹配领域
,则必须在匹配模式中指定该名称空间(或者显式地使匹配模式名称空间不敏感)。在匹配模式中指定名称空间的方法是为其声明前缀(xmlns:bi=”http://xmlns.oracle.com/weblogic/domain“
),然后在匹配模式中使用bi
前缀。