2012-02-13 9 views
6

मैं केवल उन तत्वों का चयन करने के लिए लागू-टेम्पलेट्स का उपयोग कैसे कर सकता हूं (मान नहीं) जो एक विशिष्ट पैटर्न के साथ समाप्त होता है? निम्नलिखित एक्सएमएल मान लें ...एक्सएसएल/XPath में उनके नाम के हिस्से से तत्वों का चयन कैसे करें?

<report> 
    <report_item> 
    <report_created_on/> 
    <report_cross_ref/> 
    <monthly_adj/> 
    <quarterly_adj/> 
    <ytd_adj/> 
    </report_item> 
    <report_item> 
    .... 
    </report_item> 
</report> 

मैं <report_item> के सभी उदाहरणों जहां वंशज तत्वों 'adj` के साथ समाप्त पर <xsl:apply-templates> उपयोग करना चाहते हैं, तो, इस मामले में, केवल monthly_adj, quaterly_adj, और ytd_adj चुना जाएगा और टेम्पलेट के साथ लागू किया।

<xsl:template match="report"> 
    <xsl:apply-templates select="report_item/(elements ending with 'adj')"/> 
</xsl:template> 

उत्तर

10

मुझे नहीं लगता कि इस संदर्भ में नियमित अभिव्यक्ति वाक्यविन्यास XSLT 2.0 में भी उपलब्ध है। लेकिन आपको इस मामले में इसकी आवश्यकता नहीं है।

<xsl:apply-templates select="report_item/*[ends-with(name(), 'adj')]"/> 

* किसी भी नोड

[pred] चयनकर्ता के खिलाफ एक नोड परीक्षण

name() रिटर्न (जहां pred एक विधेय चयनित नोड के संदर्भ में मूल्यांकन किया है) (इस मामले * में) करता है से मेल खाता है इस उद्देश्य के लिए तत्व का टैग नाम (local-name के बराबर होना चाहिए)।

ends-with() एक अंतर्निहित XPath स्ट्रिंग फ़ंक्शन है। (केवल XSLT 2.0+)

+0

नया स्टैक ओवरफ्लो वर्ल्ड रिकॉर्ड .... सबसे तेज़ उत्तर कभी, एलओएल, आप सही हैं, रेगेक्स नहीं, बस सरल 'समाप्त होता है' पर्याप्त था, धन्यवाद। – raffian

+0

मदद करने में खुशी हुई :) मैंने प्रश्न से बेहतर मिलान करने के लिए शीर्षक अपडेट किया है। – harpo

+0

हाँ, मैं खुद को अपडेट करने की योजना बना रहा था, धन्यवाद! – raffian

2

थोड़ा neater समाधान:

<xsl:apply-templates select="report_item/*[matches(name(),'adj$')]"/> 

यहाँ एक आत्म परीक्षण यह काम करता है साबित करने के लिए है। (सैक्सन पर परीक्षण)।

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:fn="http://www.w3.org/2005/xpath-functions" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       exclude-result-prefixes="xs fn"> 
<xsl:output method="xml" indent="yes" encoding="utf-8" /> 
<xsl:variable name="report-data"> 
    <report> 
    <report_item> 
    <report_created_on/> 
    <report_cross_ref/> 
    <monthly_adj>Jan</monthly_adj> 
    <quarterly_adj>Q1</quarterly_adj> 
    <ytd_adj>2012</ytd_adj> 
    </report_item> 
    </report> 
</xsl:variable> 
<xsl:template match="/" name="main"> 
    <reports-ending-with-adj> 
    <xsl:element name="using-regex"> 
    <xsl:apply-templates select="$report-data//report_item/*[fn:matches(name(),'adj$')]"/> 
    </xsl:element> 
    <xsl:element name="using-ends-with-function"> 
    <xsl:apply-templates select="$report-data//report_item/*[fn:ends-with(name(), 'adj')]"/> 
    </xsl:element> 
    </reports-ending-with-adj> 
</xsl:template> 
</xsl:stylesheet> 
+0

'मैच' ने मेरे लिए काम किया। लेकिन मुझे कोई समस्या है: http://xsltransform.net/pPzifp5/3 (मुझे 'मॉडर' के आदेश को यह कहना है कि 'li' मैं पहले, दूसरे, तीसरे को प्रदर्शित करना चाहता हूं ...) धन्यवाद। – Si8

+0

या ऐसा कुछ: http://xsltransform.net/pPzifp5/4 (मैं पैरेंट एलआई और उप एलआई प्रदर्शित करने के लिए देख रहा हूं लेकिन यह प्रत्येक पुनरावृत्ति और डुप्लिकेटिंग के लिए गुणा कर रहा है। खाली LI मैं केवल दिखाना नहीं चाहता दिखाएं कि इसमें कोई टेक्स्ट है या नहीं।) मदद के लिए धन्यवाद – Si8

+0

एक प्रश्न पोस्ट करें, और इसका उत्तर दिया जाएगा। –