user9727543
user9727543

Reputation: 11

How do you wildcard or string pattern match with XSLTv1.0

My XML data contains the following:

    <Cookies>
    </Cookie>
    <Cookie name="PD_STATEFUL_2707f5b6-48e3-11e8-bb87-000c2953888d">
    <Value>%2Fportal</Value>
    <Path>/</Path><Domain></Domain><Expires></Expires><Secure>0</Secure> 
    <HTTPOnly>0</HTTPOnly>
    </Cookie>
    <Cookie name="PD_STATEFUL_2808f5b6-48e3-11e8-bb87-000c2953180d">
    <Value>%2Fwasapp</Value>
    <Path>/</Path><Domain></Domain><Expires></Expires><Secure>0</Secure> 
    <HTTPOnly>0</HTTPOnly>
    </Cookie>
    </Cookies>

Using XLSTv1.0, how do I modify the following code to set attributes for all cookies starting with PD_STATEFUL_* rather than coding each specific cookie name?

    <xsl:template match="//HTTPResponse/Cookies">
        <xsl:if test="Cookie/@name='PD_STATEFUL_2707f5b6-48e3-11e8-bb87-000c2953180d'">
            <Cookie action="update" name="PD_STATEFUL_2707f5b6-48e3-11e8-bb87-000c2953180d">
                <Secure>1</Secure>
                <HTTPOnly>1</HTTPOnly>
            </Cookie>
        </xsl:if>
        <xsl:if test="Cookie/@name='PD_STATEFUL_2808f5b6-48e3-11e8-bb87-000c2953180d'">
            <Cookie action="update" name="PD_STATEFUL_2808f5b6-48e3-11e8-bb87-000c2953180d">
                <Secure>1</Secure>
                <HTTPOnly>1</HTTPOnly>
            </Cookie>
        </xsl:if>
    </xsl:template>

Upvotes: 1

Views: 300

Answers (1)

Allan
Allan

Reputation: 12438

Since I was not sure about your expected output I have provided 2 stylesheets:

1) Keep the same structure of the Cookie node and just change the values of elements Secure and HTTPOnly to 1 and add the action="update" attribute

2) Remove all other sub nodes of the Cookie node on top of adding the action="update" attribute and changing the value of Secure and HTTPOnly to 1

INPUT:

::::::::::::::
cookies.xml
::::::::::::::
<?xml version="1.0" encoding="utf-8"?>
<Cookies>
  <Cookie name="PD_STATEFUL_2707f5b6-48e3-11e8-bb87-000c2953888d">
    <Value>%2Fportal</Value>
    <Path>/</Path>
    <Domain/>
    <Expires/>
    <Secure>0</Secure>
    <HTTPOnly>0</HTTPOnly>
  </Cookie>
  <Cookie name="PD_STATEFUL_2808f5b6-48e3-11e8-bb87-000c2953180d">
    <Value>%2Fwasapp</Value>
    <Path>/</Path>
    <Domain/>
    <Expires/>
    <Secure>0</Secure>
    <HTTPOnly>0</HTTPOnly>
  </Cookie>
</Cookies>

CASE 1

STYLESHEET:

::::::::::::::
cookies.xsl
::::::::::::::
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
    <xsl:template match="Cookie[starts-with(@name,'PD_STATEFUL_')]">
        <Cookie action="update" name="{./@name}">
                <xsl:apply-templates select="@*|node()"/>
        </Cookie>
    </xsl:template>
    <xsl:template match="Cookie[starts-with(@name,'PD_STATEFUL_')]/Secure/text()">1</xsl:template>
    <xsl:template match="Cookie[starts-with(@name,'PD_STATEFUL_')]/HTTPOnly/text()">1</xsl:template>
</xsl:stylesheet>

EXPLANATIONS:

Copy every node/attribute then when reaching nodes that respect the condition Cookie[starts-with(@name,'PD_STATEFUL_')] you add the attribute action="update" use the same attribute name="{./@name}" and then copy everything under this element when reaching Cookie[starts-with(@name,'PD_STATEFUL_')]/Secure/text() and Cookie[starts-with(@name,'PD_STATEFUL_')]/HTTPOnly/text() the template will be triggered and the value will be set to 1. Other Cookie that do not respect starts-with(@name,'PD_STATEFUL_') will not be affected by this change.

OUTPUT:

$xsltproc cookies.xsl cookies.xml | xmllint --format -                                                                         
<?xml version="1.0"?>
<Cookies>
  <Cookie action="update" name="PD_STATEFUL_2707f5b6-48e3-11e8-bb87-000c2953888d">
    <Value>%2Fportal</Value>
    <Path>/</Path>
    <Domain/>
    <Expires/>
    <Secure>1</Secure>
    <HTTPOnly>1</HTTPOnly>
  </Cookie>
  <Cookie action="update" name="PD_STATEFUL_2808f5b6-48e3-11e8-bb87-000c2953180d">
    <Value>%2Fwasapp</Value>
    <Path>/</Path>
    <Domain/>
    <Expires/>
    <Secure>1</Secure>
    <HTTPOnly>1</HTTPOnly>
  </Cookie>
</Cookies>

CASE2:

STYLESHEET:

::::::::::::::
cookies2.xsl
::::::::::::::
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
    <xsl:template match="Cookie[starts-with(@name,'PD_STATEFUL_')]">
        <Cookie action="update" name="{./@name}">
                <Secure>1</Secure>
                <HTTPOnly>1</HTTPOnly>
        </Cookie>
    </xsl:template>
</xsl:stylesheet>

EXPLANATION:

Here when reaching nodes that respect Cookie[starts-with(@name,'PD_STATEFUL_') we overwrite the node content to

        <Cookie action="update" name="{./@name}">
                <Secure>1</Secure>
                <HTTPOnly>1</HTTPOnly>
        </Cookie>

Therefore other subnodes will be lost.

OUTPUT:

$ xsltproc cookies2.xsl cookies.xml | xmllint --format -
<?xml version="1.0"?>
<Cookies>
  <Cookie action="update" name="PD_STATEFUL_2707f5b6-48e3-11e8-bb87-000c2953888d">
    <Secure>1</Secure>
    <HTTPOnly>1</HTTPOnly>
  </Cookie>
  <Cookie action="update" name="PD_STATEFUL_2808f5b6-48e3-11e8-bb87-000c2953180d">
    <Secure>1</Secure>
    <HTTPOnly>1</HTTPOnly>
  </Cookie>
</Cookies>

Upvotes: 2

Related Questions