I want to insert the following security constraint
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>AcctAdminRole</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>default</realm-name>
</login-config>
just once in a new XML applying xslt, only for the role-name:AcctAdminRole . This is my XML:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="BLA"
version="2.5">
<listener>
<listener-class>com.example</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>report.pdf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/config/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
In this xsl file, I'm controlling that it will added only once, but it is adding multiple times and I don't know how to apply only when the role-name is AcctAdminRole.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ee="http://java.sun.com/xml/ns/javaee"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="ee xs fn">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ee:web-app">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:if test="not(security-constraint)">
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>AcctAdminRole</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>default</realm-name>
</login-config>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Thanks for all
If you are applying the XSLT to an XML which already has a security-constraint
node in, you need to take into account namespaces. Your XSLT is adding the security-constraint
into the namespace "http://java.sun.com/xml/ns/javaee", but the xsl:if
is checking for a security-constraint
with no namespace.
There are two ways to solve this. One way would be to add a namespace prefix to the xsl:if
test
<xsl:if test="not(ee:security-constraint)">
Alternatively, as you are using XSLT 2.0, you could make use of xpath-default-namespace
to indicate the namespace to use for any unprefixed elements in xpath expressions
<xsl:stylesheet version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ee="http://java.sun.com/xml/ns/javaee"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xpath-default-namespace="http://java.sun.com/xml/ns/javaee"
exclude-result-prefixes="ee xs fn">
You might also like to consider putting the test on security-constraint
in the template match.
Try this XSLT
<xsl:stylesheet version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ee="http://java.sun.com/xml/ns/javaee"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xpath-default-namespace="http://java.sun.com/xml/ns/javaee"
exclude-result-prefixes="ee xs fn">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="web-app[not(security-constraint)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>AcctAdminRole</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>default</realm-name>
</login-config>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>