2012-02-29 36 views
0

如何为类型属性位于xsd:namespace中的元素指定匹配项?例如:如何匹配其类型名称空间为xsd的元素:字符串

<enitityID maxOccurs="0" minOccurs="0" type="xsd:string"/> 

我试图

<xsl:template match="*[namespace-uri(@type)= 'http://www.w3.org/2001/XMLSchema']"> 
... 
</xsl:template> 

,但它似乎并没有工作。谢谢。

回答

0

该属性值中的xsd:不是一个名称空间声明;它只是属性值的一部分;你只需要@type = 'xsd:string'来匹配它。

编辑:根据意见,以匹配任何开始xsd:,你可以只使用substring-before(@type,':') = 'xsd'substring(@type,1,4) = 'xsd:'

+0

我需要搭配其他XSD类型之外的xsd:字符串 – bretter 2012-02-29 15:23:51

+1

答案是 的 您的评论让我看着办吧出。 – bretter 2012-02-29 15:29:42

+0

嘿,我正要编辑:) – Flynn1179 2012-02-29 16:03:28

0

在XPath中,前缀不确定的属性名称始终被认为是在“no namespace”中。

因此,type属性没有名称空间。

只需使用

<xsl:template match="*[@type = 'xsd:string']"> 
... 
</xsl:template> 

当然,上述匹配模式不仅identityID元件,但匹配任何元素的字符串值,其type属性是'xsd:string'

UPDATE:该OP已“承认了注释”,他居然需要其type属性指定在XML Schema命名空间的名称的任何元素匹配。

这是一个正确的解决方案(业务方案的解决方案仅适用于一个固定的前缀):

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match= 
    "*[namespace::* 
     [name() = substring-before(../@type, ':') 
     and 
     . = 'http://www.w3.org/2001/XMLSchema' 
     ] 
    ]"> 
    <xsl:copy-of select="."/> 
</xsl:template> 
</xsl:stylesheet> 

这种转变相匹配的任何元素,其type属性的值是在XML架构命名空间的名称 - 不管使用的前缀是

当应用于,例如,在下面的XML文档

<t xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<enitityID maxOccurs="0" minOccurs="0" type="xsd:string"/> 
<somethingElse/> 
<intIdID maxOccurs="0" minOccurs="0" type="xs:integer"/> 
</t> 

正确的结果(所有这些匹配元素复制到输出)产生

<enitityID xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      maxOccurs="0" minOccurs="0" type="xsd:string"/> 


<intIdID xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     maxOccurs="0" minOccurs="0" type="xs:integer"/> 
+0

我需要匹配除xsd之外的其他xsd类型:字符串 – bretter 2012-02-29 15:23:38

+0

@bretter:学会说出你在问题中需要什么! – 2012-02-29 15:32:25

+0

@bretter:并看到我的更新为您的真正问题的正确解决方案。 – 2012-02-29 15:51:04

1

在感知模式的XSLT 2.0转换,如果类型属性类型为XS架构声明:QName的,那么你想*[namespace-uri-from-QName(@type) = 'http://www.w3.org/2001/XMLSchema']

相关问题