首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >受属性值对限制的XSLT副本

受属性值对限制的XSLT副本
EN

Stack Overflow用户
提问于 2014-09-30 03:35:39
回答 3查看 590关注 0票数 1

我正在尝试将两个子文件从XML文件复制到另一个XML文件中。我创建了一个为命名元素工作的白名单,但是当我试图限制一个属性名称值对(它也是一个子元素)时,我找不到匹配项。

示例XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<catalog catalog-id="Primary">
    <product product-id="COLAKIT">
        <ean/>
        <upc/>
        <unit>SKU</unit>
        <custom-attributes>
            <custom-attribute attribute-id="Base_Color">Brown</custom-attribute>
            <custom-attribute attribute-id="Shipping_Cost">0.0</custom-attribute>
            <custom-attribute attribute-id="showEstimatedDelivery">false</custom-attribute>
        </custom-attributes>
        <classification-category>Kitchen_Housewares-Coffee_and_Tea</classification-category>
    </product>

    <product product-id="COLONIAL-48-M-K">
        <ean/>
        <upc/>
        <unit>SKU</unit>
        <custom-attributes>
            <custom-attribute attribute-id="Base_Color">Coffee</custom-attribute>
            <custom-attribute attribute-id="Shipping_Cost">0.0</custom-attribute>
            <custom-attribute attribute-id="showEstimatedDelivery">false</custom-attribute>
        </custom-attributes>
        <classification-category>Outdoor_Living-Heaters-Fireplaces</classification-category>
    </product>

</catalog>

XSLT:

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <ns:WhiteList>
  <name>classification-category</name>
  <name>custom-attribute[attribute-id()='Shipping_Cost']</name>
 </ns:WhiteList>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]"/>
</xsl:stylesheet>

此外,主XML有1000多个节点,我只想要3个节点,这就是为什么我试图使用白色列表而不是排除。请让我知道我错过了什么与attribute=value比赛。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-30 06:56:14

当然,上面所使用的尝试不起作用,因为您的源文档没有任何名称为custom-attribute[attribute-id()='Shipping_Cost']的元素(而且,没有名为attribute-id()的XPath函数)。

标准XSLT1.0不具备从XML中存在的字符串值计算XPaths的能力,因此我认为您可能需要采取一种比您希望的方法更不通用的方法。以下是一种这样的方法:

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <ns:WhiteList>
  <element>classification-category</element>
  <attributeId>Shipping_Cost</attributeId>
 </ns:WhiteList>
 <xsl:variable name="whiteList" select="document('')/*/ns:WhiteList" />

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="*[not(descendant-or-self::*[name()=$whiteList/element] or
                            (descendant-or-self::custom-attribute/@attribute-id = 
                             $whiteList/attributeId)
                           )]"/>
</xsl:stylesheet>
票数 1
EN

Stack Overflow用户

发布于 2014-09-30 06:54:26

您的方法的问题是"custom-attribute[attribute-id()='Shipping_Cost']"不是名称。它甚至不是(在您的例子中) XPath表达式;它是一个字符串,您需要一个扩展函数(或XSLT3.0)才能将计算为XPath。您必须更改它,以便计算为有效的 XPath,即使用@attribute-id而不是attribute-id()

你的方法更大的问题是它效率很低。在XSLT中,显式是值得的,特别是当您谈论数百个节点时。与其构建一个“侧面”的名字白名单,不如考虑一种更直接的方法:

XSLT1.0

代码语言:javascript
复制
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- "white list" -->
<xsl:template match="product">
    <xsl:copy>
        <xsl:copy-of select="@product-id"/>
        <xsl:copy-of select="classification-category"/>
        <xsl:copy-of select="custom-attributes/custom-attribute[@attribute-id='Shipping_Cost']"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
票数 1
EN

Stack Overflow用户

发布于 2014-09-30 08:41:27

可以在XSLT1.0中处理各种类型的动态计算,但需要执行两个转换。它利用了XSLT是格式良好的XML这一事实,这意味着您可以使用XSLT将其转换为其他XSLT。一种"XSLT编写自己“的例子。

这样做的想法是,您有一个预处理步骤,将当前的XSLT转换为一个新的改进版本的XSLT,包括您的“动态”表达式,然后将其应用于XML:

首先,将当前的XSLT更改为:

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <ns:WhiteList>
  <name>classification-category</name>
  <name>custom-attribute[@attribute-id = 'Shipping_Cost']</name>
 </ns:WhiteList>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

对于转换的第一个“预处理”步骤,您将有一个由身份模板组成的XSLT,但也有一个与复制它的xsl:styleheet匹配的模板,但也添加了一个新的xsl:template指令,其match属性是根据您的ns:WhiteList信息生成的。

以下是XSLT的预处理。

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xslOut="xlst.temp" xmlns:ns="some:ns">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:namespace-alias stylesheet-prefix="xslOut" result-prefix="xsl"/>

    <xsl:template match="/xsl:stylesheet">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>

            <xslOut:template>
                <xsl:attribute name="match">
                    <xsl:text>*[</xsl:text>
                    <xsl:for-each select="ns:WhiteList/name">
                        <xsl:if test="position() > 1"> and </xsl:if>
                        <xsl:text>not(descendant-or-self::</xsl:text>
                        <xsl:value-of select="." />
                        <xsl:text>)</xsl:text>
                    </xsl:for-each>
                    <xsl:text>]</xsl:text>
                </xsl:attribute>
            </xslOut:template>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ns:WhiteList" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

注意xsl:namespace-alias的使用,这里使用它字面上输出xslt元素(如果您试图用<xsl:template>替换<xslOut:template>,会导致错误)。

当将预处理XSLT应用于初始XSLT时,将生成此修改后的XSLT。

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns="some:ns"
                version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

   <xsl:template match="node()|@*">
     <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
   </xsl:template>
   <xsl:template match="*[not(descendant-or-self::classification-category) and not(descendant-or-self::custom-attribute[@attribute-id = 'Shipping_Cost'])]"/>
</xsl:stylesheet>

然后,可以将此生成的XSLT应用于XML,以输出以下内容:

代码语言:javascript
复制
<catalog catalog-id="Primary">
   <product product-id="COLAKIT">
      <custom-attributes>
         <custom-attribute attribute-id="Shipping_Cost">0.0</custom-attribute>
      </custom-attributes>
      <classification-category>Kitchen_Housewares-Coffee_and_Tea</classification-category>
   </product>
   <product product-id="COLONIAL-48-M-K">
      <custom-attributes>
         <custom-attribute attribute-id="Shipping_Cost">0.0</custom-attribute>
      </custom-attributes>
      <classification-category>Outdoor_Living-Heaters-Fireplaces</classification-category>
   </product>
</catalog>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26112377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档