Php 根据属性值删除XML节点
我想使用XSLV1根据属性值从XML中删除一些节点。 到目前为止我知道怎么做() 我的问题是,我想向XSL传递某种数组/字符串,以便告诉XSL要删除哪个节点 例如: XML:Php 根据属性值删除XML节点,php,xml,zend-framework,xslt,xslt-1.0,Php,Xml,Zend Framework,Xslt,Xslt 1.0,我想使用XSLV1根据属性值从XML中删除一些节点。 到目前为止我知道怎么做() 我的问题是,我想向XSL传递某种数组/字符串,以便告诉XSL要删除哪个节点 例如: XML:
XSL:
当我调用转换时,我会将类似“A-D”的内容传递给“codes”参数。
我等待的结果是:
<Segments xmlns="http://www.exchangefortravel.org/xft/current" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Segments Name="PrixDispo" What="List">
<Segment xsi:type="SegmentProductType" Index="1">
<Code Role="Reference" Value="A"/>
<Descriptions></Descriptions>
</Segment>
<Segment xsi:type="SegmentProductType" Index="2">
<Code Role="Reference" Value="D"/>
<Descriptions></Descriptions>
</Segment>
</Segments>
</Segments>
我的两个问题是:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xft="http://www.exchangefortravel.org/xft/current" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="codes"/>
<xsl:template match="xft:Segment[@xsi:type='SegmentProductType']">
<xsl:variable name="current_code" select="xft:Code[@Role='Reference']/@Value"/>
<xsl:if test="contains($codes, $current_code)">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
EDIT2:
好的,实际上我试着做的是oposite,意思是只保留'A'和'D'节点,但问题是一样的,当我找到如何做的时候,我会根据我在这里描述的问题调整解决方案
我用我正在处理的真实内容更新了代码。将值输入XSLT
参数
您可以向XSLTProcessor提供参数
$xslt = new XsltProcessor();
$xslt->importStylesheet($xslDom);
$xslt->setParameter('', 'PARAMETER_NAME', 'A D');
。。。可以在XSLT中定义为样式表元素的子元素:
<xsl:stylesheet version="1.0" xmlns:xsl="...">
<xsl:param name="PARAMETER_NAME">default</xsl:param>
...
</xsl:stylesheet>
节点集
如果这些值是一个节点集,则可以通过从中选择来进行检查。您必须将当前值放入变量中
<xsl:template match="segments/segment/code">
<xsl:variable name="current" select="@value"/>
<xsl:if test="count($filterValues/value[text() = $current]) = 0">
<!-- not in filter -->
</xsl>
</xsl:template>
正确传递参数时,以下XSLT将起到作用:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="codes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="segment">
<xsl:choose>
<xsl:when test="contains($codes, code/@value)" /> <!-- Do nothing -->
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
上述XSLT将只输出与传入参数的代码不匹配的段。如果要执行相反的操作(仅显示参数中传递的代码),请更改:
致:
我终于做到了。
以下是排除除代码允许的元素外的所有元素的正确XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xft="http://www.exchangefortravel.org/xft/current" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="codes"/>
<!-- By Default copy all elements -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- For the Segment elements retreive the coresponding code and look for a match -->
<xsl:template match="xft:Segment[@xsi:type='SegmentProductType']">
<xsl:variable name="current_code" sele ct="xft:Code[@Role='Reference']/@Value"/>
<!-- If we have valid code copy the element -->
<xsl:if test="contains($codes, concat($current_code, '-'))">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
非常感谢@MarkVeenstra您能告诉我参数将如何填充(
)。你说“像‘A-D’之类的”。你能说得具体点吗?A-D是指A到D,还是指A和D?如果您传递4个值,它会是什么样子?@MarkVeenstra“A-D”只是一个代码列表,它不是一个间隔,它可以是“A-D-B”。在运行转换时,我通过将一些参数设置到XSL处理器来传递此值:$proc=new XSLTProcessor()代码>$proc->setParameter(''code','A-D')代码>$proc->transformToXml($xml\u-doc)代码>太棒了,我要试试这些。谢谢谢谢我用真实案例更新了我的问题senario,我快到了,但我无法从转换中得到任何东西。你在我上次的编辑中看到什么不对的地方吗?
<xsl:stylesheet version="1.0" xmlns:xsl="...">
<xsl:variable name="values" select="document('mystream://identifier')/values"/>
...
</xsl:stylesheet>
$xslt = new XsltProcessor();
$xslt->importStylesheet($xslDom);
$xslt->registerPHPFunctions(array('callbackFetchingValues'));
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
extension-element-prefixes="php">
<xsl:variable name="values" select="php:function('callbackFetchingValues')/values"/>
</xsl:stylesheet>
<div class="tokenOne tokenTwo">...</div>
segments/segment/code[contains(concat(' ', normalize-space($TOKEN_LIST), ' '), concat(' ', @value, ' '))]
<xsl:template match="segments/segment/code">
<xsl:variable name="current" select="@value"/>
<xsl:if test="count($filterValues/value[text() = $current]) = 0">
<!-- not in filter -->
</xsl>
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="codes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="segment">
<xsl:choose>
<xsl:when test="contains($codes, code/@value)" /> <!-- Do nothing -->
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="segment">
<xsl:choose>
<xsl:when test="contains($codes, code/@value)" /> <!-- Do nothing -->
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="segment">
<xsl:choose>
<xsl:when test="contains($codes, code/@value)">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xft="http://www.exchangefortravel.org/xft/current" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="codes"/>
<!-- By Default copy all elements -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- For the Segment elements retreive the coresponding code and look for a match -->
<xsl:template match="xft:Segment[@xsi:type='SegmentProductType']">
<xsl:variable name="current_code" sele ct="xft:Code[@Role='Reference']/@Value"/>
<!-- If we have valid code copy the element -->
<xsl:if test="contains($codes, concat($current_code, '-'))">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>