我的XML数据包含以下内容:
<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>
使用 XLSTv1.0,如何修改以下代码以设置以 PD_STATEFUL_* 开头的所有 cookie 的属性,而不是对每个特定的 cookie 名称进行编码?
<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>
由于我不确定您的预期输出,因此我提供了 2 个样式表:
1(保持Cookie
节点的结构相同,只需将元素的值更改为Secure
和HTTPOnly
1
并添加action="update"
属性
2( 在添加action="update"
属性并将Secure
和HTTPOnly
的值更改为1
的基础上,删除Cookie
节点的所有其他子节点
输入:
::::::::::::::
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>
案例1
样式表:
::::::::::::::
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>
解释:
复制每个节点/属性,然后在到达符合条件的节点时Cookie[starts-with(@name,'PD_STATEFUL_')]
添加属性action="update"
使用相同的属性name="{./@name}"
,然后在到达Cookie[starts-with(@name,'PD_STATEFUL_')]/Secure/text()
时复制此元素下的所有内容,Cookie[starts-with(@name,'PD_STATEFUL_')]/HTTPOnly/text()
将触发模板并将值设置为 1。其他不遵守starts-with(@name,'PD_STATEFUL_')
的 Cookie 将不受此更改的影响。
输出:
$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>
案例2:
样式表:
::::::::::::::
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>
解释:
在这里,当到达尊重Cookie[starts-with(@name,'PD_STATEFUL_')
的节点时 我们将节点内容覆盖到
<Cookie action="update" name="{./@name}">
<Secure>1</Secure>
<HTTPOnly>1</HTTPOnly>
</Cookie>
因此,其他子节点将丢失。
输出:
$ 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>