你如何通配符或字符串模式与XSLTv1.0匹配

时间:2018-05-01 22:10:52

标签: xml xslt xpath xslt-1.0

我的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>

1 个答案:

答案 0 :(得分:2)

由于我不确定您的预期输出,我提供了2个样式表:

1)保持Cookie节点的相同结构,只需将元素SecureHTTPOnly的值更改为1,然后添加action="update"属性

2)除了添加Cookie属性并将action="update"Secure的值更改为{{1}之外,删除HTTPOnly节点的所有其他子节点}

<强> INPUT:

1

案例1

样式表:

::::::::::::::
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>

说明:

复制每个节点/属性,然后在到达符合条件:::::::::::::: 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",然后在到达{{}时复制此元素下的所有内容1}}和name="{./@name}"模板将被触发,值将设置为1.其他不尊重Cookie[starts-with(@name,'PD_STATEFUL_')]/Secure/text()的Cookie不会受此更改的影响。

输出:

Cookie[starts-with(@name,'PD_STATEFUL_')]/HTTPOnly/text()

<强> CASE2:

样式表:

starts-with(@name,'PD_STATEFUL_')

说明:

此时到达尊重$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> 的节点 我们将节点内容覆盖到

::::::::::::::
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_')